#
# config.py
#
-# Default config values and basic wrapper for PyYaml. New config options
+# Default config values and basic wrapper for ConfigParser. New config options
# should be added to the dictionary below, along with appropriate defaults.
#
+# Runtime configuration is done through /etc/logparse/logparse.conf (default)
+# or the path specified in the "--config" argument. The file uses the INI
+# syntax, with general options being declared in the [logparse] section and
+# parser-specific options declared in their own sections.
+#
-import yaml
-import types
-import os
+from configparser import ConfigParser
from pkg_resources import Requirement, resource_filename
import logparse
import logging
logger = logging.getLogger(__name__)
+global prefs
+prefs = None
+
+defaults = {
+ 'logparse': {
+ 'output': '',
+ 'overwrite': False,
+ 'title': logparse.__name__,
+ 'maxlist': 10,
+ 'maxcmd': 6,
+ 'resolve-domains': 'fqdn',
+ 'rotate': False,
+ 'verbose': False,
+ 'quiet': False,
+ 'hostname-path': '/etc/hostname',
+ 'parsers': '',
+ 'ignore-parsers': ''
+ },
+ 'html': {
+ 'header': '/etc/logparse/header.html',
+ 'css': '/etc/logparse/main.css',
+ 'embed-styles': False,
+ 'css-relpath': True
+ },
+ 'plain': {
+ 'plain': False,
+ 'linewidth': 80
+ },
+ 'logs': {
+ 'auth': '/var/log/auth.log',
+ 'cron': '/var/log/cron.log',
+ 'cpuinfo': '/proc/cpuinfo',
+ 'meminfo': '/proc/meminfo',
+ 'sys': '/var/log/syslog',
+ 'smbd': '/var/log/samba',
+ 'zfs': '/var/log/zpool.log',
+ 'alloc': '/var/log/du.log',
+ 'postfix': '/var/log/mail.log',
+ 'httpd-access': '/var/log/apache2/access.log',
+ 'httpd-error': '/var/log/apache2/error.log'
+ },
+ 'mail': {
+ 'to': '',
+ 'from': '',
+ 'subject': 'logparse from $hostname',
+ 'mailbin': '/usr/bin/mail'
+ },
+ 'temperatures': {
+ 'drives': ['/dev/sda'],
+ 'host': '127.0.0.1',
+ 'separator': '|',
+ 'timeout': 10,
+ 'port': 7634,
+ 'show-model': False,
+ },
+ 'sshd': {
+ 'sshd-resolve-domains': ''
+ },
+ 'smbd': {
+ 'smbd-resolve-domains': ''
+ },
+ 'httpd': {
+ 'httpd-resolve-domains': ''
+ },
+ 'du': {
+ 'paths': ['/', '/etc', '/home'],
+ 'force-write': False
+ }
+}
+
def locate(filename):
+ """
+ DEPRECATED: draft method for what is now parsers/load_parsers.py. Kept here
+ for historical purposes.
+ """
logger.debug("Searching for {0}".format(filename))
loc = resource_filename(Requirement.parse(__package__), filename)
logger.debug("Found {0}".format(loc))
return loc
-prefs = {
- 'output': '/var/www/logparse/summary.html',
- 'header': '/etc/logparse/header.html',
- 'css': '/etc/logparse/main.css',
- 'title': logparse.__name__,
- 'maxlist': 10,
- 'maxcmd': 3,
- 'resolve-domains': 'fqdn',
- 'mail': {
- 'to': '',
- 'from': '',
- 'subject': 'logparse from $hostname$',
- 'mailbin': '/usr/bin/mail',
- },
- 'rotate': 'n',
- 'verbose': 'n',
- 'hddtemp': {
- 'drives': ['/dev/sda'],
- 'host': 'localhost',
- 'separator': '|',
- 'timeout': 10,
- 'port': 7634,
- 'show-model': False,
- },
- 'apache': {
- 'resolve-domains': '',
- },
- 'sshd': {
- 'resolve-domains': '',
- },
- 'smbd': {
- 'resolve-domains': '',
- },
- 'httpd': {
- 'resolve-domains': '',
- },
- 'du': {
- 'paths': ['/', '/etc', '/home'],
- 'force-write': 'n',
- },
- 'hostname-path': '/etc/hostname',
- 'logs': {
- 'auth': '/var/log/auth.log',
- 'cron': '/var/log/cron.log',
- 'sys': '/var/log/syslog',
- 'smb': '/var/log/samba',
- 'zfs': '/var/log/zpool.log',
- 'alloc': '/var/log/du.log',
- 'postfix': '/var/log/mail.log',
- 'httpd': '/var/log/apache2'
- }
-}
-
-def loadconf(argparser, configfile = "/etc/logparse/logparse.conf"):
- logger.debug("getting config from {0}".format(configfile))
+def loadconf(configpaths):
+ """
+ Initial setup for a ConfigParser object. `configpaths` should be a list of
+ configuration files to load (typically only one). To use the generated
+ ConfigParser, use `import logparse.config` and then `config.prefs.get(..)`.
+ The prefs object is returned after creation as a convenience but this method
+ should only be called once per runtime.
+ """
+ prefs= ConfigParser()
+ prefs.read_dict(defaults)
try:
- data = yaml.safe_load(open(configfile))
- for value in data:
- if(isinstance(data[value], dict)):
- for key in data[value].items():
- prefs[value][key[0]] = key[1]
- else:
- prefs[value] = data[value]
-# config['dest'] = paths.dirname(config['output'])
- 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")
- else:
- logger.info("email will be sent to " + prefs['mail']['to'])
- return prefs
+ success = prefs.read(configpaths)
+ logger.debug("Loaded {0} config file(s): {1}".format(str(len(success)), str(success)))
except Exception as e:
- logger.warning("error processing config: " + str(e))
+ logger.warning("Error processing config: " + str(e))
+ return prefs