File:  [Local Repository] / comics / status_scan.py
Revision 1.1: download - view: text, annotated - select for diffs
Tue Feb 6 14:59:10 2018 UTC (6 years, 8 months ago) by nick
Branches: MAIN
CVS tags: HEAD
Added a status scan script to e-mail me when there are errors.  This is
intended to run from cron

#!/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>