File:  [Local Repository] / comics / status_scan.py
Revision 1.2: download - view: text, annotated - select for diffs
Mon Feb 12 13:30:58 2018 UTC (6 years, 8 months ago) by nick
Branches: MAIN
CVS tags: HEAD
Added an easier to compare date string to determine if the status json file was updated today and report if it wasn't.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import datetime
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'])

    if report.get('errorMessage'):
        body += '\nAdditional Error Message:\n\t' + report['errorMessage']
    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)
    results = json.loads(response.read())
    now = datetime.datetime.now()
    shortDate = "%d%02d%02d" % (now.year, now.month, now.day)
    if results['date'] != shortDate:
        results['errorMessage'] = "Error: Comics not ran today!"
        notifyErrors(results)
        exit(2)
    return results


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>