#!/usr/bin/python # Small statistic, how many mails where rejected by postfix, # and why (DNSBL, ...) import re import sys import getopt statfile="/var/log/mail.log" regex={ "connect": r'^connect from.*$', "DNSBL": r'^NOQUEUE: .*blocked using (.*?);.*$', "user unkown": r'.*reject:.*User unknown in virtual alias table;.*', "domain not found": r'NOQUEUE: .*Sender address rejected: Domain not found', "Relay access denied": r'.*Relay access denied.*', "Helo command rejected": r'NOQUEUE:.*Helo command rejected', "disconnect": r'^disconnect from', "lost connection": r'^lost connection after', "removed": r'^\w+: removed$', "client": r'^\w+: client=.*$', "from": r'^\w+: from=.*$', "message-id": r'^\w+: message-id=.*$', "to": r'^\w+: to=.*$', "hostname verification failed": r'^warning: smtpd_peer_init: .* hostname .* verification failed.*$', "Illegal address syntax": r'^warning: Illegal address syntax from .* in MAIL command.*$' } matches={} def main(): try: opts, args = getopt.getopt(sys.argv[1:], "d", ["debug"]) except getopt.GetoptError, e: print e usage() sys.exit(2) debug=False for o, a in opts: if o in ["-d", "--debug"]: debug=True else: raise("Unparsed arguments: %s %s" % (o, a)) if len(args)>1: usage() sys.exit(2) if args: global statfile statfile=args[0] fd=open(statfile) nomatch=0 noregex=0 for line in fd: line=line.strip() match=re.match( r'^(\w+)\s+(\d+)\s+\d+:\d+:\d+\s+\S+\s+postfix.*?:\s+(?P.+)$', line) if not match: nomatch+=1 if debug: print "nomatch", line continue msg=match.group("msg") for name, r in regex.items(): match=re.match(r, msg) if match: g=match.groups() if g: g=g[0] else: g="" old=matches.get(name, 0) old+=1 matches[name]=old break else: if debug: print "noregex", msg noregex+=1 items=[] for name, count in matches.items(): items.append((count, name)) items.sort() items.reverse() for count, name in items: print "% 4d %s" % (count, name) print "No match", nomatch print "No regex", noregex if __name__=="__main__": main()