logparse / parsers / postfix.pyon commit add more docstrings (ab93877)
   1#
   2#   postfix.py
   3#   
   4#   Get message statistics from postfix/sendmail logs
   5#
   6
   7import re
   8
   9from logparse.formatting import *
  10from logparse.util import readlog
  11from logparse import config
  12from logparse.load_parsers import Parser
  13
  14class Postfix(Parser):
  15
  16    def __init__(self):
  17        super().__init__()
  18        self.name = "postfix"
  19        self.info = "Get message statistics from postfix/sendmail logs"
  20
  21    def parse_log(self):
  22        section = Section("postfix")
  23        logger.debug("Starting postfix section")
  24        logger.debug("Searching through postfix logs")
  25        messages = re.findall('.*from\=<(.*)>, size\=(\d*),.*\n.*to=<(.*)>', readlog(config.prefs.get("logs", "postfix")))
  26        r = []
  27        s = []
  28        size = 0
  29        logger.debug("Analysing message size")
  30        for message in messages:
  31            r.append(message[2])
  32            s.append(message[0])
  33            size += int(message[1])
  34        # size = sum([int(x) for x in messages])
  35        size = parsesize(size)
  36        n = str(len(messages))
  37
  38        logger.debug("Analysing message recipients")
  39        if (len(r) > 0):
  40            rec_data = Data()
  41            s = list(set(r))    # unique recipients
  42            if (len(s) > 1):
  43                rec_data.items = r
  44                rec_data.orderbyfreq()
  45                rec_data.truncl(config.prefs.getint("logparse", "maxlist"))
  46                rec_data.subtitle = n + " messages sent to"
  47            else:
  48                rec_data.subtitle = n + " messages sent to " + r[0]
  49            section.append_data(rec_data)
  50        else:
  51            section.append_data(Data(subtitle=n + " messages sent"))
  52        logger.info("Found {0} messages sent to {1} recipients".format(n, str(len(r))))
  53        section.append_data(Data(subtitle="Total of " + size))
  54        logger.info("Finished postfix section")
  55        return section