#!/usr/bin/env python # Given a maildrop log file, generate stats on how many messages it filed per # day in folders matching regexps. import sys, os, re, time, getopt groups = [ ("spam", re.compile(r'[/\.]spam(|-learn)$')), ("virus", re.compile(r'[/\.]virus$')), ("other", None) ] def analyse(): f = open(os.getenv("HOME") + "/.maildroplog") date = None days = {} for l in f.xreadlines(): l = l.strip() if l == "": continue fs = l.split(": ", 1) if fs[0] == "Date": date = time.strptime(fs[1]) date = (date[0], date[1], date[2], 0, 0, 0, date[6], date[7], 0) elif fs[0] == "File": fn = fs[1].split()[0] i = 0 for (g, r) in groups: if r is None or r.search(fn) is not None: dh = days.setdefault(date, [0] * len(groups)) dh[i] += 1 break i += 1 f.close() daylist = days.keys() daylist.sort() f = open("maildropstats.data", "w") for day in daylist[1:-1]: counts = days[day] f.write(time.strftime("%Y-%m-%d", day) + " " + " ".join(map(str, counts)) + "\n") f.close() def plot(outputfile, format): if format == "png": terminal = "png truecolor size 800x600 small" else: terminal = "postscript color size 29.7cm,21cm" gp = os.popen("gnuplot", "w") gp.write(""" set terminal """ + terminal + """ set output '""" + outputfile + """' set title 'Mail counts' set xlabel 'Date' set ylabel 'Number of messages per day' set xdata time set timefmt '%Y-%m-%d' set format x '%Y-%m' set data style lines set key top left plot """) i = 1 sep = "" for (g, r) in groups: i += 1 gp.write(sep + "'maildropstats.data' using 1:" + str(i) + " title '" + g + "'") sep = ", " gp.write("\n") gp.close() def usage(): print """maildropstats by Adam Sampson Usage: maildropstats [OPTION...] -r, --replot Don't reanalyse; just plot with the existing data -p, --png Produce PNG output (default Postscript) -o FILE Write output file FILE (default maildropstats.ps/png) -h, --help Show usage Report bugs to .""" def main(args): try: opts, args = getopt.getopt(args, "rpho:", ["replot", "png", "help"]) except getopt.GetoptError: usage() sys.exit(1) replot = False format = "ps" outputfile = None for (o, a) in opts: if o in ("-h", "--help"): usage() sys.exit(0) elif o in ("-r", "--replot"): replot = True elif o in ("-p", "--png"): format = "png" elif o == "-o": outputfile = a if outputfile is None: outputfile = "maildropstats." + format if not replot: analyse() plot(outputfile, format) if __name__ == "__main__": main(sys.argv[1:])