-#
-# __main__.py
-#
-# This module is the entrypoint of the `logparse` shell command and also
-# contains single-use functions which don't fit elsewhere.
-#
-
-import logging, logging.handlers
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+"""
+This module is the entrypoint of the `logparse` shell command and also contains
+single-use functions which don't fit elsewhere. All user interaction with
+logparse should be through this module.
+
+This module provides the following methods:
+ - `main()`: Set up arguments, config, logging, and execute parsers
+ - `rotate()`: Rotate logs using systemd logrotate
+ - `rotate_sim()`: Simulate log rotation
+"""
+
import argparse
+from copy import copy
+import logging
+import logging.handlers
import os
-import glob
-import sys
+from sys import stdin, version
+from subprocess import check_output
from datetime import datetime
import logparse
-from . import config
-from logparse import formatting, mail
-from .parsers import load_parsers, sudo, sshd, cron, httpd, smbd, postfix, zfs, temperature
-
-def rotate():
- # rotate logs using systemd logrotate
- if parser.parse_args().function is None:
- if (config.prefs['rotate'] == 'y'):
- subprocess.call("/usr/sbin/logrotate -f /etc/logrotate.conf", shell=True)
- logger.info("rotated logfiles")
- else:
- logger.debug("user doesn't want to rotate logs")
- if (config.prefs['rotate'] == 's'):
- logger.debug("Here is the output of `logrotate -d /etc/logrotate.conf` (simulated):")
- sim = subprocess.check_output("/usr/sbin/logrotate -d /etc/logrotate.conf", shell=True)
- logger.debug(sim)
-
+from logparse import formatting, mail, config, load_parsers, util
def main():
+ """
+ Initialisation and general management of logparse functionaliy.
+ """
+
# Get arguments
- parser = argparse.ArgumentParser(description='grab logs of some common services and send them by email')
- parser.add_argument('-t','--to', help='mail recipient (\"to\" address)', required=False)
- parser.add_argument('-c', '--config', help='path to config file', required=False)
- parser.add_argument('-p', '--print', help='print HTML to stdout', required=False, dest='printout', action='store_true', default=False)
- parser.add_argument('-d', '--destination', help='file to output HTML', required=False)
- parser.add_argument('-f', '--overwrite', help='force overwrite an existing output file', required=False, action='store_true', default=False)
- parser.add_argument('-v', '--verbose', help='verbose console/syslog output (for debugging)', required=False, default=False, action='store_true')
- parser.add_argument('-r', '--rotate', help='force rotate log files using systemd logrotate', required=False, default=False, action='store_true')
- parser.add_argument('-nr', '--no-rotate', help='do not rotate logfiles (overrides logparse.conf)', required=False, default=False, action='store_true')
- parser.add_argument('-l', '--logs', help='services to analyse', required=False)
+ global argparser
+ argparser = get_argparser()
+
+ # Load config
+
+ config.prefs = config.loadconf(argparser.parse_args().config)
+ if argparser.parse_args().time_period:
+ config.prefs.set("logparse", "period",
+ argparser.parse_args().time_period)
+
# Set up logging
logger = logging.getLogger(__name__)
- loghandler = logging.handlers.SysLogHandler(address = '/dev/log')
- loghandler.setFormatter(logging.Formatter(fmt='logparse.py[' + str(os.getpid()) + ']: %(message)s'))
- loghandler.setLevel(logging.WARNING) # don't spam syslog with debug messages
- if parser.parse_args().verbose:
- print("Verbose mode is on")
- logging.basicConfig(level=logging.DEBUG)
+ if (argparser.parse_args().quiet
+ or config.prefs.getboolean("logparse", "quiet")):
+ logparse.logger.setLevel(logging.CRITICAL)
+ elif (argparser.parse_args().verbose
+ or config.prefs.getboolean("logparse", "verbose")):
+ logparse.logger.setLevel(logging.DEBUG)
logger.debug("Verbose mode turned on")
else:
- logging.basicConfig(level=logging.INFO)
- logger.addHandler(loghandler)
+ logparse.logger.setLevel(logging.INFO)
+
- # Load config
- if parser.parse_args().config or config.prefs['verbose']:
- config.prefs = config.loadconf(parser.parse_args().config, parser)
- else:
- config.prefs = config.loadconf(argparser=parser)
- logger.debug("Finished loading config")
# Time analysis
+
global start
start = datetime.now()
- logger.info("Beginning log analysis at {0} {1}".format(start.strftime(formatting.DATEFMT), start.strftime(formatting.TIMEFMT)))
- logger.debug("This is {0} version {1}, running on Python {2}".format(logparse.__name__, logparse.__version__, sys.version))
+ logger.info("Beginning log analysis at {0} {1}".format(
+ start.strftime(formatting.DATEFMT), start.strftime(formatting.TIMEFMT)))
+ logger.debug("This is {0} version {1}, running on Python {2}".format(
+ logparse.__name__, logparse.__version__, version.replace('\n', '')))
-# for l in parser.parse_args().logs.split(' '):
-# eval(l)
-# sys.exit()
-
-# print(load_parsers.search());
- # Write HTML document
- global output_html
- output_html = formatting.header(config.prefs['header'])
- output_html += sudo.parse_log()
- output_html += sshd.parse_log()
- output_html += cron.parse_log()
- output_html += httpd.parse_log()
- output_html += smbd.parse_log()
- output_html += postfix.parse_log()
- output_html += zfs.parse_log()
- output_html += temperature.parse_log()
- output_html += formatting.closetag('body') + formatting.closetag('html')
- if parser.parse_args().printout:
- print(output_html)
- if parser.parse_args().destination:
- logger.debug("Outputting to {0}".format(parser.parse_args().destination))
- if not os.path.isfile(parser.parse_args().destination) and not parser.parse_args().overwrite:
- with open(parser.parse_args().destination, 'w') as f:
- f.write(output_html)
- logger.info("Written output to {}".format(parser.parse_args().destination))
+ # Write header
+
+ formatting.init_var()
+
+ if argparser.parse_args().plain:
+ output = formatting.PlaintextOutput(
+ linewidth=config.prefs.getint("plain", "linewidth"))
+ output.append_header()
+ else:
+ output = formatting.HtmlOutput()
+ output.append_header(config.prefs.get("html", "header"))
+
+ # Find parsers
+
+ parser_names = []
+ ignore_logs = []
+ if argparser.parse_args().logs:
+ parser_names = set(argparser.parse_args().logs.split())
+ elif config.prefs.get("logparse", "parsers"):
+ parser_names = set(config.prefs.get("logparse", "parsers").split())
+
+ if argparser.parse_args().ignore_logs:
+ ignore_logs = argparser.parse_args().ignore_logs.split()
+ elif config.prefs.get("logparse", "ignore-parsers"):
+ ignore_logs = config.prefs.get("logparse", "ignore-parsers").split()
+
+ # Set up parsers
+
+ loader = load_parsers.ParserLoader()
+ if parser_names:
+ for parser_name in parser_names:
+ if parser_name not in ignore_logs:
+ loader.search(parser_name)
+ else:
+ loader.load_pkg()
+ if ignore_logs:
+ loader.ignore(ignore_logs)
+
+ # Execute parsers
+
+ for parser in loader.parsers:
+ output.append_section(parser.parse_log())
+
+ # Write footer
+ output.append_footer()
+
+ # Write output
+ if ((argparser.parse_args().destination
+ or config.prefs.get("logparse", "output"))
+ and not argparser.parse_args().no_write):
+
+ # Determine destination path
+ if argparser.parse_args().destination:
+ dest_path = argparser.parse_args().destination
else:
+ dest_path = config.prefs.get("logparse", "output")
+
+ logger.debug("Outputting to {0}".format(dest_path))
+
+ # Determine whether to clobber old file
+ if (not os.path.isfile(dest_path)) \
+ and not (argparser.parse_args().overwrite
+ or config.prefs.getboolean("logparse", "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")):
+ # Embed CSS stylesheet
+ output.embed_css(config.prefs.get("html", "css"))
+ output.write_embedded(dest_path)
+ else:
+ output.write(dest_path)
+
+ elif logging.root.level == logging.CRITICAL:
+
+ # Don't write output if running in quiet mode (only stdout)
+ pass
+
+ else:
+
logger.warning("Destination file already exists")
- if input("Would you like to overwrite {0}? (y/n) [n] ".format(parser.parse_args().destination)) == 'y':
- with open(parser.parse_args().destination, 'w') as f:
- f.write(output_html)
- logger.debug("Written output to {}".format(parser.parse_args().destination))
+ if input("Would you like to overwrite {0}? (y/n) [n] "
+ .format(dest_path)) == 'y':
+ 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"))
+ output.write_embedded(dest_path)
+
+ else:
+ output.write(dest_path)
else:
logger.warning("No output written")
- if parser.parse_args().to:
- mail.sendmail(mailbin=config.prefs['mail']['mailbin'], body=output_html, recipient=parser.parse_args().to, subject="logparse test")
-
- # Print end message
+ # Send email if requested
+
+ 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 = 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"))
+
+ # Rotate logs if requested
+
+ if not argparser.parse_args().no_rotate:
+ if (argparser.parse_args().simulate
+ or config.prefs.getboolean("logparse", "rotate")):
+ rotate_sim()
+ elif (config.prefs.getboolean("logparse", "rotate")
+ or argparser.parse_args().rotate):
+ rotate()
+ else:
+ logger.debug("User doesn't want to rotate logs")
+ else:
+ logger.debug("User doesn't want to rotate logs")
+
+ # Finish up
+
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))
+ 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:
+ if isinstance(output, formatting.HtmlOutput) \
+ and argparser.parse_args().embed_styles \
+ or config.prefs.getboolean("html", "embed-styles"):
+ output.print_stdout_embedded()
+ else:
+ output.print_stdout()
return
+
+def get_argparser():
+ """
+ Initialise arguments (in a separate function for documentation purposes)
+ """
+
+ argparser = argparse.ArgumentParser(description=
+ 'Grab logs of some common services and send them by email')
+ argparser.add_argument('-t','--to', required=False,
+ help='mail recipient (\"to\" address)')
+ argparser.add_argument('-c', '--config', required=False,
+ default="/etc/logparse/logparse.conf",
+ help='path to config file')
+ argparser.add_argument('-p', '--print', required=False, dest='printout',
+ action='store_true', default=False,
+ help='print HTML to stdout')
+ argparser.add_argument('-d', '--destination', required=False,
+ help='file to output HTML')
+ argparser.add_argument('-f', '--overwrite', required=False,
+ action='store_true', default=False,
+ help='force overwrite an existing output file')
+ argparser.add_argument('-v', '--verbose', required=False, default=False,
+ action='store_true',
+ help='verbose console/syslog output (for debugging)')
+ argparser.add_argument('-r', '--rotate', required=False, default=False,
+ action='store_true',
+ help='force rotate log files using systemd logrotate (overrides \
+ --rotate and "rotate" in logparse.conf)')
+ argparser.add_argument('-nr', '--no-rotate', required=False, default=False,
+ action='store_true',
+ help='do not rotate log files (overrides config)')
+ argparser.add_argument('-s', '--simulate', required=False, default=False,
+ action="store_true",
+ help="test run logrotate (do not actually change files)")
+ argparser.add_argument('-l', '--logs', required=False,
+ help='services to analyse')
+ argparser.add_argument('-nl', '--ignore-logs', required=False,
+ help='skip these services (takes precedence over -l)')
+ argparser.add_argument('-es', '--embed-styles', required=False,
+ default=False, action='store_true',
+ help='make CSS rules inline rather than linking the file')
+ argparser.add_argument('-nh', '--plain', required=False, default=False,
+ action='store_true', help='write/send plain text rather than HTML')
+ argparser.add_argument('-q', '--quiet', required=False, default=False,
+ action='store_true', help='no output to stdout')
+ argparser.add_argument('-nm', '--no-mail', required=False, default=False,
+ action="store_true",
+ help="do not send email (overrides config file)")
+ argparser.add_argument('-nw', '--no-write', required=False, default=False,
+ action="store_true",
+ help="do not write output file (overrides config file)")
+ argparser.add_argument('-tp', '--time-period', required=False,
+ help="time period to analyse logs for (applies to all parsers)")
+
+ return argparser
+
+
+
+
+def rotate():
+ """
+ Rotate logs using systemd logrotate. This requires root privileges, and a
+ basic check for this is attempted below. Root password will be prompted
+ for if permissions are not automatically granted.
+ """
+
+ logger = logging.getLogger(__name__)
+ try:
+ if not os.geteuid() == 0:
+ if stdin.isatty():
+ logger.warning("Not running as root, using sudo \
+ (may require password to be entered)")
+ rotate_shell = check_output(
+ "sudo logrotate /etc/logrotate.conf", shell=True)
+ else:
+ raise PermissionError("Root priviliges are required to run \
+ logrotate but were not provided")
+ else:
+ rotate_shell = check_output(
+ "/usr/sbin/logrotate /etc/logrotate.conf", shell=True)
+ logger.info("Rotated logfiles")
+ logger.debug("logrotate output: " + rotate_shell)
+ except Exception as e:
+ logger.warning("Failed to rotate log files: " + str(e))
+
+
+def rotate_sim(): # Simulate log rotation
+ """
+ Simulate log rotation using logrotate's -d flag. This does not require root
+ privileges, but permission errors will be shown in the output without it.
+ """
+
+ logger = logging.getLogger(__name__)
+ try:
+ 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))
+ sim = check_output(sim_cmd, shell=True)
+ logger.debug(sim)
+ except Exception as e:
+ logger.warning("Failed to get logrotate simulation: " + str(e))