From 67f414975839953fd30f2bb9c94f66fbe770d671 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 12 Apr 2024 14:09:25 +0200 Subject: [PATCH] Implementing collecting results in bin/get-intern-used-from-addresses --- lib/pp_admintools/app/get_from_addr.py | 89 ++++++++++++++++++++------ 1 file changed, 70 insertions(+), 19 deletions(-) diff --git a/lib/pp_admintools/app/get_from_addr.py b/lib/pp_admintools/app/get_from_addr.py index ef1fdd5..91b4512 100644 --- a/lib/pp_admintools/app/get_from_addr.py +++ b/lib/pp_admintools/app/get_from_addr.py @@ -23,7 +23,7 @@ from . import BaseDPXApplication from ..handler.pflogparse import PostfixLogfileParser from ..xlate import XLATOR -__version__ = '0.4.1' +__version__ = '0.5.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -197,7 +197,7 @@ class GetFromAddressesApp(BaseDPXApplication): def _collect_results(self): self.empty_line() - self.debug(_('Collecting results ...')) + LOG.debug(_('Collecting results ...')) for postfix_id in self.pflogparser.chain: @@ -222,28 +222,75 @@ class GetFromAddressesApp(BaseDPXApplication): continue # Client address is not an ipaddress - if isinstance(chain.client_addr, ipaddress._BaseAddress): + if chain.client_addr: + if not isinstance(chain.client_addr, ipaddress._BaseAddress): + continue + + if not self._is_local_client(chain): + # Not from our network originating continue - local_client = False - if chain.client_addr: - for net in self.local_networks: - if chain.client_addr in net: - local_client = True - break - - if local_client: - for except_src in self.except_sources: - if chain.client_addr == except_src: - local_client = False - else: - # from pickup ... - local_client = True + if not self._has_extern_rcpt(chain): + # remaining in our network + continue + + loghost = chain.host + + if loghost not in self.per_domain: + self.per_domain[loghost] = {} + + if domain not in self.per_domain[loghost]: + self.per_domain[loghost][domain] = 0 + + self.per_domain[loghost][domain] += 1 + + # ------------------------------------------------------------------------- + def _is_local_client(self, chain): + + if not chain.client_addr: + # from pickup ... + return True + + is_local_client = False + for net in self.local_networks: + if chain.client_addr in net: + is_local_client = True - # Not from our network originating - if not local_client: + if not is_local_client: + return False + + for except_src in self.except_sources: + if chain.client_addr == except_src: + is_local_client = False + break + + return is_local_client + + # ------------------------------------------------------------------------- + def _has_extern_rcpt(self, chain): + + has_extern_rcpt = False + + for action in chain.deliver_actions: + if action.command != 'smtp': continue + relay_address = action.relay_address + if not relay_address: + continue + + local_rcpt = False + for net in self.local_networks: + if relay_address in net: + local_rcpt = True + break + + if not local_rcpt: + has_extern_rcpt = True + break + + return has_extern_rcpt + # ------------------------------------------------------------------------- def _run(self): @@ -263,6 +310,10 @@ class GetFromAddressesApp(BaseDPXApplication): self.pflogparser.evaluate_logfiles(self.logfiles) + self._collect_results() + if self.verbose > 1: + LOG.debug(_('Results per domain:') + '\n' + pp(self.per_domain)) + # ============================================================================= if __name__ == '__main__': -- 2.39.5