Toolserver:Python/Traceback mailer

From mediawiki.org

This page was moved from the Toolserver wiki.
Toolserver has been replaced by Toolforge. As such, the instructions here may no longer work, but may still be of historical interest.
Please help by updating examples, links, template links, etc. If a page is still relevant, move it to a normal title and leave a redirect.

Traceback mailer[edit]

The python cgitb is an incredibly useful module. It is an enhanced traceback that includes states of the variables in use. Here's a script that will email us the information if/when one of our tools breaks.

First enable the module in each of your scripts:

# Change traceback handling and log to ~/public_html/cgi-bin/tracebacks/
import cgitb; cgitb.enable(logdir='./tracebacks/')

Automatic HTML mailing script:

#!/bin/bash
# This script will email all files from $LOGSDIR in HTML format
# Useful for automatic traceback reports

LOGSDIR=$HOME/public_html/cgi-bin/tracebacks

# Remove "Broken pipe" errors from Solaris installations
if [ "$(grep -s -l "Broken pipe" $LOGSDIR/*)" ]
then
    rm $(grep -s -l "Broken pipe" $LOGSDIR/*)
fi

# Dump the python exception html log
if [ "$(ls $LOGSDIR/)" ]
then
    cat $LOGSDIR/* | mutt -s "Python exceptions" \
      -e 'my_hdr From: Error reporter' \
      -e 'set copy=no' \
      -e 'set content_type="text/html"' \
      $(whoami)
    rm $LOGSDIR/*
fi

The suggested crontab entry:

# Python traceback mailer, every five minutes
*/5 * *   *   *    scripts/pyErrorMailer.sh

Category:Documentation