]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Performing evaluation of postfix logfiles
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 Apr 2024 15:02:21 +0000 (17:02 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 Apr 2024 15:02:21 +0000 (17:02 +0200)
lib/pp_admintools/app/get_from_addr.py
lib/pp_admintools/handler/pflogparse.py

index 0205c6469cc746db068cb68f2ef3aa0d6f09886f..c15f279615cd61f434f80700419321310eefc9a6 100644 (file)
@@ -22,7 +22,7 @@ from . import BaseDPXApplication
 from ..handler.pflogparse import PostfixLogfileParser
 from ..xlate import XLATOR
 
-__version__ = '0.2.2'
+__version__ = '0.3.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -187,8 +187,20 @@ class GetFromAddressesApp(BaseDPXApplication):
     def _run(self):
 
         self.empty_line()
-        print(self.colored('On the run ...', 'CYAN'))
-        self.empty_line()
+
+        if not self.logfiles:
+            LOG.warn(_('No logfiles for evaluating found.'))
+            self.empty_line()
+            self.exit(1)
+            return
+
+        title = _('Evaluating Postfix log files:')
+        print()
+        print(self.colored(title, 'CYAN'))
+        self.line(width=len(title), linechar='=', color='CYAN')
+        print()
+
+        self.pflogparser.evaluate_logfiles(self.logfiles)
 
 
 # =============================================================================
index b01bcd61f131a74e7eac356cfbde9afb90a47247..d81fdf1a3b2fad3a2c2463e42bf3d89786eacd24 100644 (file)
@@ -10,11 +10,17 @@ from __future__ import absolute_import
 
 # Standard modules
 # import copy
+import bz2
+import gzip
 import logging
+import lzma
+import os
 import re
+from pathlib import Path
 
 # Third party modules
 
+from fb_tools.common import is_sequence
 from fb_tools.common import pp
 from fb_tools.handling_obj import HandlingObject
 
@@ -28,7 +34,7 @@ LOG = logging.getLogger(__name__)
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.5.5'
+__version__ = '0.6.0'
 
 
 # =============================================================================
@@ -81,6 +87,11 @@ class PostfixLogfileParser(HandlingObject):
         'postfix/pipe', 'postfix/qmgr', 'postfix/smtp'
     )
 
+    open_args = {
+        'encoding': 'utf-8',
+        'errors': 'surrogateescape',
+    }
+
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
@@ -119,16 +130,75 @@ class PostfixLogfileParser(HandlingObject):
         self.active_smtpd_pid = {}
         self.connect = {}
 
+    # -------------------------------------------------------------------------
+    def evaluate_logfiles(self, logfiles):
+        """Evaluate and parse all given logfiles."""
+        if not is_sequence(logfiles):
+            msg = _('Given parameter {p!r} for {f}() is not a sequential objects: {v!r}.').format(
+                p='logfiles', f='evaluate_logfiles', v=logfiles)
+            raise TypeError(msg)
+
+        self.reset()
+
+        for logfile in logfiles:
+            self.evaluate_logfile(logfile)
+
+    # -------------------------------------------------------------------------
+    def evaluate_logfile(self, logfile):
+        """Evaluate the log entries from the given logfile."""
+        if not isinstance(logfile, Path):
+            msg = _('Parameter {p!r} for {f}() is not a {what} object: {v!r}.').format(
+                p='logfile', f='evaluate_logfile', what='Path', v=logfile)
+            raise TypeError(msg)
+
+        if not logfile.exists():
+            LOG.error(_('Logfile {!r} does not exists.').format(str(logfile)))
+            return
+
+        if not logfile.is_file():
+            LOG.error(_('Logfile {!r} is not a regular file.').format(str(logfile)))
+            return
+
+        if not os.access(logfile, os.R_OK):
+            LOG.error(_('No read access to logfile {!r}.').format(str(logfile)))
+            return
+
+        LOG.info(_('Evaluating logfile {!r} ...').format(str(logfile)))
+
+        try:
+            if logfile.name.endswith('.gz'):
+                LOG.debug(_('Reading file {f!r} as a {what} compressed file.').format(
+                    f=str(logfile), what='gzip'))
+                fh = gzip.open(str(logfile), 'rt', **self.open_args)
+
+            elif logfile.name.endswith('.xz'):
+                LOG.debug(_('Reading file {f!r} as a {what} compressed file.').format(
+                    f=str(logfile), what='xz/lzma'))
+                fh = lzma.open(str(logfile), 'rt', **self.open_args)
+
+            elif logfile.name.endswith('.bz2') or logfile.name.endswith('.bzip2'):
+                LOG.debug(_('Reading file {f!r} as a {what} compressed file.').format(
+                    f=str(logfile), what='bzip2'))
+                fh = bz2.open(str(logfile), 'rt', **self.open_args)
+
+            else:
+                LOG.debug(_('Reading file {!r} as an uncompressed file.').format(str(logfile)))
+                fh = logfile.open('rt', **self.open_args)
+
+#            self,parse(fh)
+
+        finally:
+            if fh:
+                LOG.debug(_('Closing file {!r}.').format(str(logfile)))
+                fh.close()
+
     # -------------------------------------------------------------------------
     def parse(self, fh):
         """Parse the content of a given logfile and store the results in self.chain."""
         LOG.debug(_('Start parsing postfix logfile ...'))
 
-        try:
-            for line in fh.readlines():
-                self.evaluate_logline(line)
-        finally:
-            self.active_smtpd_pid = {}
+        for line in fh.readlines():
+            self.evaluate_logline(line)
 
     # -------------------------------------------------------------------------
     def evaluate_logline(self, line):