#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2009 Pascal Volk
__author__ = 'Pascal Volk'
__version__ = '0.1.1'
__date__ = '2009-06-08'
import os
import re
class NiXSapmSum:
"""Do sth ..."""
"""Regular expression pattern for mail logs from Postfix"""
RE_PF = '''^[\w\s:-]{17,80}\spostfix\/smtpd\[[\d]{3,5}\]: NOQUEUE: reject:.*blocked using ix.dnsbl.manitu.net; Spam sent to the mailhost ((?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}) was detected by NiX Spam.*$'''
def __init__(self):
self._doms = {}
self._mxs = {}
self._repo = None
def setLogFormat(self, format='postfix'):
if format == 'postfix':
self._repo = re.compile(NiXSapmSum.RE_PF)
else:
raise Exception('MTA/Logformat not supported yet.')
def parseLog(self, filehandle):
for l in filehandle:
mo = self._repo.match(l)
if mo:
mx = mo.group(1)
try:
self._mxs[mx] += 1
except KeyError:
self._mxs[mx] = 1
def countByDom(self):
for mx in self._mxs.keys():
dom = '.'.join(mx.split('.')[-2:])
try:
self._doms[dom] += self._mxs[mx]
except KeyError:
self._doms[dom] = self._mxs[mx]
def getDomains(self):
return self._doms
def getMXs(self):
return self._mxs
def getOptionParser():
from optparse import OptionParser
description = 'do something ...'
usage = 'usage: %prog [options] maillog [maillog [...]]'
version = '%prog '+__version__
parser = OptionParser(description=description,usage=usage,version=version)
parser.add_option('-d', action='store_true', dest='countByDom',
default=False, help='summarize all MX by domain')
parser.add_option('-m', action='store_false', dest='countByDom',
help='count per MX host [default]')
parser.add_option('-o', dest='oFormat', default='table',metavar='FORMAT',
help='the output format: table or csv [default: %default]')
parser.add_option('-p', action='store_true', dest='percent', default=False,
help='show also percentages in table output [default: %default]')
parser.add_option('-s', dest='order', default='name', metavar='SORTBY',
help='arrange output by: name or count [default: %default]')
parser.add_option('-t', dest='format', default='postfix',metavar='MTA',
help='MTA that generated the maillog [default: %default]')
return parser
def openLogFile(fname):
try:
fh = open(fname)
return fh
except IOError, e:
os.sys.stderr.write('Warning: %s\nskipped file: %s\n' % (e.strerror,
fname))
def getDomLen(domainnames):
dlen = 0
for d in domainnames:
l = len(d)
if l > dlen:
dlen = l
return dlen
def buildTable(output, domains, percent, orderBy):
k = 0 if orderBy == 'name' else 1
doms = sorted(domains.items(), lambda d,c: cmp(d[k],c[k]), reverse=k)
dlen = getDomLen(domains.keys())+1
clen = len(str(max(domains.values())))
total = sum(domains.values())
if percent:
format = ' %%%ds %%%dd %%6.2f %%%%\n' % (-dlen, clen)
for d, c in doms:
dfrac = 100./total*c
output.write(format % (d, c, dfrac))
output.write('%s\n' % ((clen+dlen+14)*'-'))
output.write(format % ('total', total, 100))
else:
format = ' %%%ds %%%dd\n' % (-dlen, clen)
for d in doms:
output.write(format % d)
output.write('%s\n' % ((clen+dlen+4)*'-'))
output.write(format % ('total', total))
def showResult(nixspamsum, options):
from cStringIO import StringIO
output = StringIO()
if options.countByDom:
nixspamsum.countByDom()
domains = nixspamsum.getDomains()
else:
domains = nixspamsum.getMXs()
# build the table
if options.oFormat == 'table':
buildTable(output, domains, options.percent, options.order)
# generate comma separated values
elif options.oFormat == 'csv':
for d in domains.items():
output.write("'%s',%d\n" % d)
# should never be reached
else:
print "Oops, error in function showResult() happend"
# show the result
print output.getvalue()
def main():
parser = getOptionParser()
opts, args = parser.parse_args()
if opts.oFormat not in ('csv', 'table'):
parser.error("Output format '%s' is not supported" % opts.oFormat)
if len(args) < 1:
parser.error('No logfiles specified')
nixss = NiXSapmSum()
nixss.setLogFormat(opts.format)
for fn in args:
fh = openLogFile(fn)
if fh is not None:
nixss.parseLog(fh)
fh.close()
showResult(nixss, opts)
if __name__ == '__main__':
main()