bcdc8b266722bbdc23ebf6da45639ceba5665bc4
   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 minute'
  39        },
  40        'html': {
  41            'header':  '/etc/logparse/header.html',
  42            'css': '/etc/logparse/main.css',
  43            'embed-styles': False,
  44            'css-relpath': True
  45        },
  46        'plain': {
  47            'plain': False,
  48            'linewidth': 80
  49        },
  50        'logs': {
  51            'auth': '/var/log/auth.log',
  52            'cron': '/var/log/cron.log',
  53            'cpuinfo': '/proc/cpuinfo',
  54            'meminfo': '/proc/meminfo',
  55            'uptime': '/proc/uptime',
  56            'sys': '/var/log/syslog',
  57            'smbd': '/var/log/samba',
  58            'zfs': '/var/log/zpool.log',
  59            'alloc': '/var/log/du.log',
  60            'postfix': '/var/log/mail.log',
  61            'httpd-access': '/var/log/apache2/access.log',
  62            'httpd-error': '/var/log/apache2/error.log'
  63        },
  64        'cron': {
  65            'period': ''
  66        },
  67        'mail': {
  68            'to': '',
  69            'from': '',
  70            'subject': 'logparse from $hostname',
  71            'mailbin': '/usr/bin/mail'
  72        },
  73        'temperatures': {
  74            'drives': ['/dev/sda'],
  75            'host': '127.0.0.1',
  76            'separator': '|',
  77            'timeout': 10,
  78            'port': 7634,
  79            'show-model': False, 
  80            'period': ''
  81        },
  82        'sshd': {
  83            'sshd-resolve-domains': '',
  84            'period': ''
  85        },
  86        'smbd': {
  87            'shares': '^((?!IPC\$).)*$',
  88            'users': '.*',
  89            'smbd-resolve-domains': '',
  90            'period': ''
  91        },
  92        'httpd': {
  93            'httpd-resolve-domains': '',
  94            'period': ''
  95        },
  96        'du': {
  97            'paths': ['/', '/etc', '/home'],
  98            'force-write': False
  99        },
 100        'ufw': {
 101            'ufw-resolve-domains': '',
 102            'period': ''
 103        },
 104        'sudo': {
 105            'period': '',
 106            'list-users': True,
 107            'summary': True,
 108            'truncate-commands': True
 109        },
 110        'systemctl': {
 111            'period': '',
 112            'show-all': True
 113        }
 114}
 115
 116
 117def loadconf(configpaths):
 118    """
 119    Initial setup for a ConfigParser object. `configpaths` should be a list of
 120    configuration files to load (typically only one). To use the generated
 121    ConfigParser, use `import logparse.config` and then `config.prefs.get(..)`.
 122    The prefs object is returned after creation as a convenience but this method
 123    should only be called once per runtime.
 124    """
 125    prefs= ConfigParser()
 126    prefs.read_dict(defaults)
 127    try:
 128        success = prefs.read(configpaths)
 129        logger.debug("Loaded {0} config file(s): {1}".format(str(len(success)), str(success)))
 130    except Exception as e:
 131        logger.warning("Error processing config: " + str(e))
 132    return prefs