-#
-# postfix.py
-#
-# Get message statistics from postfix/sendmail logs
-#
+"""
+Get message statistics from postfix/sendmail logs
+"""
import re
-from ..formatting import *
-from ..util import readlog, resolve
-from .. import config
+from logparse.formatting import *
+from logparse.util import readlog
+from logparse import config
+from logparse.load_parsers import Parser
-import logging
-logger = logging.getLogger(__name__)
+class Postfix(Parser):
-def parse_log():
- output = ''
- logger.debug("Starting postfix section")
- output += opentag('div', 1, 'postfix', 'section')
- output += writetitle("postfix")
- logger.debug("Searching through postfix logs")
- messages = re.findall('.*from\=<(.*)>, size\=(\d*),.*\n.*to=<(.*)>', readlog(config.prefs['logs']['postfix']))
- r = []
- s = []
- size = 0
- logger.debug("Analysing message size")
- for message in messages:
- r.append(message[2])
- s.append(message[0])
- size += int(message[1])
- # size = sum([int(x) for x in messages])
- size = parsesize(size)
- n = str(len(messages))
+ def __init__(self):
+ super().__init__()
+ self.name = "postfix"
+ self.info = "Get message statistics from postfix/sendmail logs"
- logger.debug("Analysing message recipients")
- if (len(r) > 0):
- s = list(set(r)) # unique recipients
- if (len(s) > 1):
- r = orderbyfreq(r)
- r = truncl(r, config.prefs['maxlist'])
- output += writedata(n + " messages sent to", r)
+ def parse_log(self):
+ section = Section("postfix")
+ logger.debug("Starting postfix section")
+ logger.debug("Searching through postfix logs")
+ messages = re.findall('.*from\=<(.*)>, size\=(\d*),.*\n.*to=<(.*)>',
+ readlog(config.prefs.get("logs", "postfix")))
+ r = []
+ s = []
+ size = 0
+ logger.debug("Analysing message size")
+ for message in messages:
+ r.append(message[2])
+ s.append(message[0])
+ size += int(message[1])
+ # size = sum([int(x) for x in messages])
+ size = parsesize(size)
+ n = str(len(messages))
+
+ logger.debug("Analysing message recipients")
+ if (len(r) > 0):
+ rec_data = Data()
+ s = list(set(r)) # unique recipients
+ if (len(s) > 1):
+ rec_data.items = r
+ rec_data.orderbyfreq()
+ rec_data.truncl(config.prefs.getint("logparse", "maxlist"))
+ rec_data.subtitle = n + " messages sent to"
+ else:
+ rec_data.subtitle = n + " messages sent to " + r[0]
+ section.append_data(rec_data)
else:
- output += writedata(n + " messages sent to " + r[0])
- else:
- output += writedata(n + " messages sent")
- logger.info("Found {0} messages sent to {1} recipients".format(n, str(len(r))))
- output += writedata("total of " + size)
- output += closetag('div', 1)
- logger.info("Finished postfix section")
- return output
+ section.append_data(Data(subtitle=n + " messages sent"))
+ logger.info("Found {0} messages sent to {1} recipients".format(n, str(len(r))))
+ section.append_data(Data(subtitle="Total of " + size))
+ logger.info("Finished postfix section")
+ return section