From 6b6fec6ca92d82c05b56d7224541e56eed5f2d37 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Tue, 6 Nov 2018 18:12:24 +1100 Subject: [PATCH] add time test and start fixing relative css path --- logparse.py | 52 +++++++++++++++++++++++++++++---------------------- logparse.yaml | 3 ++- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/logparse.py b/logparse.py index 3a82e96..59370bc 100755 --- a/logparse.py +++ b/logparse.py @@ -29,7 +29,7 @@ config = { 'from': '', 'subject': 'logparse from $hostname$' }, - 'rotate': 1, + 'rotate': 'y', 'hddtemp': { 'drives': ['/dev/sda'], 'port': 7634 @@ -81,16 +81,27 @@ logger.addHandler(loghandler) # 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) -to = parser.parse_args().to def __main__(): logger.info("Beginning log analysis at " + str(datenow) + ' ' + str(timenow)) - if (to == None): + loadconf(scriptdir + "/logparse.yaml") + + if (config['mail']['to'] == None): logger.info("no recipient address provided, outputting to stdout") else: - logger.info("email will be sent to " + to) + logger.info("email will be sent to " + config['mail']['to']) - loadconf(scriptdir + "/logparse.yaml") + global pathfilter + global pathpattern + pathfilter = {"auth": config['logs']['auth'], "cron": config['logs']['cron'], "sys": config['logs']['sys'], "postfix": config['logs']['postfix'], "smb": config['logs']['smb'], "zfs": config['logs']['zfs'], "alloc": config['logs']['alloc'], "httpd": config['logs']['httpd'], "header": config['header']} + pathfilter = dict((re.escape(k), v) for k, v in pathfilter.iteritems()) + pathpattern = re.compile("|".join(pathfilter.keys())) + + global varfilter + global varpattern + varfilter = {"$title$": config['title'], "$date$": datenow, "$time$": timenow, "$hostname$": hostname(), "$version$": VERSION, "$css$": os.path.relpath(config['css'], os.path.dirname(config['output']))} + varfilter = dict((re.escape(k), v) for k, v in varfilter.iteritems()) + varpattern = re.compile("|".join(varfilter.keys())) global tempfile tempfile = open(config['output'], 'w+') @@ -110,10 +121,10 @@ def __main__(): closetag(tag, 1) tempfile.close() mailprep(config['output'], MAILPATH) - if (to != None): + if (config['mail']['to'] != None): logger.debug("sending email") ms = subject(config['mail']['subject']) - cmd = "/bin/cat " + MAILPATH + " | /usr/bin/mail --debug-level=10 -a 'Content-type: text/html' -s '" + ms + "' " + to + cmd = "/bin/cat " + MAILPATH + " | /usr/bin/mail --debug-level=10 -a 'Content-type: text/html' -s '" + ms + "' " + config['mail']['to'] logger.debug(cmd) subprocess.call(cmd, shell=True) logger.info("sent email") @@ -738,18 +749,10 @@ def du(): # # # - +starttime = datetime.datetime.now() timenow = time.strftime("%H:%M:%S") datenow = time.strftime("%x") -pathfilter = {"auth": config['logs']['auth'], "cron": config['logs']['cron'], "sys": config['logs']['sys'], "postfix": config['logs']['postfix'], "smb": config['logs']['smb'], "zfs": config['logs']['zfs'], "alloc": config['logs']['alloc'], "httpd": config['logs']['httpd'], "header": config['header']} -pathfilter = dict((re.escape(k), v) for k, v in pathfilter.iteritems()) -pathpattern = re.compile("|".join(pathfilter.keys())) - -varfilter = {"$title$": config['title'], "$date$": datenow, "$time$": timenow, "$hostname$": hostname(), "$version$": VERSION, "$css$": config['css']} -varfilter = dict((re.escape(k), v) for k, v in varfilter.iteritems()) -varpattern = re.compile("|".join(varfilter.keys())) - def loadconf(configfile): try: data = yaml.safe_load(open(configfile)) @@ -761,6 +764,9 @@ def loadconf(configfile): else: config[value] = data[value] config['dest'] = os.path.dirname(config['output']) + logger.debug(str(type(parser.parse_args().to))) + logger.debug(config['mail']['to']) + if parser.parse_args().to is not None: config['mail']['to'] = parser.parse_args().to logger.debug(str(config)) except Exception as e: logger.warning("error processing config: " + str(e)) @@ -770,14 +776,16 @@ try: __main__() finally: # rotate logs using systemd logrotate - if (config['rotate'] == 1): + if (config['rotate'] == 'y'): subprocess.call("/usr/sbin/logrotate -f /etc/logrotate.conf", shell=True) logger.info("rotated logfiles") else: - logger.info("user doesn't want to rotate logs") - 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) + logger.debug("user doesn't want to rotate logs") + if (config['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) + timenow = time.strftime("%H:%M:%S") datenow = time.strftime("%x") - logger.info("finished parsing logs at " + str(datenow) + ' ' + str(timenow)) + logger.info("finished parsing logs at " + datetime.datetime.now().strftime("%x %H:%M:%S") + " (" + str(datetime.datetime.now() - starttime) + ")") diff --git a/logparse.yaml b/logparse.yaml index ee81a36..a9dcaa8 100755 --- a/logparse.yaml +++ b/logparse.yaml @@ -11,4 +11,5 @@ hddtemp: port: 7634 du-paths: - /home/andrew - - /mnt/andrew \ No newline at end of file + - /mnt/andrew +rotate: n \ No newline at end of file -- 2.43.2