Hi All!

Well, since we're posting scripts, I guess I'll post a simple one I
whippedup to give me a quick idea of how many messages were
delivered, bounced, deferred, etc.  You need the HTMLGen python
module if you want it to be fancy.  Otherwise, just hack out the
HTML stuff at the bottom and format the contents of the "datapoints"
variable however you like...

Also, it's python, so don't expect it to be lightning fast if you have
100MB of logfiles.  :)

All I ask is that if the inevitable happens, and someone improves on it,
send me a copy, k?


-------- CUT HERE
#!/usr/bin/python

import string,time

# ---------- Change stuff in here to your liking

# Location of your logfile
LOGFILE='/var/log/maillog'
OUTPUTFILE=''

# Messages per day
FORMAT='%B %d'
CHARTTITLE='Number of messages per day'
# -OR-
# Messages per hour
#FORMAT='%B %d %H:00-%H:59'
#CHARTTITLE='Number of messages per hour'


PAGETITLE='Mail Stats'

# ---------- Shouldn't need to change anything below here

logfile = open(LOGFILE, 'r')
loglines = logfile.readlines()
logfile.close()

# Here's the Data set we're going to populate
DELIVERED=0
DEFERRED=1
BOUNCED=2
datapoints = {}

# Pull the data out of the mail log file
for line in loglines:
    fields = string.split(line)
    if fields[6] == 'delivery':
        tfields = string.split(fields[5], '.')
        ltime = time.localtime(int(tfields[0]))
        dp = time.strftime(FORMAT, ltime)
        dp = time.strftime(FORMAT, ltime)

        if not datapoints.has_key(dp):
            datapoints[dp] = [0,0,0]
        if fields[8] == 'success:':
            datapoints[dp][DELIVERED] = datapoints[dp][DELIVERED]+1
        elif fields[8] == 'deferral:':
            datapoints[dp][DEFERRED] = datapoints[dp][DEFERRED]+1
        elif fields[8] == 'failure:':
            datapoints[dp][BOUNCED] = datapoints[dp][BOUNCED]+1

# Okay, now make it pretty
from HTMLgen import *
import barchart

Document = BasicDocument(title=PAGETITLE,VLINK="White",ALINK="White",LINK="White")

tl = []
for n in datapoints.keys():
    s2 = (n, datapoints[n][DELIVERED],datapoints[n][DEFERRED],datapoints[n][BOUNCED])
    tl.append(s2)
dl = barchart.DataList()
dl.segment_names = ('DELIVERED','DEFERRED','BOUNCED')
dl.load_tuples(tl)
dl.sort()
ch = barchart.StackedBarChart(dl)
ch.title = CHARTTITLE
Document.append(ch)

Document.write(OUTPUTFILE)


-------- END OF ATTACHMENT


Reply via email to