from fb_tools.common import pp
from fb_tools.mailaddress import MailAddress
+import yaml
+
# Own modules
from . import BaseDPXApplication
from ..argparse_actions import NonNegativeIntegerOptionAction
from ..argparse_actions import OutputFileOptionAction
+from ..errors import DpxFileError
from ..handler.pflogparse import PostfixLogfileParser
from ..xlate import XLATOR
-__version__ = '0.7.0'
+__version__ = '0.8.0'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
re_mail_domain = re.compile(r'.*@')
+ open_args = {
+ 'encoding': 'utf-8',
+ 'errors': 'surrogateescape',
+ }
+
local_networks = [
ipaddress.ip_network('10.0.0.0/8'),
ipaddress.ip_network('172.16.0.0/12'),
self.empty_line()
LOG.debug(_('Results per address:') + '\n' + pp(self.per_address))
+ self._generate_totals()
+
if self.oldest_entry or self.newest_entry:
self.empty_line()
if self.oldest_entry:
self._output_results_per_address()
self._output_results_per_domain()
+ # -------------------------------------------------------------------------
+ def _generate_totals(self):
+
+ totals_file_abs = self.totals_yaml_file.resolve()
+ if self.verbose > 0:
+ LOG.debug(_('Using totals YAML file {!r}.').format(str(totals_file_abs)))
+
+ totals = self._read_totals(totals_file_abs)
+
+ for loghost in self.per_address.keys():
+ if loghost not in totals:
+ totals[loghost] = {}
+
+ totals[loghost]['per_address'] = {}
+ for address in self.per_address[loghost].keys():
+ count = self.per_address[loghost][address]
+ totals[loghost]['per_address'][address] = count
+
+ for loghost in self.per_address.keys():
+ if loghost not in totals:
+ totals[loghost] = {}
+
+ totals[loghost]['per_domain'] = {}
+ for domain in self.per_domain[loghost].keys():
+ count = self.per_domain[loghost][domain]
+ totals[loghost]['per_domain'][domain] = count
+
+ if self.verbose > 0:
+ self.empty_line()
+ LOG.debug(_('Results total:') + '\n' + pp(totals))
+
+ LOG.debug(_('Writing file {!r} ...').format(str(totals_file_abs)))
+ with totals_file_abs.open('wt', **self.open_args) as fh:
+ yaml.safe_dump(totals, fh, explicit_start=True)
+
+ # -------------------------------------------------------------------------
+ def _read_totals(self, totals_file_abs):
+
+ totals = {}
+ try:
+ self.check_output_file(totals_file_abs)
+ except DpxFileError as e:
+ LOG.error(str(e))
+ self.exit(3)
+
+ if totals_file_abs.exists():
+ if self.verbose > 0:
+ LOG.debug(_('Reading file {!r} ...').format(str(totals_file_abs)))
+ with totals_file_abs.open('rt', **self.open_args) as fh:
+ totals = yaml.safe_load(fh)
+
+ return totals
+
# -------------------------------------------------------------------------
def _output_results_per_address(self):
print(self.colored(title, 'CYAN'))
self.line(width=len(title), linechar='-', color='CYAN')
+ if not self.per_address:
+ print('None.')
+ return
+
for loghost in self.per_address.keys():
if len(loghost) > max_len_loghost:
print(self.colored(title, 'CYAN'))
self.line(width=len(title), linechar='-', color='CYAN')
+ if not self.per_domain:
+ print('None.')
+ return
+
for loghost in self.per_domain.keys():
if len(loghost) > max_len_loghost: