add inline tags from premailer
[logparse.git] / logparse.py
index 6bd01ff9fe4598fcdf4e4f86141178803dd1259c..cd1e632b4bfa3a748b43dcefebca17f19767eb17 100755 (executable)
@@ -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 != ''):