]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding connect statistics to lib/pp_admintools/handler/pflogparse.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 26 Mar 2024 13:33:31 +0000 (14:33 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 26 Mar 2024 13:33:31 +0000 (14:33 +0100)
lib/pp_admintools/handler/pflogparse.py

index 6206b9aee68ba2f2ed53f30ec1b10b4e8a9f31bf..4942b13adf4ee6e206f64ec9ef9f3c50e39b31e4 100644 (file)
@@ -27,7 +27,7 @@ LOG = logging.getLogger(__name__)
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.3.1'
+__version__ = '0.3.2'
 
 
 # =============================================================================
@@ -67,6 +67,7 @@ class PostfixLogfileParser(HandlingObject):
         """Initialize the PostfixLogfileParser object."""
         self.chain = {}
         self.active_smtpd_pid = {}
+        self.connect = {}
 
         super(PostfixLogfileParser, self).__init__(
             appname=appname, verbose=verbose, version=version, base_dir=base_dir,
@@ -90,14 +91,18 @@ class PostfixLogfileParser(HandlingObject):
 
         self.chain = {}
         self.active_smtpd_pid = {}
+        self.connect = {}
 
     # -------------------------------------------------------------------------
     def parse(self, fh):
         """Parse the content of a given logfile and store the results in self.chain."""
         LOG.debug(_('Start parsing postfix logfile ...'))
 
-        for line in fh.readlines():
-            self.evaluate_logline(line)
+        try:
+            for line in fh.readlines():
+                self.evaluate_logline(line)
+        finally:
+            self.active_smtpd_pid = {}
 
     # -------------------------------------------------------------------------
     def evaluate_logline(self, line):
@@ -158,6 +163,10 @@ class PostfixLogfileParser(HandlingObject):
         m_conn = self.re_connect.search(message)
 
         if m_conn:
+            who = m_conn['client']
+            if who.lower() == 'unknown':
+                who = m_conn['addr']
+
             chain = PostfixLogchainInfo(
                 client_host=m_conn['client'],
                 client_addr=m_conn['addr'],
@@ -168,6 +177,10 @@ class PostfixLogfileParser(HandlingObject):
             )
             self.active_smtpd_pid[pid] = chain
 
+            if who not in self.connect:
+                self.connect[who] = 0
+            self.connect[who] += 1
+
             if self.verbose > 3:
                 msg = 'Creating Postfix log chain:\n' + pp(chain.as_dict())
                 LOG.debug(msg)