From: Frank Brehm Date: Thu, 28 Mar 2024 16:10:37 +0000 (+0100) Subject: Adding parsing smtp loglines X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=d1ef5fec7850e7db5682b564bf7e3a6043fb4e7a;p=pixelpark%2Fpp-admin-tools.git Adding parsing smtp loglines --- diff --git a/lib/pp_admintools/handler/pflogparse.py b/lib/pp_admintools/handler/pflogparse.py index fa28ee6..75e6790 100644 --- a/lib/pp_admintools/handler/pflogparse.py +++ b/lib/pp_admintools/handler/pflogparse.py @@ -20,6 +20,7 @@ from fb_tools.handling_obj import HandlingObject # Own modules from ..postfix_chain import PostfixLogchainInfo +from ..postfix_chain import SmtpAction from ..xlate import XLATOR LOG = logging.getLogger(__name__) @@ -27,7 +28,7 @@ LOG = logging.getLogger(__name__) _ = XLATOR.gettext ngettext = XLATOR.ngettext -__version__ = '0.4.0' +__version__ = '0.4.1' # ============================================================================= @@ -285,6 +286,12 @@ class PostfixLogfileParser(HandlingObject): 'into chain.') self.chain[postfix_id] = self.active_smtpd_pid[pid] + if command == 'postfix/smtp' and pid: + self.eval_smtp_action( + postfix_id=postfix_id, timestamp=timestamp, host=host, pid=pid, + message=message) + return + if postfix_id in self.chain: m = self.re_message_id.search(message) if m: @@ -301,6 +308,7 @@ class PostfixLogfileParser(HandlingObject): if message.lower() == 'removed' and postfix_id in self.chain: self.chain[postfix_id].end = timestamp + self.chain[postfix_id].finished = True return if smtpd_done: @@ -310,6 +318,19 @@ class PostfixLogfileParser(HandlingObject): msg = f'Evaluating further entry of {command!r} for Postfix Id {postfix_id!r}: ' LOG.debug(msg + message) + # ------------------------------------------------------------------------- + def eval_smtp_action( + self, postfix_id, timestamp=None, host=None, pid=None, message=None): + """Evaluate a postfix/smtp action log line.""" + if postfix_id not in self.chain: + LOG.warn(_('Postfix transaction {!r} does not exists.').format(postfix_id)) + return + + action = SmtpAction.from_log_entry( + timestamp=timestamp, pid=pid, message=message, verbose=self.verbose) + + self.chain[postfix_id].add_smtp_action(action) + # ========================================================================= if __name__ == '__main__': diff --git a/lib/pp_admintools/postfix_chain.py b/lib/pp_admintools/postfix_chain.py index dca8fc0..df54b57 100644 --- a/lib/pp_admintools/postfix_chain.py +++ b/lib/pp_admintools/postfix_chain.py @@ -18,6 +18,7 @@ from collections.abc import Sequence # Third party modules from fb_tools.common import pp +from fb_tools.common import to_bool from fb_tools.errors import InvalidMailAddressError from fb_tools.mailaddress import MailAddress from fb_tools.obj import FbGenericBaseObject @@ -31,7 +32,7 @@ from .xlate import XLATOR _ = XLATOR.gettext ngettext = XLATOR.ngettext -__version__ = '0.7.1' +__version__ = '0.7.2' LOG = logging.getLogger(__name__) @@ -749,7 +750,7 @@ class PostfixLogchainInfo(FbGenericBaseObject): attributes = ( 'client_host', 'client_addr', 'start', 'end', 'message_id', 'postfix_id', 'ehlo', 'starttls', 'sent_quit', 'auth', 'commands', 'rcpt', 'data', 'mail', 'from_address', - 'smtpd_pid', 'mailhost', 'tls_version', 'tls_cipher', 'size', 'nr_rcpt', + 'smtpd_pid', 'mailhost', 'tls_version', 'tls_cipher', 'size', 'nr_rcpt', 'finished', ) # ------------------------------------------------------------------------- @@ -759,6 +760,8 @@ class PostfixLogchainInfo(FbGenericBaseObject): priv_name = '_' + attr setattr(self, priv_name, None) + self._finished = False + self.smtp_actions = [] if smtp_actions: self.set_smtp_actions(smtp_actions) @@ -993,6 +996,20 @@ class PostfixLogchainInfo(FbGenericBaseObject): return self._end = val + # ----------------------------------------------------------- + @property + def finished(self): + """ + Return, whether the Postfix transaction was finished. + + It means also, taht the mail was removed from queue. + """ + return self._finished + + @finished.setter + def finished(self, value): + self._finished = to_bool(value) + # ----------------------------------------------------------- @property def from_address(self):