From cc1ecc6ddc7eb9570eac3bd128a187e0f8259ad6 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 20 Mar 2024 18:45:11 +0100 Subject: [PATCH] Start parsing --- lib/pp_admintools/handler/pflogparse.py | 66 +++++++++++++++++++++++-- lib/pp_admintools/postfix_chain.py | 8 ++- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/lib/pp_admintools/handler/pflogparse.py b/lib/pp_admintools/handler/pflogparse.py index afe7374..7cc6613 100644 --- a/lib/pp_admintools/handler/pflogparse.py +++ b/lib/pp_admintools/handler/pflogparse.py @@ -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__': diff --git a/lib/pp_admintools/postfix_chain.py b/lib/pp_admintools/postfix_chain.py index 87036c9..7832226 100644 --- a/lib/pp_admintools/postfix_chain.py +++ b/lib/pp_admintools/postfix_chain.py @@ -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, -- 2.39.5