Skip to content

Commit

Permalink
send the mail_log.py report to the box admin every Monday
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshData committed Feb 25, 2018
1 parent 9c7820f commit 1eba7b0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
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.
* The undocumented feature for proxying web requests to another server now sets X-Forwarded-For.

Expand Down
6 changes: 6 additions & 0 deletions management/daily_tasks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export LC_ALL=en_US.UTF-8
export LANG=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.
management/backup.py | management/email_administrator.py "Backup Status"

Expand Down
24 changes: 21 additions & 3 deletions management/email_administrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

import sys

import html
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

Expand All @@ -26,11 +32,23 @@
sys.exit(0)

# create MIME message
msg = Message()
msg = MIMEMultipart('alternative')

# In Python 3.6:
#msg = Message()

msg['From'] = "\"%s\" <%s>" % (env['PRIMARY_HOSTNAME'], admin_addr)
msg['To'] = admin_addr
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
smtpclient = smtplib.SMTP('127.0.0.1', 25)
Expand Down
19 changes: 10 additions & 9 deletions management/mail_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def scan_files(collector):
tmp_file = tempfile.NamedTemporaryFile()
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

for line in reverse_readline(fn):
Expand Down Expand Up @@ -119,8 +120,8 @@ def scan_mail_log(env):
except ImportError:
pass

print("Scanning from {:%Y-%m-%d %H:%M:%S} back to {:%Y-%m-%d %H:%M:%S}".format(
START_DATE, END_DATE)
print("Scanning logs from {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}".format(
END_DATE, START_DATE)
)

# Scan the lines in the log files until the date goes out of range
Expand All @@ -138,8 +139,8 @@ def scan_mail_log(env):
# Print Sent Mail report

if collector["sent_mail"]:
msg = "Sent email between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
print_header(msg.format(END_DATE, START_DATE))
msg = "Sent email"
print_header(msg)

data = OrderedDict(sorted(collector["sent_mail"].items(), key=email_sort))

Expand Down Expand Up @@ -173,8 +174,8 @@ def scan_mail_log(env):
# Print Received Mail report

if collector["received_mail"]:
msg = "Received email between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
print_header(msg.format(END_DATE, START_DATE))
msg = "Received email"
print_header(msg)

data = OrderedDict(sorted(collector["received_mail"].items(), key=email_sort))

Expand Down Expand Up @@ -202,8 +203,8 @@ def scan_mail_log(env):
# Print login report

if collector["logins"]:
msg = "User logins per hour between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
print_header(msg.format(END_DATE, START_DATE))
msg = "User logins per hour"
print_header(msg)

data = OrderedDict(sorted(collector["logins"].items(), key=email_sort))

Expand Down

1 comment on commit 1eba7b0

@jirislav
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man, I like this feature :)

Please sign in to comment.