#!/bin/sh
#
# Plugin to graph perdition connections and errors 
# Requires: logtail
# 
# Copyright Micah Anderson <micah@riseup.net>
# Jan 23, 2005
#
#
#%# family=contrib
#%# capabilities=autoconf

# The different log lines we are interested in:
#
# buffy perdition[7583]: Connect: 64.45.82.181->69.50.164.185
# buffy perdition[20097]: Close: 217.19.50.108->69.50.74.154 user="mek" received=12 sent=23
# buffy perdition[7435]: Auth: 130.22.173.20->69.90.134.185 user="gotn" server="192.168.0.2" port="143" status="ok"
# buffy perdition[26986]: Auth: 72.13.2.186->69.92.134.215 user="moves" server="192.168.0.2" port="110" status="ok"

# Then there are some errors, I'm just going to put these all into one line, they could easily be
# separate out if we were interested in how many of each type of error, but 7 lines for this graph is a lot
# buffy perdition[20908]: Fatal Error reading authentication information from client "203.52.112.34->68.92.124.155": Exiting child
# buffy perdition[27754]: Fatal error establishing SSL connection to client
# buffy perdition[5139]: Fatal error negotiating setup. Exiting child.

# Changelog
# version 0.1 - Jan 23, 2005
# Micah Anderson <micah@riseup.net>
#  - initial author
# version 0.2 - Oct 10, 2007
# Micah Anderson <micah@riseup.net>
#  - fixed copyright and added changelog
#  - added TMP env variable
#  - set all TEMP_FILE variables to use $TMP

# Set the location of the perdition logs
PERDITION_LOG=${logfile:-/var/log/perdition.log}
OFFSET_FILE=/var/lib/munin/plugin-state/perdition.offset
LOGTAIL=${logtail:-/usr/sbin/logtail}
TMP=${TMP:-/tmp}

case `uname -s` in
Linux)
        TEMP_FILE=`mktemp -p $TMP/ munin-perdition.XXXXXX`
        if [ $? != 0 ]; then
                TEMP_FILE=`mktemp $TMP/munin-perdition.XXXXXX`
        fi
        ;;
FreeBSD)
        TEMP_FILE=`mktemp -t $TMP`
        STATUS=$?
        ;;
esac

if [ -z "$TEMP_FILE" ]; then
        # Yes, this is unsafe
        TEMP_FILE=$TMP/munin-perdition.$$
        rm -rf $TEMP_FILE
        touch $TEMP_FILE
fi

if [ ! -f "$TEMP_FILE" ]; then
    exit 3
fi

case $1 in
    autoconf|detect)
    if [ -f ${PERDITION_LOG} -a -x ${LOGTAIL} ] 
    then
	echo yes
	exit 0
    else
	echo "no (either $PERDITION_LOG was not found, or logtail was not in your path)"
	exit 1
    fi
    ;;
    config)
    cat <<EOF
graph_title Perdition Connections
graph_vlabel Number of Connections
graph_total Total
connection.label connections
disconnected.label disconnections
imap.label IMAP Auths
pop.label POP Auths
fatal.label Fatal Errors
EOF
    exit 0
    ;;
esac

ARGS=0
`$LOGTAIL /etc/hosts 2>/dev/null >/dev/null`
if [ $? = 66 ]; then
    if [ ! -n "$logtail" ]; then
        ARGS=1
    fi
fi

if [ $ARGS != 0 ]; then
    ${LOGTAIL} -f ${PERDITION_LOG} -o ${OFFSET_FILE} > ${TEMP_FILE}
else
    ${LOGTAIL} ${PERDITION_LOG} ${OFFSET_FILE} > ${TEMP_FILE}
fi
connection=`grep "Connect:" ${TEMP_FILE} | wc -l`
disconnected=`grep "Close:" ${TEMP_FILE} | wc -l`
imap=`grep 'port="143" status="ok"' ${TEMP_FILE} | wc -l`
pop=`grep 'port="110" status="ok"' ${TEMP_FILE} | wc -l`
fatal=`grep 'Fatal [e|E]rror' ${TEMP_FILE} | wc -l`

rm ${TEMP_FILE}

echo "connection.value ${connection}"
echo "disconnected.value ${disconnected}"
echo "imap.value ${imap}"
echo "pop.value ${pop}"
echo "fatal.value ${fatal}"
