send the mail_log.py report to the box admin every Monday

This commit is contained in:
Joshua Tauberer 2018-02-24 09:19:00 -05:00
parent 9c7820f422
commit 1eba7b0616
4 changed files with 38 additions and 12 deletions

View file

@ -4,6 +4,7 @@ CHANGELOG
In Development In Development
-------------- --------------
* A report of box activity, including sent/received mail totals and logins by user, is now emailed to the box's administrator user each week.
* Update Roundcube to version 1.3.4 and Z-Push to version 2.3.9. * Update Roundcube to version 1.3.4 and Z-Push to version 2.3.9.
* The undocumented feature for proxying web requests to another server now sets X-Forwarded-For. * The undocumented feature for proxying web requests to another server now sets X-Forwarded-For.

View file

@ -9,6 +9,12 @@ export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8 export LANG=en_US.UTF-8
export LC_TYPE=en_US.UTF-8 export LC_TYPE=en_US.UTF-8
# On Mondays, i.e. once a week, send the administrator a report of total emails
# sent and received so the admin might notice server abuse.
if [ `date "+%u"` -eq 1 ]; then
management/mail_log.py -t week | management/email_administrator.py "Mail-in-a-Box Usage Report"
fi
# Take a backup. # Take a backup.
management/backup.py | management/email_administrator.py "Backup Status" management/backup.py | management/email_administrator.py "Backup Status"

View file

@ -4,8 +4,14 @@
import sys import sys
import html
import smtplib import smtplib
from email.message import Message
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# In Python 3.6:
#from email.message import Message
from utils import load_environment from utils import load_environment
@ -26,11 +32,23 @@ if content == "":
sys.exit(0) sys.exit(0)
# create MIME message # create MIME message
msg = Message() msg = MIMEMultipart('alternative')
# In Python 3.6:
#msg = Message()
msg['From'] = "\"%s\" <%s>" % (env['PRIMARY_HOSTNAME'], admin_addr) msg['From'] = "\"%s\" <%s>" % (env['PRIMARY_HOSTNAME'], admin_addr)
msg['To'] = admin_addr msg['To'] = admin_addr
msg['Subject'] = "[%s] %s" % (env['PRIMARY_HOSTNAME'], subject) msg['Subject'] = "[%s] %s" % (env['PRIMARY_HOSTNAME'], subject)
msg.set_payload(content, "UTF-8")
content_html = "<html><body><pre>{}</pre></body></html>".format(html.escape(content))
msg.attach(MIMEText(content, 'plain'))
msg.attach(MIMEText(content_html, 'html'))
# In Python 3.6:
#msg.set_content(content)
#msg.add_alternative(content_html, "html")
# send # send
smtpclient = smtplib.SMTP('127.0.0.1', 25) smtpclient = smtplib.SMTP('127.0.0.1', 25)

View file

@ -76,7 +76,8 @@ def scan_files(collector):
tmp_file = tempfile.NamedTemporaryFile() tmp_file = tempfile.NamedTemporaryFile()
shutil.copyfileobj(gzip.open(fn), tmp_file) shutil.copyfileobj(gzip.open(fn), tmp_file)
print("Processing file", fn, "...") if VERBOSE:
print("Processing file", fn, "...")
fn = tmp_file.name if tmp_file else fn fn = tmp_file.name if tmp_file else fn
for line in reverse_readline(fn): for line in reverse_readline(fn):
@ -119,8 +120,8 @@ def scan_mail_log(env):
except ImportError: except ImportError:
pass pass
print("Scanning from {:%Y-%m-%d %H:%M:%S} back to {:%Y-%m-%d %H:%M:%S}".format( print("Scanning logs from {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}".format(
START_DATE, END_DATE) END_DATE, START_DATE)
) )
# Scan the lines in the log files until the date goes out of range # Scan the lines in the log files until the date goes out of range
@ -138,8 +139,8 @@ def scan_mail_log(env):
# Print Sent Mail report # Print Sent Mail report
if collector["sent_mail"]: if collector["sent_mail"]:
msg = "Sent email between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" msg = "Sent email"
print_header(msg.format(END_DATE, START_DATE)) print_header(msg)
data = OrderedDict(sorted(collector["sent_mail"].items(), key=email_sort)) data = OrderedDict(sorted(collector["sent_mail"].items(), key=email_sort))
@ -173,8 +174,8 @@ def scan_mail_log(env):
# Print Received Mail report # Print Received Mail report
if collector["received_mail"]: if collector["received_mail"]:
msg = "Received email between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" msg = "Received email"
print_header(msg.format(END_DATE, START_DATE)) print_header(msg)
data = OrderedDict(sorted(collector["received_mail"].items(), key=email_sort)) data = OrderedDict(sorted(collector["received_mail"].items(), key=email_sort))
@ -202,8 +203,8 @@ def scan_mail_log(env):
# Print login report # Print login report
if collector["logins"]: if collector["logins"]:
msg = "User logins per hour between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" msg = "User logins per hour"
print_header(msg.format(END_DATE, START_DATE)) print_header(msg)
data = OrderedDict(sorted(collector["logins"].items(), key=email_sort)) data = OrderedDict(sorted(collector["logins"].items(), key=email_sort))