logparse / config.pyon commit rename parsers, better journald integration (e1f7605)
   1"""
   2This modules contains default config values and basic wrapper for ConfigParser.
   3New config options should be added to the dictionary below, along with 
   4appropriate defaults. Runtime configuration is done through the config file at
   5/etc/logparse/logparse.conf (default) or the path specified in the "--config"
   6argument. The file uses the INI syntax, with general options being declared in
   7the [logparse] section and parser-specific options declared in their own
   8sections.
   9
  10This module provides the following methods:
  11    - `loadconf()`: set up ConfigParser and process config file
  12"""
  13
  14from configparser import ConfigParser
  15from pkg_resources import Requirement, resource_filename
  16
  17import logparse
  18import logging
  19logger = logging.getLogger(__name__)
  20
  21global prefs
  22prefs = None
  23
  24defaults = {
  25        'logparse': {
  26            'output': '',
  27            'overwrite': False,
  28            'title': logparse.__name__,
  29            'maxlist': 10,
  30            'maxcmd': 6,
  31            'resolve-domains': 'fqdn',
  32            'rotate': False,
  33            'verbose': False, 
  34            'quiet': False,
  35            'hostname-path': '/etc/hostname',
  36            'parsers': '',
  37            'ignore-parsers': '',
  38            'period': '1 week',
  39            'datetime-format': "%%b %%d %%H:%%M:%%S",
  40            'journald': True
  41        },
  42        'html': {
  43            'header':  '/etc/logparse/header.html',
  44            'css': '/etc/logparse/main.css',
  45            'embed-styles': False,
  46            'css-relpath': True
  47        },
  48        'plain': {
  49            'plain': False,
  50            'linewidth': 80
  51        },
  52        'logs': {
  53            'auth': '/var/log/auth.log',
  54            'cron': '/var/log/cron.log',
  55            'cpuinfo': '/proc/cpuinfo',
  56            'meminfo': '/proc/meminfo',
  57            'uptime': '/proc/uptime',
  58            'sys': '/var/log/syslog',
  59            'smbd': '/var/log/samba',
  60            'zfs': '/var/log/zpool.log',
  61            'alloc': '/var/log/du.log',
  62            'postfix': '/var/log/mail.log',
  63            'httpd-access': '/var/log/apache2/access.log',
  64            'httpd-error': '/var/log/apache2/error.log'
  65        },
  66        'cron': {
  67            'summary': False,
  68            'list-users': True,
  69            'period': '',
  70            'datetime-format': '',
  71            'truncate-commands': True,
  72            'users': '.*',
  73            'commands': '.*'
  74        },
  75        'mail': {
  76            'to': '',
  77            'from': '',
  78            'subject': 'logparse from $hostname',
  79            'mailbin': '/usr/bin/mail'
  80        },
  81        'temperatures': {
  82            'drives': ['/dev/sda'],
  83            'host': '127.0.0.1',
  84            'separator': '|',
  85            'timeout': 10,
  86            'port': 7634,
  87            'show-model': False, 
  88            'period': ''
  89        },
  90        'sshd': {
  91            'sshd-resolve-domains': '',
  92            'period': ''
  93        },
  94        'smbd': {
  95            'shares': '^((?!IPC\$).)*$',
  96            'users': '.*',
  97            'smbd-resolve-domains': '',
  98            'period': ''
  99        },
 100        'httpd': {
 101            'httpd-resolve-domains': '',
 102            'datetime-format': "%%d/%%b/%%Y:%%H:%%M:%%S %%z",
 103            'period': '',
 104            'clients': '.*',
 105            'files': '.*',
 106            'referrers': '.*',
 107            'access-format': "%%h %%l %%u %%t \"%%r\" %%>s %%O \"%%{Referer}i\" \"%%{User-Agent}i\""
 108        },
 109        'du': {
 110            'paths': ['/', '/etc', '/home'],
 111            'force-write': False
 112        },
 113        'ufw': {
 114            'ufw-resolve-domains': '',
 115            'period': ''
 116        },
 117        'sudo': {
 118            'journald': '',
 119            'datetime-format': '',
 120            'period': '',
 121            'list-users': True,
 122            'summary': True,
 123            'truncate-commands': True,
 124            'init-users': '.*',
 125            'superusers': '.*',
 126            'commands': '.*',
 127            'directories': '.*'
 128
 129        },
 130        'systemd': {
 131            'period': '',
 132            'show-all': True
 133        }
 134}
 135
 136
 137def loadconf(configpaths):
 138    """
 139    Initial setup for a ConfigParser object. `configpaths` should be a list of
 140    configuration files to load (typically only one). To use the generated
 141    ConfigParser, use `import logparse.config` and then `config.prefs.get(..)`.
 142    The prefs object is returned after creation as a convenience but this method
 143    should only be called once per runtime.
 144    """
 145    prefs= ConfigParser()
 146    prefs.read_dict(defaults)
 147    try:
 148        success = prefs.read(configpaths)
 149        logger.debug("Loaded {0} config file(s): {1}".format(
 150                str(len(success)), str(success)))
 151    except Exception as e:
 152        logger.warning("Error processing config: " + str(e))
 153    return prefs