#! /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
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)
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")
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)
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)
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"
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")
+
+
+
#
#
#
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)
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()
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 != ''):