e2aa830a327458e7afde8e03d3b9f00bf1b479f2
   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 day'
  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        },
 107        'systemctl': {
 108            'period': '',
 109            'show-all': True
 110        }
 111}
 112
 113
 114def loadconf(configpaths):
 115    """
 116    Initial setup for a ConfigParser object. `configpaths` should be a list of
 117    configuration files to load (typically only one). To use the generated
 118    ConfigParser, use `import logparse.config` and then `config.prefs.get(..)`.
 119    The prefs object is returned after creation as a convenience but this method
 120    should only be called once per runtime.
 121    """
 122    prefs= ConfigParser()
 123    prefs.read_dict(defaults)
 124    try:
 125        success = prefs.read(configpaths)
 126        logger.debug("Loaded {0} config file(s): {1}".format(str(len(success)), str(success)))
 127    except Exception as e:
 128        logger.warning("Error processing config: " + str(e))
 129    return prefs