]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Start parsing
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 20 Mar 2024 17:45:11 +0000 (18:45 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 20 Mar 2024 17:45:11 +0000 (18:45 +0100)
lib/pp_admintools/handler/pflogparse.py
lib/pp_admintools/postfix_chain.py

index afe7374283231758273dd42b112c4fc65d11a8e6..7cc6613561d08bd5266260d520a6a411370f7962 100644 (file)
@@ -9,17 +9,17 @@
 from __future__ import absolute_import
 
 # Standard modules
+import copy
 import logging
 import re
 
 # Third party modules
 
-# from fb_tools.common import pp
-# from fb_tools.common import to_str
+from fb_tools.common import pp
 from fb_tools.handling_obj import HandlingObject
 
 # Own modules
-from ..postfix_chain import PostfixLogchainInfo
+from ..postfix_chain import PostfixLogchainInfo
 from ..xlate import XLATOR
 
 LOG = logging.getLogger(__name__)
@@ -27,7 +27,7 @@ LOG = logging.getLogger(__name__)
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.1.0'
+__version__ = '0.2.0'
 
 
 # =============================================================================
@@ -91,6 +91,64 @@ class PostfixLogfileParser(HandlingObject):
         self.chains = {}
         self.active_smtpd_pid = {}
 
+    # -------------------------------------------------------------------------
+    def parse(self, fh, mailhost=None):
+        """Parse the content of a given logfile and store the results in self.chains."""
+        if mailhost:
+            msg = _('Start parsing postfix logfile from {!r} ...').format(mailhost)
+        else:
+            msg = _('Start parsing postfix logfile ...')
+        LOG.debug(msg)
+
+        for line in fh.readlines():
+            self.evaluate_logline(line, mailhost)
+
+    # -------------------------------------------------------------------------
+    def evaluate_logline(self, line, mailhost):
+        """Parse a single line from a postfix logfile."""
+        m = self.re_logentry.match(line)
+        if not m:
+            return
+
+        timestamp = m['timestamp']
+        command = m['command']
+        pid = int(m['pid'])
+        message = m['msg']
+        postfix_id = None
+
+        m_id = self.re_postfix_id.match(message)
+        if m_id:
+            postfix_id = m_id['id'].upper()
+            message = self.re_postfix_id.sub('', message)
+
+        if self.verbose > 3:
+            entry = {
+                'timestamp': timestamp,
+                'command': command,
+                'pid': pid,
+                'postfix_id': postfix_id,
+                'message': message,
+            }
+            LOG.debug('Found log entry:\n' + pp(entry))
+
+        if command == 'postfix/smtpd' and pid:
+            m_conn = self.re_connect.search(message)
+            if m_conn:
+                chain = PostfixLogchainInfo(
+                    client_host= m_conn['client'],
+                    client_addr=m_conn['addr'],
+                    start=timestamp,
+                    postfix_id=postfix_id,
+                    smtpd_pid=pid,
+                    mailhost=mailhost,
+                )
+                self.active_smtpd_pid[pid] = chain
+
+                if self.verbose > 3:
+                    msg = 'Creating Postfix log chain:\n' + pp(chain.as_dict())
+                    LOG.debug(msg)
+                return
+
 
 # =============================================================================
 if __name__ == '__main__':
index 87036c9bf231f248946f9d949ea95a8acf827e22..78322264df06390f2b6a34814e37625ca29cef82 100644 (file)
@@ -28,7 +28,7 @@ from .xlate import XLATOR
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.5.0'
+__version__ = '0.5.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -111,6 +111,12 @@ class PostfixLogchainInfo(FbGenericBaseObject):
 
     warn_on_parse_error = True
 
+    attributes = (
+        'client_host', 'client_addr', 'start', 'end', 'message_id', 'postfix_id', 'ehlo',
+        'starttls', 'sent_quit', 'auth', 'commands', 'rcpt', 'data', 'mail', 'from_address',
+        'to_address', 'smtpd_pid', 'mailhost',
+    )
+
     # -------------------------------------------------------------------------
     def __init__(
             self, client_host=None, client_addr=None, start=None, end=None, message_id=None,