logparse / config.pyon commit finish initial plugin system, get email and other features working (d8d26ab)
   1#
   2#   config.py
   3#
   4#   Default config values and basic wrapper for PyYaml. New config options
   5#   should be added to the dictionary below, along with appropriate defaults.
   6#
   7
   8import yaml
   9import types
  10import os
  11from pkg_resources import Requirement, resource_filename
  12from types import SimpleNamespace
  13
  14import logparse
  15import logging
  16logger = logging.getLogger(__name__)
  17
  18def locate(filename):
  19    logger.debug("Searching for {0}".format(filename))
  20    loc = resource_filename(Requirement.parse(__package__), filename)
  21    logger.debug("Found {0}".format(loc))
  22    return loc
  23
  24class Configuration(dict):
  25
  26    def __init__(self, *arg, **kw):
  27        super(Configuration, self).__init__(*arg, **kw)
  28    
  29    def _str2bool(x):
  30        positives = ["yes", "true", "1", "y"]
  31        negatives = ["no", "false", "0", "n"]
  32        x = x.lower()
  33        if x in positives:
  34            return True
  35        elif x in negatives:
  36            return False
  37        else:
  38            raise ValueError("Unknown option %s" % x)
  39
  40defaults = Configuration({
  41    'output': '',
  42    'header':  '/etc/logparse/header.html',
  43    'css': '/etc/logparse/main.css',
  44    'embed-styles': False,
  45    'overwrite': False,
  46    'title': logparse.__name__,
  47    'maxlist': 10,
  48    'maxcmd': 3,
  49    'resolve-domains': 'fqdn',
  50    'mail': {
  51        'to': '',
  52        'from': '',
  53        'subject': 'logparse from $hostname$',
  54        'mailbin': '/usr/bin/mail',
  55    },
  56    'rotate': 'n',
  57    'verbose': 'n',
  58    'hddtemp': {
  59        'drives': ['/dev/sda'],
  60        'host': '127.0.0.1',
  61        'separator': '|',
  62        'timeout': 10,
  63        'port': 7634,
  64        'show-model': False, 
  65    },
  66    'apache': {
  67        'resolve-domains': '',
  68    },
  69    'sshd': {
  70        'resolve-domains': '',
  71    },
  72    'smbd': {
  73        'resolve-domains': '',
  74    },
  75    'httpd': {
  76        'resolve-domains': '',
  77    },
  78    'du': {
  79        'paths': ['/', '/etc', '/home'],
  80        'force-write': 'n',
  81    },
  82    'hostname-path': '/etc/hostname',
  83    'parsers': {},
  84    'ignore-parsers': {},
  85    'logs': {
  86        'auth': '/var/log/auth.log',
  87        'cron': '/var/log/cron.log',
  88        'sys': '/var/log/syslog',
  89        'smb': '/var/log/samba',
  90        'zfs': '/var/log/zpool.log',
  91        'alloc': '/var/log/du.log',
  92        'postfix': '/var/log/mail.log',
  93        'httpd': '/var/log/apache2'
  94    }
  95})
  96
  97def verify(raw_dict, defaults):
  98    for key, value in raw_dict.items():
  99        if key in defaults: # valid key
 100            logger.debug("Found valid key {0} with value {1}".format(key, value))
 101            if (isinstance(value, dict)):
 102                verify(value, defaults[key])   # recurse nested dictionaries
 103
 104        else:               # invalid key
 105            logger.warning("Invalid key {0} with value {1}".format(key, value))
 106
 107def loadconf(argparser, configfile = "/etc/logparse/logparse.conf"):
 108    logger.debug("Getting config from {0}".format(configfile))
 109    try:
 110        raw_dict = yaml.safe_load(open(configfile))
 111        # verify fields
 112        verify(raw_dict, defaults)
 113        prefs = defaults
 114        for value in raw_dict:
 115            if(isinstance(raw_dict[value], dict)):
 116                for key in raw_dict[value].items():
 117                    logger.debug("Inserting key {0} with value {1}".format(key[0], key[1]))
 118                    if not value in prefs:
 119                        prefs[value] = {}
 120                    prefs[value][key[0]] = key[1]
 121            else:
 122                prefs[value] = raw_dict[value]
 123        if argparser.parse_args().to is not None:
 124            prefs['mail']['to'] = argparser.parse_args().to
 125        if not prefs['mail']['to']:
 126            logger.info("No recipient address provided, outputting to stdout")
 127        else:
 128            logger.info("Email will be sent to " + prefs['mail']['to'])
 129        return prefs 
 130    except Exception as e:
 131        logger.warning("Error processing config: " + str(e))