]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Generating logging configuration
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 8 Aug 2017 10:59:47 +0000 (12:59 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 8 Aug 2017 10:59:47 +0000 (12:59 +0200)
pp_lib/config_named_app.py

index 93404dad0ce1c9144d78652b5f204ff03fe71458..15c9e3bb6fc2e049b5d2967b5416d8ad608f6bbd 100644 (file)
@@ -39,7 +39,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication
 
 from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile
 
-__version__ = '0.5.4'
+__version__ = '0.5.5'
 LOG = logging.getLogger(__name__)
 
 
@@ -101,6 +101,118 @@ class PpConfigNamedApp(PpConfigApplication):
             'errors': 'surrogateescape',
         }
 
+    log_channels = {
+        'named': {
+            'type': 'file',
+            'print-time': True,
+            'print-category': True,
+            'print-severity': True,
+        },
+        'syslog': {
+            'type': 'syslog',
+            'facility': 'daemon',
+            'print-category': True,
+        },
+        'security': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'config': {
+            'type': 'file',
+            'severity': 'debug',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'dnssec': {
+            'type': 'file',
+            'severity': 'dynamic',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'ratelimit': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'query': {
+            'type': 'file',
+            'severity': 'debug',
+            'print-time': True,
+        },
+        'query-error': {
+            'type': 'file',
+            'severity': 'notice',
+            'print-time': True,
+        },
+        'resolver': {
+            'type': 'file',
+            'severity': 'dynamic',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'xfer-in': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'xfer-out': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'update': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'notify': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'unmatched': {
+            'type': 'file',
+            'print-time': True,
+            'print-category': True,
+            'print-severity': True,
+        },
+        'network': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+        'lame-servers': {
+            'type': 'file',
+            'print-time': True,
+            'print-severity': True,
+        },
+    }
+
+    log_categories = {
+        'client': ['null'],
+        'config': ['syslog', 'named', 'config',],
+        'database': ['syslog', 'named',],
+        'default': ['syslog', 'named',],
+        'delegation-only': ['syslog', 'named',],
+        'dispatch': ['syslog', 'named',],
+        'dnssec': ['syslog', 'named', 'dnssec',],
+        'general': ['syslog', 'named',],
+        'lame-servers': ['lame-servers'],
+        'network': ['syslog', 'named', 'network',],
+        'notify': ['syslog', 'named', 'notify',],
+        'queries': ['query', 'query-error',],
+        'resolver': ['syslog', 'named', 'resolver',],
+        'rpz': ['syslog', 'named',],
+        'rate-limit': ['syslog', 'named', 'ratelimit',],
+        'security': ['syslog', 'named', 'security',],
+        'unmatched': ['syslog', 'named', 'unmatched',],
+        'update': ['syslog', 'named', 'update',],
+        'update-security': ['syslog', 'named', 'update', 'security',],
+        'xfer-in': ['syslog', 'named', 'xfer-in',],
+        'xfer-out': ['syslog', 'named', 'xfer-out',],
+    }
+
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, version=__version__):
 
@@ -702,6 +814,7 @@ class PpConfigNamedApp(PpConfigApplication):
 
         self.generate_acl_file()
         self.generate_named_conf()
+        self.generate_log_cfg_file()
 
         time.sleep(2)
 
@@ -758,8 +871,7 @@ class PpConfigNamedApp(PpConfigApplication):
             if acl_name in ('allow-notify', 'allow-transfer'):
                 lines.append('\t// Localhost')
                 lines.append('\t127.0.0.1;')
-                if self.named_listen_on_v6:
-                    lines.append('\t::1;')
+                lines.append('\t::1;')
 
             ips_done = []
 
@@ -804,7 +916,6 @@ class PpConfigNamedApp(PpConfigApplication):
         if self.verbose > 2:
             LOG.debug("Generated {!r}:\n{}".format(self.temp_acl_cfg_file, content.strip()))
 
-
     # -------------------------------------------------------------------------
     def generate_named_conf(self):
 
@@ -900,6 +1011,9 @@ class PpConfigNamedApp(PpConfigApplication):
         content += '\t};\n'
         content += '};\n'
 
+        content += '\n// logging configuration\n'
+        content += 'include "{}";\n'.format(self.named_log_cfg_file)
+
         content += '\n// vim: ts=8 filetype=named noet noai\n'
 
         with open(self.temp_named_conf, 'w', **self.open_args) as fh:
@@ -908,6 +1022,108 @@ class PpConfigNamedApp(PpConfigApplication):
         if self.verbose > 2:
             LOG.debug("Generated {!r}:\n{}".format(self.temp_named_conf, content.strip()))
 
+    # -------------------------------------------------------------------------
+    def generate_log_cfg_file(self):
+
+        LOG.info("Generating {} ...".format(self.default_named_log_cfg_file))
+
+        cur_date = datetime.datetime.now().isoformat(' ')
+
+        lines = []
+        lines.append('###############################################################')
+        lines.append('')
+        lines.append(' Bind9 configuration for logging')
+        lines.append(' {}'.format(self.named_log_cfg_file))
+        lines.append('')
+        lines.append(' Generated at: {}'.format(cur_date))
+        lines.append('')
+        lines.append('###############################################################')
+        header = textwrap.indent('\n'.join(lines), '//', lambda line: True) + '\n'
+
+        content = header
+
+        content += '\nlogging {\n';
+
+        content += '\n\t// -----------------------------------\n'
+        content += '\t// Channels\n'
+        for channel in sorted(self.log_channels.keys()):
+            channel_def = self.log_channels[channel]
+            lines = []
+            lines.append('')
+            lines.append('\tchannel {} {{'.format(channel))
+            ctype = 'file'
+            if 'type' in channel_def:
+                if channel_def['type'].lower() in ('file', 'syslog', 'stderr', 'null'):
+                    ctype = channel_def['type'].lower()
+                else:
+                    LOG.error("Wrong type {!r} for logging channel {!r}.".format(
+                        channel_def['type'], channel))
+                    continue
+            if ctype == 'file':
+                filename = os.path.join(self.named_logdir, channel + '.log')
+                lines.append('\t\tfile "{}";'.format(filename))
+            elif ctype == 'syslog':
+                fac = 'daemon'
+                if 'facility' in channel_def and channel_def['facility'].strip():
+                    fac = channel_def['facility'].strip().lower()
+                lines.append('\t\tsyslog {};'.format(fac))
+            else:
+                lines.append('\t\t{};'.format(ctype))
+
+            if 'severity' in channel_def and channel_def['severity'].strip():
+                lines.append('\t\tseverity {};'.format(channel_def['severity'].strip().lower()))
+
+            if 'print-category' in channel_def:
+                if to_bool(channel_def['print-category']):
+                    lines.append('\t\tprint-category yes;')
+
+            if 'print-severity' in channel_def:
+                if to_bool(channel_def['print-severity']):
+                    lines.append('\t\tprint-severity yes;')
+
+            if 'print-time' in channel_def:
+                if to_bool(channel_def['print-time']):
+                    lines.append('\t\tprint-time yes;')
+
+            lines.append('\t};')
+
+            content += '\n'.join(lines) + '\n'
+
+        content += '\n\t// -----------------------------------\n'
+        content += '\t// Categories\n'
+        for cat_name in sorted(self.log_categories.keys()):
+            lines = []
+            lines.append('')
+            channels = self.log_categories[cat_name]
+            lines.append('\tcategory {} {{'.format(cat_name))
+
+            if not channels:
+                channels = ['null']
+            if cat_name == 'queries':
+                if self.query_log:
+                    if not 'query' in channels:
+                        channels.append('query')
+                else:
+                    if 'query' in channels:
+                        channels.remove('query')
+
+            for channel in channels:
+                lines.append('\t\t{};'.format(channel))
+
+            lines.append('\t};')
+
+            content += '\n'.join(lines) + '\n'
+
+
+        content += '\n};\n'
+        content += '\n// vim: ts=8 filetype=named noet noai\n'
+
+        with open(self.temp_log_cfg_file, 'w', **self.open_args) as fh:
+            fh.write(content)
+
+        if self.verbose > 2:
+            LOG.debug("Generated {!r}:\n{}".format(self.temp_log_cfg_file, content.strip()))
+
     # -------------------------------------------------------------------------
     def get_api_zones(self):