3dc2e46a3eaaa1a7f13971cb2277ee01a9c23c6b
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': 3,
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 'sys': '/var/log/syslog',
91 'smb': '/var/log/samba',
92 'zfs': '/var/log/zpool.log',
93 'alloc': '/var/log/du.log',
94 'postfix': '/var/log/mail.log',
95 'httpd': '/var/log/apache2'
96 }
97})
98
99def verify(raw_dict, defaults):
100 for key, value in raw_dict.items():
101 if key in defaults: # valid key
102 logger.debug("Found valid key {0} with value {1}".format(key, value))
103 if (isinstance(value, dict)):
104 verify(value, defaults[key]) # recurse nested dictionaries
105
106 else: # invalid key
107 logger.warning("Invalid key {0} with value {1}".format(key, value))
108
109def loadconf(argparser, configfile = "/etc/logparse/logparse.conf"):
110 logger.debug("Getting config from {0}".format(configfile))
111 try:
112 raw_dict = yaml.safe_load(open(configfile))
113 # verify fields
114 verify(raw_dict, defaults)
115 prefs = defaults
116 for value in raw_dict:
117 if(isinstance(raw_dict[value], dict)):
118 for key in raw_dict[value].items():
119 logger.debug("Inserting key {0} with value {1}".format(key[0], key[1]))
120 if not value in prefs:
121 prefs[value] = {}
122 prefs[value][key[0]] = key[1]
123 else:
124 prefs[value] = raw_dict[value]
125 if argparser.parse_args().to is not None:
126 prefs['mail']['to'] = argparser.parse_args().to
127 if not prefs['mail']['to']:
128 logger.info("No recipient address provided, outputting to stdout")
129 else:
130 logger.info("Email will be sent to " + prefs['mail']['to'])
131 return prefs
132 except Exception as e:
133 logger.warning("Error processing config: " + str(e))