# 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
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 = [
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 = _(
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'),
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
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
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()