5913b6baacc9107b501f6f561a2f89efa186f1bb
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 . import config
17from logparse import formatting, mail
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 (config.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 (config.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 # Set up logging
49 logger = logging.getLogger(__name__)
50 loghandler = logging.handlers.SysLogHandler(address = '/dev/log')
51 loghandler.setFormatter(logging.Formatter(fmt='logparse.py[' + str(os.getpid()) + ']: %(message)s'))
52 loghandler.setLevel(logging.WARNING) # don't spam syslog with debug messages
53 if parser.parse_args().verbose:
54 print("Verbose mode is on")
55 logging.basicConfig(level=logging.DEBUG)
56 logger.debug("Verbose mode turned on")
57 else:
58 logging.basicConfig(level=logging.INFO)
59 logger.addHandler(loghandler)
60
61 # Load config
62 if parser.parse_args().config or config.prefs['verbose']:
63 config.prefs = config.loadconf(parser.parse_args().config, parser)
64 else:
65 config.prefs = config.loadconf(argparser=parser)
66 logger.debug("Finished loading config")
67
68 # Time analysis
69 global start
70 start = datetime.now()
71 logger.info("Beginning log analysis at {0} {1}".format(start.strftime(formatting.DATEFMT), start.strftime(formatting.TIMEFMT)))
72 logger.debug("This is {0} version {1}, running on Python {2}".format(logparse.__name__, logparse.__version__, sys.version))
73
74# for l in parser.parse_args().logs.split(' '):
75# eval(l)
76# sys.exit()
77
78# print(load_parsers.search());
79 # Write HTML document
80 global output_html
81 output_html = formatting.header(config.prefs['header'])
82 output_html += sudo.parse_log()
83 output_html += sshd.parse_log()
84 output_html += cron.parse_log()
85 output_html += httpd.parse_log()
86 output_html += smbd.parse_log()
87 output_html += postfix.parse_log()
88 output_html += zfs.parse_log()
89 output_html += temperature.parse_log()
90 output_html += formatting.closetag('body') + formatting.closetag('html')
91 if parser.parse_args().printout:
92 print(output_html)
93 if parser.parse_args().destination:
94 logger.debug("Outputting to {0}".format(parser.parse_args().destination))
95 if not os.path.isfile(parser.parse_args().destination) and not parser.parse_args().overwrite:
96 with open(parser.parse_args().destination, 'w') as f:
97 f.write(output_html)
98 logger.info("Written output to {}".format(parser.parse_args().destination))
99 else:
100 logger.warning("Destination file already exists")
101 if input("Would you like to overwrite {0}? (y/n) [n] ".format(parser.parse_args().destination)) == 'y':
102 with open(parser.parse_args().destination, 'w') as f:
103 f.write(output_html)
104 logger.debug("Written output to {}".format(parser.parse_args().destination))
105 else:
106 logger.warning("No output written")
107
108 if parser.parse_args().to:
109 mail.sendmail(mailbin=config.prefs['mail']['mailbin'], body=output_html, recipient=parser.parse_args().to, subject="logparse test")
110
111 # Print end message
112 finish = datetime.now()
113 logger.info("Finished parsing logs at {0} {1} (total time: {2})".format(finish.strftime(formatting.DATEFMT), finish.strftime(formatting.TIMEFMT), finish - start))
114
115 return