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 rec_data.subtitle = n + " messages sent to"
42 else:
43 rec_data.subtitle = n + " messages sent to " + r[0]
44 section.append_data(rec_data)
45 else:
46 section.append_data(Data(subtitle=n + " messages sent"))
47 logger.info("Found {0} messages sent to {1} recipients".format(n, str(len(r))))
48 section.append_data(Data(subtitle="Total of " + size))
49 logger.info("Finished postfix section")
50 return section