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