From 6dd8a04a402bf10e4f32dbdee0e25d866c3204dd Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Wed, 6 Mar 2019 08:11:52 -0800 Subject: [PATCH] Added banned IPs count to digest. --- bin/fail2ban_digest | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bin/fail2ban_digest b/bin/fail2ban_digest index 136b597..00dba70 100755 --- a/bin/fail2ban_digest +++ b/bin/fail2ban_digest @@ -37,7 +37,7 @@ db_location = '/var/lib/fail2ban/digest' db_creation_date_key = 'db_creation_date' db_date_format = '%Y-%m-%d %H:%M:%S' default_mail_template = Template('''Hi,\n -This is a digest email of banned IPs since ${creation_date} and ${date_now}: +This is a digest email of the ${count} banned IPs between ${creation_date} and ${date_now}: ${digest} @@ -79,7 +79,7 @@ default_html_template = Template('''

Hi,

-

This is a digest email of banned IPs since ${creation_date} and ${date_now}:

+

This is a digest email of the ${count} banned IPs between ${creation_date} and ${date_now}:

@@ -219,12 +219,12 @@ def digest(db, delete, sort): for ban in events_list: msg_html += html_tr_template.substitute(count = len(ban.events), ip = ban.ip, events = '
'.join(ban.events)) msg += '%3d event(s) for IP %-42s: %s\n' %(len(ban.events), ban.ip, ', '.join(ban.events)) - return (db_creation_date, msg, msg_html) + return (len(events_list), db_creation_date, msg, msg_html) def mail_digest(db, mail_to, mail_from, delete, html, quiet, sort): msg = EmailMessage() date_now = datetime.now().strftime(db_date_format) - creation_date, dgst, dgst_html = digest(db, delete, sort) + count, creation_date, dgst, dgst_html = digest(db, delete, sort) if dgst == '': if quiet: return @@ -232,12 +232,14 @@ def mail_digest(db, mail_to, mail_from, delete, html, quiet, sort): dgst = ' No ban event recorded for the named time frame.' dgst_html = html_error_template.substitute(error_msg = dgst) msg.set_content(default_mail_template.substitute( + count = count, creation_date = creation_date, date_now = date_now, digest = dgst )) if html: msg.add_alternative(default_html_template.substitute( + count = count, creation_date = creation_date, date_now = date_now, digest = dgst_html @@ -254,7 +256,7 @@ def main(args): if args.cmd == 'add': add(args.database, args.ip) elif args.cmd == 'digest': - print(digest(args.database, args.delete, args.sort)[1]) + print(digest(args.database, args.delete, args.sort)[2]) elif args.cmd == 'maildigest': mail_digest(args.database, args.to, args.mail_from, args.delete, args.html, args.quiet, args.sort) elif args.cmd is None:
#