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