1""" 2This modules contains default config values and basic wrapper for ConfigParser. 3New config options should be added to the dictionary below, along with 4appropriate defaults. Runtime configuration is done through the config file at 5/etc/logparse/logparse.conf (default) or the path specified in the "--config" 6argument. The file uses the INI syntax, with general options being declared in 7the [logparse] section and parser-specific options declared in their own 8sections. 9 10This module provides the following methods: 11 - `loadconf()`: set up ConfigParser and process config file 12""" 13 14from configparser import ConfigParser 15from pkg_resources import Requirement, resource_filename 16 17import logparse 18import logging 19logger = logging.getLogger(__name__) 20 21global prefs 22prefs =None 23 24defaults = { 25'logparse': { 26'output':'', 27'overwrite':False, 28'title': logparse.__name__, 29'maxlist':10, 30'maxcmd':6, 31'resolve-domains':'fqdn', 32'rotate':False, 33'verbose':False, 34'quiet':False, 35'hostname-path':'/etc/hostname', 36'parsers':'', 37'ignore-parsers':'' 38}, 39'html': { 40'header':'/etc/logparse/header.html', 41'css':'/etc/logparse/main.css', 42'embed-styles':False, 43'css-relpath':True 44}, 45'plain': { 46'plain':False, 47'linewidth':80 48}, 49'logs': { 50'auth':'/var/log/auth.log', 51'cron':'/var/log/cron.log', 52'cpuinfo':'/proc/cpuinfo', 53'meminfo':'/proc/meminfo', 54'sys':'/var/log/syslog', 55'smbd':'/var/log/samba', 56'zfs':'/var/log/zpool.log', 57'alloc':'/var/log/du.log', 58'postfix':'/var/log/mail.log', 59'httpd-access':'/var/log/apache2/access.log', 60'httpd-error':'/var/log/apache2/error.log' 61}, 62'mail': { 63'to':'', 64'from':'', 65'subject':'logparse from $hostname', 66'mailbin':'/usr/bin/mail' 67}, 68'temperatures': { 69'drives': ['/dev/sda'], 70'host':'127.0.0.1', 71'separator':'|', 72'timeout':10, 73'port':7634, 74'show-model':False, 75}, 76'sshd': { 77'sshd-resolve-domains':'' 78}, 79'smbd': { 80'smbd-resolve-domains':'' 81}, 82'httpd': { 83'httpd-resolve-domains':'' 84}, 85'du': { 86'paths': ['/','/etc','/home'], 87'force-write':False 88} 89} 90 91 92defloadconf(configpaths): 93""" 94 Initial setup for a ConfigParser object. `configpaths` should be a list of 95 configuration files to load (typically only one). To use the generated 96 ConfigParser, use `import logparse.config` and then `config.prefs.get(..)`. 97 The prefs object is returned after creation as a convenience but this method 98 should only be called once per runtime. 99 """ 100 prefs=ConfigParser() 101 prefs.read_dict(defaults) 102try: 103 success = prefs.read(configpaths) 104 logger.debug("Loaded{0}config file(s):{1}".format(str(len(success)),str(success))) 105exceptExceptionas e: 106 logger.warning("Error processing config: "+str(e)) 107return prefs