]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding options for output files to class GetFromAddressesApp
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 15 Apr 2024 14:19:40 +0000 (16:19 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 15 Apr 2024 14:19:40 +0000 (16:19 +0200)
lib/pp_admintools/app/get_from_addr.py

index 8b92b0b98542a9d4ed16615209fc1ebca3ec11f9..6313692576779ee03e476fcc07bf11c4e3de928d 100644 (file)
@@ -20,10 +20,12 @@ from fb_tools.mailaddress import MailAddress
 
 # Own modules
 from . import BaseDPXApplication
+from ..argparse_actions import NonNegativeIntegerOptionAction
+from ..argparse_actions import OutputFileOptionAction
 from ..handler.pflogparse import PostfixLogfileParser
 from ..xlate import XLATOR
 
-__version__ = '0.6.1'
+__version__ = '0.7.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -41,6 +43,12 @@ class GetFromAddressesApp(BaseDPXApplication):
     default_logdir = Path('/var/log')
     default_logfile_pattern = 'maillog*'
 
+    default_limit = 10
+
+    default_totals_yaml_file = Path('get-from-addr-totals.yaml')
+    default_stats_per_address_csv_file = Path('get-from-addr-per-address-{loghost}.csv')
+    default_stats_per_domain_csv_file = Path('get-from-addr-per-domain-{loghost}.csv')
+
     re_mail_domain = re.compile(r'.*@')
 
     local_networks = [
@@ -70,11 +78,19 @@ class GetFromAddressesApp(BaseDPXApplication):
 
         self.logdir = self.default_logdir
         self.logfile_pattern = self.default_logfile_pattern
+        self.limit = self.default_limit
         self.logfiles = []
 
         self.per_domain = {}
         self.per_address = {}
 
+        self.totals_yaml_file = self.default_totals_yaml_file
+        self.stats_per_address_csv_file = self.default_stats_per_address_csv_file
+        self.stats_per_domain_csv_file = self.default_stats_per_domain_csv_file
+
+        self.oldest_entry = None
+        self.newest_entry = None
+
         self.show_address_results = False
 
         desc1 = _(
@@ -102,6 +118,42 @@ class GetFromAddressesApp(BaseDPXApplication):
             help=_('Show also results for particular mail addresses.'),
         )
 
+        app_group.add_argument(
+            '-L', '--limit', type=int, metavar=_('COUNT'), dest='limit',
+            action=NonNegativeIntegerOptionAction,
+            help=_(
+                'Limit the output of domains and addresses to those with a count of '
+                'at least this value. Default: {}').format(self.limit)
+        )
+
+        app_group.add_argument(
+            '--totals-yaml-file', metavar=_('FILE'), dest='totals_yaml_file',
+            action=OutputFileOptionAction,
+            help=_(
+                'The output YAML file for collecting and cummulating the results. '
+                'Dafault: {!r}.').format(str(self.totals_yaml_file))
+        )
+
+        app_group.add_argument(
+            '--address-csv-file', metavar=_('FILE'), dest='address_csv_file',
+            action=OutputFileOptionAction,
+            help=_(
+                'The output CSV file about found FROM addresses for importing in a spread sheet '
+                'or into a database. The file name may have the tag {{loghost}}, which will be '
+                'substituted with the hostname of the mailserver. Default: {!r}').format(
+                str(self.stats_per_address_csv_file))
+        )
+
+        app_group.add_argument(
+            '--domain-csv-file', metavar=_('FILE'), dest='domain_csv_file',
+            action=OutputFileOptionAction,
+            help=_(
+                'The output CSV file about the domains of found FROM addresses for importing '
+                'in a spread sheet or into a database. The file name may have the tag '
+                '{{loghost}}, which will be substituted with the hostname of the mailserver. '
+                'Default: {!r}').format(str(self.stats_per_domain_csv_file))
+        )
+
         logfiles_default = str(self.logdir / self.logfile_pattern)
         app_group.add_argument(
             'logfiles', nargs='*', metavar=_('FILE'),
@@ -124,6 +176,18 @@ class GetFromAddressesApp(BaseDPXApplication):
         self.pflogparser.terminal_has_colors = self.terminal_has_colors
         self.pflogparser.initialized = True
 
+        totals_yaml_file = getattr(self.args, 'totals_yaml_file', None)
+        if totals_yaml_file:
+            self.totals_yaml_file = totals_yaml_file
+
+        address_csv_file = getattr(self.args, 'address_csv_file', None)
+        if address_csv_file:
+            self.stats_per_address_csv_file = address_csv_file
+
+        domain_csv_file = getattr(self.args, 'domain_csv_file', None)
+        if domain_csv_file:
+            self.stats_per_domain_csv_file = domain_csv_file
+
         if getattr(self.args, 'show_address_results', False):
             self.show_address_results = True
 
@@ -204,6 +268,11 @@ class GetFromAddressesApp(BaseDPXApplication):
 
             chain = self.pflogparser.chain[postfix_id]
 
+            if self.oldest_entry is None or chain.start < self.oldest_entry:
+                self.oldest_entry = chain.start
+            if self.newest_entry is None or chain.start > self.newest_entry:
+                self.newest_entry = chain.start
+
             # Did not come from smtpd or pickup
             if not chain.client_addr and not chain.pickup_from:
                 continue
@@ -323,6 +392,17 @@ class GetFromAddressesApp(BaseDPXApplication):
             self.empty_line()
             LOG.debug(_('Results per address:') + '\n' + pp(self.per_address))
 
+        if self.oldest_entry or self.newest_entry:
+            self.empty_line()
+        if self.oldest_entry:
+            msg = _('Start of oldest found Postfix transaction:') + ' '
+            msg += self.oldest_entry.isoformat(' ', 'seconds')
+            print(msg)
+        if self.newest_entry:
+            msg = _('Start of newest found Postfix transaction:') + ' '
+            msg += self.newest_entry.isoformat(' ', 'seconds')
+            print(msg)
+
         if self.show_address_results:
             self._output_results_per_address()
         self._output_results_per_domain()