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#
78
import logging, logging.handlers
9import argparse
10import os
11import glob
12import sys
13from subprocess import check_output
14from datetime import datetime
1516
import logparse
17import logparse.config
18from logparse.config import prefs, loadconf
19from logparse import formatting, mail, config, load_parsers
2021
global argparser
2223
def rotate(): # Rotate logs using systemd logrotate
24try:
25if not os.geteuid() == 0:
26if sys.stdin.isatty():
27logger.warning("Not running as root, using sudo (may require password to be entered)")
28rotate_shell = check_output("sudo logrotate /etc/logrotate.conf", shell=True)
29else:
30raise PermissionError("Root priviliges are required to run logrotate but are not provided")
31else:
32rotate_shell = check_output("/usr/sbin/logrotate /etc/logrotate.conf", shell=True)
33logger.info("Rotated logfiles")
34logger.debug("logrotate output: " + rotate_shell)
35except Exception as e:
36logger.warning("Failed to rotate log files: " + str(e))
3738
def rotate_sim(): # Simulate log rotation
39try:
40if not os.geteuid() == 0:
41logger.warning("Cannot run logrotate as root - you will see permission errors in the output below")
42sim_cmd = "logrotate -d /etc/logrotate.conf"
43logger.debug("Here is the output of `{0}` (simulated):".format(sim_cmd))
44sim = check_output(sim_cmd, shell=True)
45logger.debug(sim)
46except Exception as e:
47logger.warning("Failed to get logrotate simulation: " + str(e))
4849
50
def main():
51# Get arguments
52global argparser
53argparser = argparse.ArgumentParser(description='grab logs of some common services and send them by email')
54argparser.add_argument('-t','--to', help='mail recipient (\"to\" address)', required=False)
55argparser.add_argument('-c', '--config', help='path to config file', required=False, default="/etc/logparse/logparse.conf")
56argparser.add_argument('-p', '--print', help='print HTML to stdout', required=False, dest='printout', action='store_true', default=False)
57argparser.add_argument('-d', '--destination', help='file to output HTML', required=False)
58argparser.add_argument('-f', '--overwrite', help='force overwrite an existing output file', required=False, action='store_true', default=False)
59argparser.add_argument('-v', '--verbose', help='verbose console/syslog output (for debugging)', required=False, default=False, action='store_true')
60argparser.add_argument('-r', '--rotate', help='force rotate log files using systemd logrotate (overrides --rotate and "rotate" in logparse.conf)', required=False, default=False, action='store_true')
61argparser.add_argument('-nr', '--no-rotate', help='do not rotate logfiles (overrides --rotate and logparse.conf)', required=False, default=False, action='store_true')
62argparser.add_argument('-s', '--simulate', help="test run logrotate (do not actually change files)", required=False, default=False, action="store_true")
63argparser.add_argument('-l', '--logs', help='services to analyse', required=False)
64argparser.add_argument('-nl', '--ignore-logs', help='skip these services (takes precedence over -l)', required=False)
65argparser.add_argument('-es', '--embed-styles', help='make CSS rules inline rather than linking the file', required=False, default=False, action='store_true')
66argparser.add_argument('-nh', '--plain', help='write/send plain text rather than HTML', required=False, default=False, action='store_true')
67argparser.add_argument('-q', '--quiet', help='no output to stdout', required=False, default=False, action='store_true')
68argparser.add_argument('-nm', '--no-mail', help="do not send email (overrides config file)", required=False, default=False, action="store_true")
69argparser.add_argument('-nw', '--no-write', help="do not write output file (overrides config file)", required=False, default=False, action="store_true")
7071
# Load config
72config.prefs = loadconf(argparser.parse_args().config)
7374
# Set up logging
75logger = logging.getLogger(__name__)
76loghandler = logging.handlers.SysLogHandler(address = '/dev/log')
77loghandler.setFormatter(logging.Formatter(fmt='logparse[' + str(os.getpid()) + ']: %(message)s'))
78loghandler.setLevel(logging.INFO) # don't spam syslog with debug messages
79if argparser.parse_args().quiet or config.prefs.getboolean("logparse", "quiet"):
80logging.basicConfig(level=logging.CRITICAL)
81elif argparser.parse_args().verbose or config.prefs.getboolean("logparse", "verbose"):
82logging.basicConfig(level=logging.DEBUG)
83logger.debug("Verbose mode turned on")
84else:
85logging.basicConfig(level=logging.INFO)
86logger.addHandler(loghandler)
8788
logger.debug([x for x in config.prefs.sections()])
89logger.debug(config.prefs.get("logparse", "output"))
90logger.debug("Config test: " + config.prefs.get("logparse", "output"))
9192
# Time analysis
93global start
94start = datetime.now()
95logger.info("Beginning log analysis at {0} {1}".format(start.strftime(formatting.DATEFMT), start.strftime(formatting.TIMEFMT)))
96logger.debug("This is {0} version {1}, running on Python {2}".format(logparse.__name__, logparse.__version__, sys.version.replace('\n', '')))
9798
# Write header
99100
formatting.init_var()
101102
if argparser.parse_args().plain:
103output = formatting.PlaintextOutput(linewidth=config.prefs.getint("plain", "linewidth"))
104output.append_header()
105else:
106output = formatting.HtmlOutput()
107output.append_header(config.prefs.get("html", "header"))
108109
110
# Find parsers
111112
loader = load_parsers.ParserLoader("logparse.parsers")
113parser_names = set([x.name for x in loader.parsers])
114115
if argparser.parse_args().logs:
116parser_names = parser_names.intersection(set(argparser.parse_args().logs.split()))
117elif config.prefs.get("logparse", "parsers"):
118parser_names = parser_names.intersection(set(config.prefs.get("logparse", "parsers").split()))
119120
if argparser.parse_args().ignore_logs:
121parser_names = parser_names.difference(set(argparser.parse_args().ignore_logs.split()))
122elif config.prefs.get("logparse", "ignore-parsers"):
123parser_names = parser_names.difference(set(config.prefs.get("logparse", "ignore-parsers").split()))
124125
# Execute parsers
126127
logger.debug("Queued the following parsers: " + str(loader.parsers))
128for parser in loader.parsers:
129if parser.name in parser_names:
130output.append_section(parser.parse_log())
131132
# Write HTML footer
133output.append_footer()
134135
if (argparser.parse_args().destination or config.prefs.get("logparse", "output")) and not argparser.parse_args().no_write:
136if argparser.parse_args().destination:
137dest_path = argparser.parse_args().destination
138else:
139dest_path = config.prefs.get("logparse", "output")
140logger.debug("Outputting to {0}".format(dest_path))
141if (argparser.parse_args().embed_styles or config.prefs.getboolean("html", "embed-styles")) and not (argparser.parse_args().plain or config.prefs.getboolean("plain", "plain")):
142output.embed_css(config.prefs.get("html", "css"))
143if (not os.path.isfile(dest_path)) and not (argparser.parse_args().overwrite or config.prefs.getboolean("logparse", "overwrite")):
144output.write(dest_path)
145elif logging.root.level == logging.CRITICAL:
146pass
147else:
148logger.warning("Destination file already exists")
149if input("Would you like to overwrite {0}? (y/n) [n] ".format(dest_path)) == 'y':
150output.write(dest_path)
151else:
152logger.warning("No output written")
153154
if (str(argparser.parse_args().to) or str(config.prefs.get("mail", "to"))) and not argparser.parse_args().no_mail:
155if str(argparser.parse_args().to):
156to = argparser.parse_args().to
157else:
158to = config.prefs.get("mail", "to")
159mail.sendmail(
160mailbin=config.prefs.get("mail", "mailbin"),
161body=(output.embed_css(config.prefs.get("html", "css")) if isinstance(output, formatting.HtmlOutput) else output.content),
162recipient=to,
163subject=formatting.fsubject(config.prefs.get("mail", "subject")),
164html=isinstance(output, formatting.HtmlOutput),
165sender=config.prefs.get("mail", "from"))
166167
if not argparser.parse_args().no_rotate:
168if argparser.parse_args().simulate or config.prefs.getboolean("logparse", "rotate"):
169rotate_sim()
170elif config.prefs.getboolean("logparse", "rotate") or argparser.parse_args().rotate:
171rotate()
172else:
173logger.debug("User doesn't want to rotate logs")
174else:
175logger.debug("User doesn't want to rotate logs")
176177
# Print end message
178finish = datetime.now()
179logger.info("Finished parsing logs at {0} {1} (total time: {2})".format(finish.strftime(formatting.DATEFMT), finish.strftime(formatting.TIMEFMT), finish - start))
180181
if argparser.parse_args().printout:
182output.print_stdout()
183184
return