-#
-# postfix.py
-#
-# Get message statistics from postfix/sendmail logs
-#
+"""
+Get message statistics from postfix/sendmail logs
+"""
import re
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():
- 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))
+ 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):
- 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"
+ 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:
- rec_data.subtitle = n + " messages sent to " + r[0]
- section.append_data(rec_data)
- else:
- 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
+ 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