bee1809edb1642df5df26cdf2733c6c41bf905d5
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 section = Section("postfix")
18 logger.debug("Starting postfix section")
19 logger.debug("Searching through postfix logs")
20 messages = re.findall('.*from\=<(.*)>, size\=(\d*),.*\n.*to=<(.*)>', readlog(config.prefs['logs']['postfix']))
21 r = []
22 s = []
23 size = 0
24 logger.debug("Analysing message size")
25 for message in messages:
26 r.append(message[2])
27 s.append(message[0])
28 size += int(message[1])
29 # size = sum([int(x) for x in messages])
30 size = parsesize(size)
31 n = str(len(messages))
32
33 logger.debug("Analysing message recipients")
34 if (len(r) > 0):
35 rec_data = Data()
36 s = list(set(r)) # unique recipients
37 if (len(s) > 1):
38 rec_data.items = r
39 rec_data.orderbyfreq()
40 rec_data.truncl(config.prefs['maxlist'])
41 else:
42 rec_data.subtitle = n + " messages sent to " + r[0]
43 section.append_data(rec_data)
44 else:
45 section.append_data(Data(subtitle=n + " messages sent")))
46 logger.info("Found {0} messages sent to {1} recipients".format(n, str(len(r))))
47 section.append_data(Data(subtitle="total of " + size))
48 logger.info("Finished postfix section")
49 return section