5033042b8c
Refactor by moving the email-the-admin code out of the status checks and into a new separate tool. This is why I suppressed non-error output of the backups last commit - so it doesn't send a daily email.
42 lines
952 B
Python
Executable file
42 lines
952 B
Python
Executable file
#!/usr/bin/python3
|
|
|
|
# Reads in STDIN. If the stream is not empty, mail it to the system administrator.
|
|
|
|
import sys
|
|
|
|
import smtplib
|
|
from email.message import Message
|
|
|
|
from utils import load_environment
|
|
|
|
# Load system environment info.
|
|
env = load_environment()
|
|
|
|
# Process command line args.
|
|
subject = sys.argv[1]
|
|
|
|
# Administrator's email address.
|
|
admin_addr = "administrator@" + env['PRIMARY_HOSTNAME']
|
|
|
|
# Read in STDIN.
|
|
content = sys.stdin.read().strip()
|
|
|
|
# If there's nothing coming in, just exit.
|
|
if content == "":
|
|
sys.exit(0)
|
|
|
|
# create MIME message
|
|
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")
|
|
|
|
# send
|
|
smtpclient = smtplib.SMTP('localhost', 25)
|
|
smtpclient.ehlo()
|
|
smtpclient.sendmail(
|
|
admin_addr, # MAIL FROM
|
|
admin_addr, # RCPT TO
|
|
msg.as_string())
|
|
smtpclient.quit()
|