#!/bin/sh
#
# Plugin to monitor the amavis mail filter for Debian
# (based upon a plugin authored by Geoffroy Desvernay)
#
# This plugin is built and tested on Debian Etch using:
# 	munin 1.2.5-1
#	amavisd-new 2.4.2-6.1
#
# With some minor modification it should also work on non-debian systems
# This, however, is up to you
#
# Munin graph will sum up: Passed CLEAN, Blocked VIRUS, Blocked SPAM, Other
#
# Parameters understood:
# 	config   (required)
# 	autoconf (optional)
#
# Config variables:
#       AMAVIS_LOG	- file where amavis logs are written
#	LOGTAIL		- location of logtail
#	BC		- location of bc
#
# Enjoy!
# Fili Wiese
#

AMAVIS_LOG=${logfile:-/var/log/mail.log}
STATEFILE=$MUNIN_PLUGSTATE/amavis.offset
LOGTAIL=${logtail:-`which logtail`}
BC=${bc:-`which bc`}

mktempfile () {
	mktemp
}

if [ "$1" = "autoconf" ]; then
        if [ -f "${AMAVIS_LOG}" -a -n "${LOGTAIL}" -a -x "${LOGTAIL}" -a -n "${BC}" -a -x "${BC}" ] ; then
		echo yes
	else
		echo no
	fi
	exit 0
fi

if [ "$1" = "config" ]; then
	echo 'graph_title Amavis filter statistics'
	echo 'graph_category antivirus'
	echo 'graph_order total clean spam virus other'
	echo 'graph_vlabel Mails filtered'
	echo 'graph_scale no'
	echo 'total.label Total'
	echo 'total.draw AREA'
	echo 'total.colour DDDDDD'
	echo 'clean.label Passed CLEAN'
	echo 'clean.draw LINE1'
	echo 'clean.colour 32FA00'
	echo 'spam.label Blocked SPAM'
	echo 'spam.draw LINE1'
	echo 'spam.colour FF0000'
	echo 'virus.label Blocked VIRUS'
	echo 'virus.draw LINE1'
	echo 'virus.colour 880088'
	echo 'other.label Other'
	echo 'other.draw LINE1'
	echo 'other.colour 0099FF'
	exit 0
fi

clean=0
virus=0
spams=0
other=0
total=0

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

TEMP_FILE=`mktempfile munin-amavis.XXXXXX`

if [ -n "$TEMP_FILE" -a -f "$TEMP_FILE" ]
then
	if [ $ARGS != 0 ]; then
	    $LOGTAIL ${AMAVIS_LOG} $STATEFILE | grep 'amavis\[.*\]:' | grep -v 'TIMED OUT' > ${TEMP_FILE}
	else
	    $LOGTAIL ${AMAVIS_LOG} $STATEFILE | grep 'amavis\[.*\]:' | grep -v 'TIMED OUT' > ${TEMP_FILE}
	fi
	total=`cat ${TEMP_FILE} | wc -l`
	clean=`grep 'CLEAN' ${TEMP_FILE} | wc -l`
	virus=`grep 'INFECTED' ${TEMP_FILE} | wc -l`
	spam=`grep 'Blocked SPAM' ${TEMP_FILE} | wc -l`
	other=`echo ${total}-${clean}-${virus}-${other}-${spam} | ${BC}`

	/bin/rm -f $TEMP_FILE
fi


echo "clean.value ${clean}"
echo "virus.value ${virus}"
echo "spam.value ${spam}"
echo "other.value ${other}"
echo "total.value ${total}"

