From 4da12d5ca7b56965ec12345242c92e9f6b9d2c70 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Sat, 28 Jul 2018 01:37:44 +1000 Subject: [PATCH] add inline tags from premailer uses library premailer to convert stylesheet main.css to inline css tags which are friendlier for email clients.\ also implemented some more complex email stuff with mail metadata through postfix. --- header.html | 2 +- logparse.py | 47 +++++++++++++++++++++++++++++++++++++---------- main.css | 1 + 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/header.html b/header.html index e3af90d..0ca9aaa 100755 --- a/header.html +++ b/header.html @@ -1,7 +1,7 @@ - $title$ $version$ on $hostname$ ($date) + $title$ $version$ on $hostname$ ($date$) diff --git a/logparse.py b/logparse.py index 6bd01ff..cd1e632 100755 --- a/logparse.py +++ b/logparse.py @@ -1,6 +1,6 @@ #! /usr/bin/python -import argparse, logging, os, shutil, re, subprocess, sys, requests, glob, socket, sensors, datetime, time, operator +import argparse, logging, os, shutil, re, subprocess, sys, requests, glob, socket, sensors, datetime, time, operator, premailer from sys import stdin from collections import namedtuple, defaultdict @@ -22,14 +22,20 @@ DUPATHS = ["/home/andrew", "/mnt/andrew"] HDDTEMPS = ["/dev/sda", "/dev/sdc", "/dev/sdd", "/dev/sde"] HDDTEMPPORT = 7634 SUMMARYPATH = "/mnt/andrew/temp/logparse-test.html" +OUTPUTPATH = "/mnt/andrew/temp/logparse-test2.html" +MAILPATH = "/mnt/andrew/temp/log-parse-test-3.html" HEADERPATH = "header.html" STYLEPATH = "main.css" -OUTPUT = "" +MAILOUT = "" +HTMLOUT = "" +TXTOUT = "" TITLE = "logparse" MAXLIST = 10 CMDNO = 3 -MAILSUBJECT = "logparse from $host$" +MAILSUBJECT = "logparse from $hostname$" VERSION = "v0.1" +# DEG = u'\N{DEGREE SIGN}'.encode('utf-8') +DEG = 'C' # Set up logging logging.basicConfig(level=logging.DEBUG) @@ -64,9 +70,13 @@ def __main__(): for tag in ['div', 'body', 'html']: closetag(tag, 1) tempfile.close() + mailprep(SUMMARYPATH, MAILPATH) if (to != None): logger.debug("sending email") - subprocess.call("cat " + SUMMARYPATH + " | mail -a 'Content-type: text/html' -s " + subject(MAILSUBJECT) + ' ' + to, shell=True) + ms = subject(MAILSUBJECT) + cmd = "cat " + MAILPATH + " | mail --debug-level=10 -a 'Content-type: text/html' -s '" + ms + "' " + to + logger.debug(cmd) + subprocess.call(cmd, shell=True) logger.info("sent email") @@ -84,7 +94,7 @@ def writedata(subtitle, data = None): # write title and data to tempfile tag('p', 0, subtitle) if (data == None): - logger.warning("no data provided.. just printing subtitle") + logger.debug("no data provided.. just printing subtitle") else: logger.debug("received data " + str(data)) opentag('ul', 1) @@ -122,11 +132,14 @@ def header(template): # return a parsed html header from file return headercontent def subject(template): - return varpattern.sub(lambda m: varfilter[re.escape(m.group(0))], template) + r = varpattern.sub(lambda m: varfilter[re.escape(m.group(0))], template) + logger.debug("returning subject line " + r) + return r def hostname(): # get the hostname hnfile = open(HOSTNAMEPATH, 'r') - return hnfile.read() + hn = re.search('^(.*)\n*', hnfile.read()).group(1) + return hn def resolve(ip): # try to resolve an ip to hostname logger.debug("trying to resolve ip " + ip) @@ -136,7 +149,7 @@ def resolve(ip): # try to resolve an ip to hostname logger.debug("found hostname " + hn) return(hn) except: - logger.warning("failed to resolve hostname for " + ip) + logger.debug("failed to resolve hostname for " + ip) return(ip) # return ip if no hostname exists def plural(noun, quantity): # return "1 noun" or "n nouns" @@ -197,6 +210,19 @@ def truncl(input, limit): # truncate list else: return(input) +def mailprep(inputpath, outputpath, *stylesheet): + logger.debug("converting stylesheet to inline tags") + old = readlog(inputpath) + pm = premailer.Premailer(old, external_styles=STYLEPATH) + MAILOUT = pm.transform() + logger.info("converted stylesheet to inline tags") + file = open(outputpath, 'w') + file.write(MAILOUT) + file.close() + logger.info("written to temporary mail file") + + + # # # @@ -466,6 +492,7 @@ def zfs(): logger.debug("starting zfs section") opentag('div', 1, 'zfs', 'section') zfslog = readlog('zfs') + logger.debug("got zfs logfile\n" + zfslog + "---end log---") pool = re.search('.*---\n(\w*)', zfslog).group(1) scrub = re.search('.*scrub repaired (\d*) in \d*h\d*m with (\d*) errors on (\S*\s)(\S*)\s(\d+\s)', zfslog) iostat = re.search('.*---\n\w*\s*(\S*)\s*(\S*)\s', zfslog) @@ -509,7 +536,7 @@ def temp(): logger.debug("average cpu temp is " + str(core_avg)) coretemps.append(["avg", str(core_avg)]) coretemps.append(["pkg", pkgtemp]) - coretemps = [x[0] + ": " + str(x[1]) + '℃' for x in coretemps] + coretemps = [x[0] + ": " + str(x[1]) + DEG for x in coretemps] finally: sensors.cleanup() @@ -543,7 +570,7 @@ def temp(): data.append("avg: " + str(hddavg)) writetitle("temperatures") if (systemp != 0): - writedata("sys: " + str(systemp) + '℃') + writedata("sys: " + str(systemp) + DEG) if (coretemps != ''): writedata("cores", coretemps) if (hddtemps != ''): diff --git a/main.css b/main.css index 37278ea..bc52163 100755 --- a/main.css +++ b/main.css @@ -13,6 +13,7 @@ body { div#main { column-count: 2; + display: inline-block; } a { -- 2.43.2