Skip to content

Instantly share code, notes, and snippets.

@danielrichman
Last active December 15, 2015 21:09
Show Gist options
  • Save danielrichman/5323661 to your computer and use it in GitHub Desktop.
Save danielrichman/5323661 to your computer and use it in GitHub Desktop.
Summarise exim4 email failures
#!/usr/bin/python
import glob
import gzip
import datetime
logfiles = glob.glob("/var/log/exim4/mainlog*")
time_format = "%Y-%m-%d %H:%M:%S"
def all_lines():
for filename in logfiles:
if ".gz" in filename:
f = gzip.open(filename)
else:
f = open(filename)
for line in f:
yield line
f.close()
days = {}
for line in all_lines():
split = line.split()
when = " ".join(split[0:2])
if len(split) > 3 and split[3] == '**':
day = datetime.datetime.strptime(when, time_format).date()
if day not in days:
days[day] = {}
failures = days[day]
local_part, domain = split[4].split("@")
if domain not in failures:
failures[domain] = 0
failures[domain] += 1
print "Outgoing email hard failure summary"
print
for day, failures in sorted(days.items()):
print day
for domain, times in sorted(failures.items(), key=lambda (d, t): -t):
print " ", str(times).rjust(5), domain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment