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__)
_ = XLATOR.gettext
ngettext = XLATOR.ngettext
-__version__ = '0.1.0'
+__version__ = '0.2.0'
# =============================================================================
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__':
_ = XLATOR.gettext
ngettext = XLATOR.ngettext
-__version__ = '0.5.0'
+__version__ = '0.5.1'
LOG = logging.getLogger(__name__)
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,