import types
import os
from pkg_resources import Requirement, resource_filename
+from types import SimpleNamespace
import logparse
import logging
logger.debug("Found {0}".format(loc))
return loc
-prefs = {
- 'output': '/var/www/logparse/summary.html',
+class Configuration(dict):
+
+ def __init__(self, *arg, **kw):
+ super(Configuration, self).__init__(*arg, **kw)
+
+ def _str2bool(x):
+ positives = ["yes", "true", "1", "y"]
+ negatives = ["no", "false", "0", "n"]
+ x = x.lower()
+ if x in positives:
+ return True
+ elif x in negatives:
+ return False
+ else:
+ raise ValueError("Unknown option %s" % x)
+
+defaults = Configuration({
+ 'output': '',
'header': '/etc/logparse/header.html',
'css': '/etc/logparse/main.css',
+ 'linewidth': 80,
+ 'embed-styles': False,
+ 'plain': False,
+ 'overwrite': False,
'title': logparse.__name__,
'maxlist': 10,
'maxcmd': 3,
'verbose': 'n',
'hddtemp': {
'drives': ['/dev/sda'],
- 'host': 'localhost',
+ 'host': '127.0.0.1',
'separator': '|',
'timeout': 10,
'port': 7634,
'force-write': 'n',
},
'hostname-path': '/etc/hostname',
+ 'parsers': {},
+ 'ignore-parsers': {},
'logs': {
'auth': '/var/log/auth.log',
'cron': '/var/log/cron.log',
'postfix': '/var/log/mail.log',
'httpd': '/var/log/apache2'
}
-}
+})
+
+def verify(raw_dict, defaults):
+ for key, value in raw_dict.items():
+ if key in defaults: # valid key
+ logger.debug("Found valid key {0} with value {1}".format(key, value))
+ if (isinstance(value, dict)):
+ verify(value, defaults[key]) # recurse nested dictionaries
+
+ else: # invalid key
+ logger.warning("Invalid key {0} with value {1}".format(key, value))
def loadconf(argparser, configfile = "/etc/logparse/logparse.conf"):
- logger.debug("getting config from {0}".format(configfile))
+ logger.debug("Getting config from {0}".format(configfile))
try:
- data = yaml.safe_load(open(configfile))
- for value in data:
- if(isinstance(data[value], dict)):
- for key in data[value].items():
+ raw_dict = yaml.safe_load(open(configfile))
+ # verify fields
+ verify(raw_dict, defaults)
+ prefs = defaults
+ for value in raw_dict:
+ if(isinstance(raw_dict[value], dict)):
+ for key in raw_dict[value].items():
+ logger.debug("Inserting key {0} with value {1}".format(key[0], key[1]))
+ if not value in prefs:
+ prefs[value] = {}
prefs[value][key[0]] = key[1]
else:
- prefs[value] = data[value]
-# config['dest'] = paths.dirname(config['output'])
+ prefs[value] = raw_dict[value]
if argparser.parse_args().to is not None:
prefs['mail']['to'] = argparser.parse_args().to
if not prefs['mail']['to']:
- logger.info("no recipient address provided, outputting to stdout")
+ logger.info("No recipient address provided, outputting to stdout")
else:
- logger.info("email will be sent to " + prefs['mail']['to'])
+ logger.info("Email will be sent to " + prefs['mail']['to'])
return prefs
except Exception as e:
- logger.warning("error processing config: " + str(e))
+ logger.warning("Error processing config: " + str(e))