#!@@GOODSH@@
# -*- sh -*-

: << =cut

=head1 NAME

perdition - Plugin to graph perdition connections and errors

=head1 CONFIGURATION

The following configuration variables are available for this plugin:

 logfile - Log file to tail (default: "/var/log/perdition.log")
 logtail - Path to logtail (default: "/usr/sbin/logtail")

=head1 DEPENDENCIES

Requires: logtail

=head1 AUTHOR

Copyright Micah Anderson <micah@riseup.net>
Jan 23, 2005

=head1 LICENSE

Unknown

=head1 MAGIC MARKERS

 #%# family=contrib
 #%# capabilities=autoconf

=head1 NOTES

# 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.

=cut


# Set the location of the perdition logs
PERDITION_LOG=${logfile:-/var/log/perdition.log}
OFFSET_FILE=${MUNIN_PLUGSTATE}/perdition.offset
LOGTAIL=${logtail:-/usr/sbin/logtail}

case $1 in
    autoconf|detect)
    if [ -f "$PERDITION_LOG" ] && [ -x "$LOGTAIL" ]; then
        echo yes
        exit 0
    else
        echo "no (either $PERDITION_LOG was not found, or logtail was not in your path)"
        exit 0
    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
imaps.label IMAPS Auths
pop.label POP Auths
pops.label POPS Auths
fatal.label Fatal Errors
EOF
    # Echo warnnig and critical attributes if set in the plugin config
    for i in imap imaps pop pops fatal connection disconnected; do
        warn=\$${i}_warn; eval val=$warn
        [ -n "${val:-}" ] && echo "$i.warning ${val}"
        crit=\$${i}_crit; eval val=$crit
        [ -n "${val:-}" ] && echo "$i.critical ${val}"
    done
    exit 0
    ;;
esac

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

TEMP_FILE=$(@@MKTEMP@@ munin-perdition.XXXXXX)

if [ -z "$TEMP_FILE" ] || [ ! -f "$TEMP_FILE" ]; then
    exit 3
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)
imaps=$(grep 'port="993" status="ok"' "$TEMP_FILE" | wc -l)
pop=$(grep 'port="110" status="ok"' "$TEMP_FILE" | wc -l)
pops=$(grep 'port="995" 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 "imaps.value ${imaps}"
echo "pop.value ${pop}"
echo "pops.value ${pops}"
echo "fatal.value ${fatal}"
