d93ca92f7debda4ec904e73b33eb3beeb1ed8739
   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, sudo, sshd, cron, httpd, smbd, postfix, zfs, temperature
  19
  20def rotate():   
  21    # rotate logs using systemd logrotate
  22    if parser.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    parser = argparse.ArgumentParser(description='grab logs of some common services and send them by email')
  38    parser.add_argument('-t','--to', help='mail recipient (\"to\" address)', required=False)
  39    parser.add_argument('-c', '--config', help='path to config file', required=False)
  40    parser.add_argument('-p', '--print', help='print HTML to stdout', required=False, dest='printout', action='store_true', default=False)
  41    parser.add_argument('-d', '--destination', help='file to output HTML', required=False)
  42    parser.add_argument('-f', '--overwrite', help='force overwrite an existing output file', required=False, action='store_true', default=False)
  43    parser.add_argument('-v', '--verbose', help='verbose console/syslog output (for debugging)', required=False, default=False, action='store_true')
  44    parser.add_argument('-r', '--rotate', help='force rotate log files using systemd logrotate', required=False, default=False, action='store_true')
  45    parser.add_argument('-nr', '--no-rotate', help='do not rotate logfiles (overrides logparse.conf)', required=False, default=False, action='store_true')
  46    parser.add_argument('-l', '--logs', help='services to analyse', required=False)
  47
  48    # Load config
  49    if parser.parse_args().config:
  50        config.prefs = config.loadconf(parser.parse_args().config, parser)
  51    else:
  52        config.prefs = config.loadconf(argparser=parser)
  53    prefs = config.prefs
  54    
  55    # Set up logging
  56    logger = logging.getLogger(__name__)
  57    loghandler = logging.handlers.SysLogHandler(address = '/dev/log')
  58    loghandler.setFormatter(logging.Formatter(fmt='logparse.py[' + str(os.getpid()) + ']: %(message)s'))
  59    loghandler.setLevel(logging.WARNING)   # don't spam syslog with debug messages
  60    if parser.parse_args().verbose or (config.prefs['verbose'] == 'y' or config.prefs['verbose'] == 'yes'):
  61        print("Verbose mode is on")
  62        logging.basicConfig(level=logging.DEBUG)
  63        logger.debug("Verbose mode turned on")
  64    else:
  65        logging.basicConfig(level=logging.INFO)
  66    logger.addHandler(loghandler)
  67
  68    logger.debug("Finished loading config")
  69
  70    # Time analysis
  71    global start
  72    start = datetime.now()
  73    logger.info("Beginning log analysis at {0} {1}".format(start.strftime(formatting.DATEFMT), start.strftime(formatting.TIMEFMT)))
  74    logger.debug("This is {0} version {1}, running on Python {2}".format(logparse.__name__, logparse.__version__, sys.version.replace('\n', '')))
  75     
  76#    for l in parser.parse_args().logs.split(' '):
  77#        eval(l)
  78#        sys.exit()
  79
  80    print(load_parsers.search());
  81    # Write HTML document
  82    global output_html
  83    output_html = formatting.header(prefs['header'])
  84    output_html += sudo.parse_log()
  85    output_html += sshd.parse_log()
  86    output_html += cron.parse_log()
  87    output_html += httpd.parse_log()
  88    output_html += smbd.parse_log()
  89    output_html += postfix.parse_log()
  90    output_html += zfs.parse_log()
  91    output_html += temperature.parse_log()
  92    output_html += formatting.closetag('body') + formatting.closetag('html')
  93    if parser.parse_args().printout:
  94        print(output_html)
  95    if parser.parse_args().destination:
  96        logger.debug("Outputting to {0}".format(parser.parse_args().destination))
  97        if not os.path.isfile(parser.parse_args().destination) and not parser.parse_args().overwrite:
  98            with open(parser.parse_args().destination, 'w') as f:
  99                f.write(output_html)
 100                logger.info("Written output to {}".format(parser.parse_args().destination))
 101        else:
 102            logger.warning("Destination file already exists")
 103            if input("Would you like to overwrite {0}? (y/n) [n] ".format(parser.parse_args().destination)) == 'y':
 104                with open(parser.parse_args().destination, 'w') as f:
 105                    f.write(output_html)
 106                logger.debug("Written output to {}".format(parser.parse_args().destination))
 107            else:
 108                logger.warning("No output written")
 109
 110    if parser.parse_args().to:
 111       mail.sendmail(mailbin=prefs['mail']['mailbin'], body=output_html, recipient=parser.parse_args().to, subject="logparse test")
 112    
 113    # Print end message
 114    finish = datetime.now()
 115    logger.info("Finished parsing logs at {0} {1} (total time: {2})".format(finish.strftime(formatting.DATEFMT), finish.strftime(formatting.TIMEFMT), finish - start))
 116
 117    return