#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import smtplib
import socket
import urllib
from email.mime.text import MIMEText
def sendMail(subject, body, address):
_from = "comics@%s" % socket.getfqdn()
msg = MIMEText(body)
msg['Subject'] = subject
msg['To'] = address
msg['From'] = _from
s = smtplib.SMTP('localhost')
s.sendmail(_from, [address], msg.as_string())
def generateMessageBody(report):
body = 'The following comic(s) are reporting errors:\n'
for error in report['comics']:
if error['error']:
body += ' %-25s%s\n' % (error['comicName'], error['error'])
return body
def notifyErrors(report):
to = 'nick@demandred.dyndns.org'
subject = 'Error found in daily comics'
body = generateMessageBody(report)
sendMail(subject, body, to)
def fetchStatusReport(source):
response = urllib.urlopen(source)
return json.loads(response.read())
def main():
status_report = 'http://demandred.dyndns.org/daily/comics/status_report.json'
report = fetchStatusReport(status_report)
if report.get('totalErrors') and report['totalErrors'] > 0:
notifyErrors(report)
exit(1)
if __name__ == '__main__':
main()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>