migrate configuration system to the stdlib ConfigParser
[logparse.git] / logparse / interface.py
index 583d6b61c7c939b00b18d02b63d9b77ff67fefe4..ef8d7fcb869e79f4121724c1abc8b601faca3fd4 100644 (file)
@@ -14,10 +14,13 @@ from subprocess import check_output
 from datetime import datetime
 
 import logparse
-from .config import *
+import logparse.config
+from logparse.config import prefs, loadconf
 from logparse import formatting, mail, config
 from .parsers import load_parsers
 
+global argparser
+
 def rotate():       # Rotate logs using systemd logrotate
     try:
         if not os.geteuid() == 0:
@@ -38,7 +41,7 @@ def rotate_sim():   # Simulate log rotation
         if not os.geteuid() == 0:
             logger.warning("Cannot run logrotate as root - you will see permission errors in the output below")
         sim_cmd = "logrotate -d /etc/logrotate.conf"
-        logger.debug("Here is the output of `{0}` (simulated):".format(sim_cmd)
+        logger.debug("Here is the output of `{0}` (simulated):".format(sim_cmd))
         sim = check_output(sim_cmd, shell=True)
         logger.debug(sim)
     except Exception as e:
@@ -47,9 +50,10 @@ def rotate_sim():   # Simulate log rotation
 
 def main():
     # Get arguments
+    global argparser
     argparser = argparse.ArgumentParser(description='grab logs of some common services and send them by email')
     argparser.add_argument('-t','--to', help='mail recipient (\"to\" address)', required=False)
-    argparser.add_argument('-c', '--config', help='path to config file', required=False)
+    argparser.add_argument('-c', '--config', help='path to config file', required=False, default="/etc/logparse/logparse.conf")
     argparser.add_argument('-p', '--print', help='print HTML to stdout', required=False, dest='printout', action='store_true', default=False)
     argparser.add_argument('-d', '--destination', help='file to output HTML', required=False)
     argparser.add_argument('-f', '--overwrite', help='force overwrite an existing output file', required=False, action='store_true', default=False)
@@ -62,29 +66,29 @@ def main():
     argparser.add_argument('-es', '--embed-styles', help='make CSS rules inline rather than linking the file', required=False, default=False, action='store_true')
     argparser.add_argument('-nh', '--plain', help='write/send plain text rather than HTML', required=False, default=False, action='store_true')
     argparser.add_argument('-q', '--quiet', help='no output to stdout', required=False, default=False, action='store_true')
+    argparser.add_argument('-nm', '--no-mail', help="do not send email (overrides config file)", required=False, default=False, action="store_true")
+    argparser.add_argument('-nw', '--no-write', help="do not write output file (overrides config file)", required=False, default=False, action="store_true")
 
     # Load config
-    if argparser.parse_args().config:
-        config.prefs = config.loadconf(argparser.parse_args().config, argparser)
-    else:
-        config.prefs = config.loadconf(argparser=argparser)
-    prefs = config.prefs
+    config.prefs = loadconf(argparser.parse_args().config)
     
     # Set up logging
     logger = logging.getLogger(__name__)
     loghandler = logging.handlers.SysLogHandler(address = '/dev/log')
     loghandler.setFormatter(logging.Formatter(fmt='logparse[' + str(os.getpid()) + ']: %(message)s'))
     loghandler.setLevel(logging.INFO)   # don't spam syslog with debug messages
-    if argparser.parse_args().quiet or config.prefs['quiet']:
+    if argparser.parse_args().quiet or config.prefs.getboolean("logparse", "quiet"):
         logging.basicConfig(level=logging.CRITICAL)
-    elif argparser.parse_args().verbose or config.prefs['verbose']:
+    elif argparser.parse_args().verbose or config.prefs.getboolean("logparse", "verbose"):
         logging.basicConfig(level=logging.DEBUG)
         logger.debug("Verbose mode turned on")
     else:
         logging.basicConfig(level=logging.INFO)
     logger.addHandler(loghandler)
 
-    logger.debug("Finished loading config")
+    logger.debug([x for x in config.prefs.sections()])
+    logger.debug(config.prefs.get("logparse", "output"))
+    logger.debug("Config test: " + config.prefs.get("logparse", "output"))
 
     # Time analysis
     global start
@@ -94,21 +98,23 @@ def main():
      
     # Write header
 
-    global output
+    formatting.init_var()
+
     if argparser.parse_args().plain:
-        output = formatting.PlaintextOutput(linewidth=prefs['linewidth'])
+        output = formatting.PlaintextOutput(linewidth=config.prefs.getint("plain", "linewidth"))
+        output.append_header()
     else:
         output = formatting.HtmlOutput()
+        output.append_header(config.prefs.get("html", "header"))
 
-    output.append_header(prefs['header'])
 
     # Find parsers
     
     parser_providers = []
     if argparser.parse_args().logs:
         log_src = argparser.parse_args().logs.split()
-    elif len(prefs['parsers']) > 0:
-        log_src = prefs['parsers']
+    elif config.prefs.get("logparse", "parsers"):
+        log_src = config.prefs.get("logparse", "parsers").split()
     else:
         log_src = load_parsers.default_parsers
 
@@ -120,11 +126,13 @@ def main():
         else:
             parser_providers.append(load_parsers.load(parser))
 
-    if argparser.parse_args().ignore_logs or len(prefs['ignore-parsers']) > 0:
-        if argparser.parse_args().ignore_logs:
-            ignore_src = argparser.parse_args().ignore_logs.split()
-        else:
-            ignore_src = prefs['ignore-parsers']
+    if argparser.parse_args().ignore_logs:
+        ignore_src = argparser.parse_args().ignore_logs.split()
+    elif config.prefs.get("logparse", "ignore-parsers"):
+        ignore_src = config.prefs.get("logparse", "ignore-parsers").split()
+    else:
+        ignore_src = []
+    if len(ignore_src) > 0:
         for parser_name in ignore_src:
             if parser_name in [x.__name__.rpartition('.')[2] for x in parser_providers]:
                 logger.info("Ignoring default parser {0}".format(parser_name))
@@ -144,17 +152,15 @@ def main():
     # Write HTML footer
     output.append_footer()
 
-    if argparser.parse_args().printout:
-        print(output)
-    if argparser.parse_args().destination or prefs['output']:
+    if (argparser.parse_args().destination or config.prefs.get("logparse", "output")) and not argparser.parse_args().no_write:
         if argparser.parse_args().destination:
             dest_path = argparser.parse_args().destination
         else:
-            dest_path = prefs['output']
+            dest_path = config.prefs.get("logparse", "output")
         logger.debug("Outputting to {0}".format(dest_path))
-        if (argparser.parse_args().embed_styles or prefs['embed-styles']) and not (argparser.parse_args().plain or prefs['plain']):
-            output.embed_css(prefs['css'])
-        if (not os.path.isfile(dest_path)) and not (argparser.parse_args().overwrite or config['overwrite']):
+        if (argparser.parse_args().embed_styles or config.prefs.getboolean("html", "embed-styles")) and not (argparser.parse_args().plain or config.prefs.getboolean("plain", "plain")):
+            output.embed_css(config.prefs.get("html", "css"))
+        if (not os.path.isfile(dest_path)) and not (argparser.parse_args().overwrite or config.prefs.getboolean("logparse", "overwrite")):
             output.write(dest_path)
         elif logging.root.level == logging.CRITICAL:
             pass
@@ -165,17 +171,23 @@ def main():
             else:
                 logger.warning("No output written")
 
-    if argparser.parse_args().to or prefs['mail']['to']:
-        if argparser.parse_args().to:
+    if (str(argparser.parse_args().to) or str(config.prefs.get("mail", "to"))) and not argparser.parse_args().no_mail:
+        if str(argparser.parse_args().to):
             to = argparser.parse_args().to
         else:
-            to = prefs['mail']['to']
-        mail.sendmail(mailbin=prefs['mail']['mailbin'], body=(output.embed_css(prefs['css']) if isinstance(output, formatting.HtmlOutput) else output.content), recipient=to, subject=formatting.fsubject(config.prefs['mail']['subject']), html=isinstance(output, formatting.HtmlOutput), sender=prefs['mail']['from'])
+            to = config.prefs.get("mail", "to")
+        mail.sendmail(
+            mailbin=config.prefs.get("mail", "mailbin"),
+            body=(output.embed_css(config.prefs.get("html", "css")) if isinstance(output, formatting.HtmlOutput) else output.content),
+            recipient=to,
+            subject=formatting.fsubject(config.prefs.get("mail", "subject")),
+            html=isinstance(output, formatting.HtmlOutput),
+            sender=config.prefs.get("mail", "from"))
 
     if not argparser.parse_args().no_rotate:
-        if argparser.parse_args().simulate or prefs['rotate'] == 's':
+        if argparser.parse_args().simulate or config.prefs.getboolean("logparse", "rotate"):
             rotate_sim()
-        elif prefs['rotate'] or argparser.parse_args().rotate:
+        elif config.prefs.getboolean("logparse", "rotate") or argparser.parse_args().rotate:
             rotate()
         else:
             logger.debug("User doesn't want to rotate logs")
@@ -186,4 +198,7 @@ def main():
     finish = datetime.now()
     logger.info("Finished parsing logs at {0} {1} (total time: {2})".format(finish.strftime(formatting.DATEFMT), finish.strftime(formatting.TIMEFMT), finish - start))
 
+    if argparser.parse_args().printout:
+        output.print_stdout()
+
     return