1#
2# postfix.py
3#
4# Get message statistics from postfix/sendmail logs
5#
6
7import re
8
9from ..formatting import *
10from ..util import readlog, resolve
11from .. import config
12
13import logging
14logger = logging.getLogger(__name__)
15
16def parse_log():
17 output = ''
18 logger.debug("Starting postfix section")
19 output += opentag('div', 1, 'postfix', 'section')
20 output += writetitle("postfix")
21 logger.debug("Searching through postfix logs")
22 messages = re.findall('.*from\=<(.*)>, size\=(\d*),.*\n.*to=<(.*)>', readlog(config.prefs['logs']['postfix']))
23 r = []
24 s = []
25 size = 0
26 logger.debug("Analysing message size")
27 for message in messages:
28 r.append(message[2])
29 s.append(message[0])
30 size += int(message[1])
31 # size = sum([int(x) for x in messages])
32 size = parsesize(size)
33 n = str(len(messages))
34
35 logger.debug("Analysing message recipients")
36 if (len(r) > 0):
37 s = list(set(r)) # unique recipients
38 if (len(s) > 1):
39 r = orderbyfreq(r)
40 r = truncl(r, config.prefs['maxlist'])
41 output += writedata(n + " messages sent to", r)
42 else:
43 output += writedata(n + " messages sent to " + r[0])
44 else:
45 output += writedata(n + " messages sent")
46 logger.info("Found {0} messages sent to {1} recipients".format(n, str(len(r))))
47 output += writedata("total of " + size)
48 output += closetag('div', 1)
49 logger.info("Finished postfix section")
50 return output