#! /usr/bin/env python3 # Copyright (C) 2021 by the Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. """This script assumes the Mailman 3 smtp.log is at LOGPATH and the next older generation is at LOGPATH + '.1'. It reads those two logs and accumulates the total number of messages sent including administrivia by day and by list and prints a simple report. You may need to adjust LOGPATH. """ import re import datetime TSRE = '(?P[^(]*) \(\d+\)' LOGRE = (' (?P<[^>]*>) smtp to ' '(?P[^ ]*) for (?P\d+) recips, completed ') LOGPATH = '/opt/mailman/mm/var/logs/smtp.log' def get_log_data(log): data = [] with open(log) as fp: for line in fp.readlines(): mo = re.match(TSRE, line) if mo: dt = datetime.datetime.strptime(mo.group('time_stamp'), '%b %d %H:%M:%S %Y') mo = re.search(LOGRE, line) if not mo: continue mid = mo.group('msgid') lname = mo.group('listname') count = int(mo.group('count')) data.append([dt, mid, lname, count]) return data def main(): data = get_log_data(LOGPATH + '.1') + get_log_data(LOGPATH) days = dict() for dt, mid, lname, count in data: day = dt.strftime('%b %d') days.setdefault(day, []).append([lname, count]) for day in days.keys(): days[day].sort() lname = None tot = 0 print(day) for l, c in days[day]: if l != lname: if lname is not None: print(f' {lname}: {tot}') lname = l tot = c else: tot += c print(f' {lname}: {tot}') if __name__ == '__main__': main()