c7455bd65323f639a11e516b2610a32a5eb22cdd
   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    'linewidth': 80,
  45    'embed-styles': False,
  46    'plain': False,
  47    'overwrite': False,
  48    'title': logparse.__name__,
  49    'maxlist': 10,
  50    'maxcmd': 6,
  51    'resolve-domains': 'fqdn',
  52    'mail': {
  53        'to': '',
  54        'from': '',
  55        'subject': 'logparse from $hostname$',
  56        'mailbin': '/usr/bin/mail',
  57    },
  58    'rotate': 'n',
  59    'verbose': 'n',
  60    'hddtemp': {
  61        'drives': ['/dev/sda'],
  62        'host': '127.0.0.1',
  63        'separator': '|',
  64        'timeout': 10,
  65        'port': 7634,
  66        'show-model': False, 
  67    },
  68    'apache': {
  69        'resolve-domains': '',
  70    },
  71    'sshd': {
  72        'resolve-domains': '',
  73    },
  74    'smbd': {
  75        'resolve-domains': '',
  76    },
  77    'httpd': {
  78        'resolve-domains': '',
  79    },
  80    'du': {
  81        'paths': ['/', '/etc', '/home'],
  82        'force-write': 'n',
  83    },
  84    'hostname-path': '/etc/hostname',
  85    'parsers': {},
  86    'ignore-parsers': {},
  87    'logs': {
  88        'auth': '/var/log/auth.log',
  89        'cron': '/var/log/cron.log',
  90        'cpuinfo': '/proc/cpuinfo',
  91        'meminfo': '/proc/meminfo',
  92        'sys': '/var/log/syslog',
  93        'smb': '/var/log/samba',
  94        'zfs': '/var/log/zpool.log',
  95        'alloc': '/var/log/du.log',
  96        'postfix': '/var/log/mail.log',
  97        'httpd': '/var/log/apache2'
  98    }
  99})
 100
 101def verify(raw_dict, defaults):
 102    for key, value in raw_dict.items():
 103        if key in defaults: # valid key
 104            logger.debug("Found valid key {0} with value {1}".format(key, value))
 105            if (isinstance(value, dict)):
 106                verify(value, defaults[key])   # recurse nested dictionaries
 107
 108        else:               # invalid key
 109            logger.warning("Invalid key {0} with value {1}".format(key, value))
 110
 111def loadconf(argparser, configfile = "/etc/logparse/logparse.conf"):
 112    logger.debug("Getting config from {0}".format(configfile))
 113    try:
 114        raw_dict = yaml.safe_load(open(configfile))
 115        # verify fields
 116        verify(raw_dict, defaults)
 117        prefs = defaults
 118        for value in raw_dict:
 119            if(isinstance(raw_dict[value], dict)):
 120                for key in raw_dict[value].items():
 121                    logger.debug("Inserting key {0} with value {1}".format(key[0], key[1]))
 122                    if not value in prefs:
 123                        prefs[value] = {}
 124                    prefs[value][key[0]] = key[1]
 125            else:
 126                prefs[value] = raw_dict[value]
 127        if argparser.parse_args().to is not None:
 128            prefs['mail']['to'] = argparser.parse_args().to
 129        if not prefs['mail']['to']:
 130            logger.info("No recipient address provided, outputting to stdout")
 131        else:
 132            logger.info("Email will be sent to " + prefs['mail']['to'])
 133        return prefs 
 134    except Exception as e:
 135        logger.warning("Error processing config: " + str(e))