c37ec9a946f64cf0ece2486d4a8a937b5f9633c6
   1#
   2#   __main__.py
   3#
   4#   This module is the entrypoint of the `logparse` shell command and also
   5#   contains single-use functions which don't fit elsewhere.
   6#
   7
   8import logging, logging.handlers
   9import argparse
  10import os
  11import glob
  12import sys
  13from datetime import datetime
  14
  15import logparse
  16from .config import *
  17from logparse import formatting, mail, config
  18from .parsers import load_parsers
  19
  20def rotate():   
  21    # rotate logs using systemd logrotate
  22    if argparser.parse_args().function is None:
  23        if (prefs['rotate'] == 'y'):
  24            subprocess.call("/usr/sbin/logrotate -f /etc/logrotate.conf", shell=True)
  25            logger.info("rotated logfiles")
  26        else:
  27            logger.debug("user doesn't want to rotate logs")
  28            if (prefs['rotate'] == 's'):
  29                logger.debug("Here is the output of `logrotate -d /etc/logrotate.conf` (simulated):")
  30                sim = subprocess.check_output("/usr/sbin/logrotate -d /etc/logrotate.conf", shell=True)
  31                logger.debug(sim)
  32
  33
  34
  35def main():
  36    # Get arguments
  37    argparser = argparse.ArgumentParser(description='grab logs of some common services and send them by email')
  38    argparser.add_argument('-t','--to', help='mail recipient (\"to\" address)', required=False)
  39    argparser.add_argument('-c', '--config', help='path to config file', required=False)
  40    argparser.add_argument('-p', '--print', help='print HTML to stdout', required=False, dest='printout', action='store_true', default=False)
  41    argparser.add_argument('-d', '--destination', help='file to output HTML', required=False)
  42    argparser.add_argument('-f', '--overwrite', help='force overwrite an existing output file', required=False, action='store_true', default=False)
  43    argparser.add_argument('-v', '--verbose', help='verbose console/syslog output (for debugging)', required=False, default=False, action='store_true')
  44    argparser.add_argument('-r', '--rotate', help='force rotate log files using systemd logrotate', required=False, default=False, action='store_true')
  45    argparser.add_argument('-nr', '--no-rotate', help='do not rotate logfiles (overrides logparse.conf)', required=False, default=False, action='store_true')
  46    argparser.add_argument('-l', '--logs', help='services to analyse', required=False)
  47    argparser.add_argument('-nl', '--ignore-logs', help='skip these services (takes precedence over -l)', required=False)
  48    argparser.add_argument('-es', '--embed-styles', help='make CSS rules inline rather than linking the file', required=False, default=False, action='store_true')
  49    argparser.add_argument('-nh', '--plain', help='write/send plain text rather than HTML', required=False, default=False, action='store_true')
  50    argparser.add_argument('-q', '--quiet', help='no output to stdout', required=False, default=False, action='store_true')
  51
  52    # Load config
  53    if argparser.parse_args().config:
  54        config.prefs = config.loadconf(argparser.parse_args().config, argparser)
  55    else:
  56        config.prefs = config.loadconf(argparser=argparser)
  57    prefs = config.prefs
  58    
  59    # Set up logging
  60    logger = logging.getLogger(__name__)
  61    loghandler = logging.handlers.SysLogHandler(address = '/dev/log')
  62    loghandler.setFormatter(logging.Formatter(fmt='logparse.py[' + str(os.getpid()) + ']: %(message)s'))
  63    loghandler.setLevel(logging.INFO)   # don't spam syslog with debug messages
  64    if argparser.parse_args().quiet or config.prefs['quiet']:
  65        logging.basicConfig(level=logging.CRITICAL)
  66    elif argparser.parse_args().verbose or config.prefs['verbose']:
  67        logging.basicConfig(level=logging.DEBUG)
  68        logger.debug("Verbose mode turned on")
  69    else:
  70        logging.basicConfig(level=logging.INFO)
  71    logger.addHandler(loghandler)
  72
  73    logger.debug("Finished loading config")
  74
  75    # Time analysis
  76    global start
  77    start = datetime.now()
  78    logger.info("Beginning log analysis at {0} {1}".format(start.strftime(formatting.DATEFMT), start.strftime(formatting.TIMEFMT)))
  79    logger.debug("This is {0} version {1}, running on Python {2}".format(logparse.__name__, logparse.__version__, sys.version.replace('\n', '')))
  80     
  81    # Write header
  82
  83    global output
  84    if argparser.parse_args().plain:
  85        output = formatting.PlaintextOutput(linewidth=prefs['linewidth'])
  86    else:
  87        output = formatting.HtmlOutput()
  88
  89    output.append_header(prefs['header'])
  90
  91    # Find parsers
  92    
  93    parser_providers = []
  94    if argparser.parse_args().logs:
  95        log_src = argparser.parse_args().logs.split()
  96    elif len(prefs['parsers']) > 0:
  97        log_src = prefs['parsers']
  98    else:
  99        log_src = load_parsers.default_parsers
 100
 101    for parser_name in log_src:
 102        parser = load_parsers.search(parser_name)
 103        if parser == None:
 104            logger.warning("Can't find parser {0}".format(parser_name))
 105            continue
 106        else:
 107            parser_providers.append(load_parsers.load(parser))
 108
 109    if argparser.parse_args().ignore_logs or len(prefs['ignore-parsers']) > 0:
 110        if argparser.parse_args().ignore_logs:
 111            ignore_src = argparser.parse_args().ignore_logs.split()
 112        else:
 113            ignore_src = prefs['ignore-parsers']
 114        for parser_name in ignore_src:
 115            if parser_name in [x.__name__.rpartition('.')[2] for x in parser_providers]:
 116                logger.info("Ignoring default parser {0}".format(parser_name))
 117                parser_providers_new = []
 118                for p in parser_providers:
 119                    if p.__name__.rpartition('.')[2] != parser_name:
 120                        parser_providers_new.append(p)
 121                parser_providers = parser_providers_new
 122                continue
 123
 124    # Execute parsers
 125
 126    logger.debug(str(parser_providers))
 127    for parser in parser_providers:
 128        output.append_section(parser.parse_log())
 129
 130    # Write HTML footer
 131    output.append_footer()
 132
 133    if argparser.parse_args().printout:
 134        print(output)
 135    if argparser.parse_args().destination or prefs['output']:
 136        if argparser.parse_args().destination:
 137            dest_path = argparser.parse_args().destination
 138        else:
 139            dest_path = prefs['output']
 140        logger.debug("Outputting to {0}".format(dest_path))
 141        if (argparser.parse_args().embed_styles or prefs['embed-styles']) and not (argparser.parse_args().plain or prefs['plain']):
 142            output.embed_css(prefs['css'])
 143        if (not os.path.isfile(dest_path)) and not (argparser.parse_args().overwrite or config['overwrite']):
 144            output.write(dest_path)
 145        elif logging.root.level == logging.CRITICAL:
 146            pass
 147        else:
 148            logger.warning("Destination file already exists")
 149            if input("Would you like to overwrite {0}? (y/n) [n] ".format(dest_path)) == 'y':
 150                output.write(dest_path)
 151            else:
 152                logger.warning("No output written")
 153
 154    if argparser.parse_args().to or prefs['mail']['to']:
 155        if argparser.parse_args().to:
 156            to = argparser.parse_args().to
 157        else:
 158            to = prefs['mail']['to']
 159        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'])
 160
 161    # Print end message
 162    finish = datetime.now()
 163    logger.info("Finished parsing logs at {0} {1} (total time: {2})".format(finish.strftime(formatting.DATEFMT), finish.strftime(formatting.TIMEFMT), finish - start))
 164
 165    return