#!/bin/bash
#
# Plugin to monitor Amavis virus and spam statistics.
#
# 
# Based on a routine by William Towle
# Uncomment the cdef lines to convert the graph to mails/minute
# Comment out the line "total.graph no" to show the total on the graph. This may not be aesthetically pleasing.
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional)
#

# requires logtail

LOGDIR=${logdir:-/var/log/amavis}
MAIL_LOG=$LOGDIR/${logfile:-amavisd.log}
LOGTAIL=${logtail:-`which logtail`}
STATEFILE=/var/lib/munin/plugin-state/amavis.offset

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

if [ "$1" = "config" ]; then
	echo 'graph_title Amavis message filtering'

	echo 'graph_category mail'
	echo 'graph_vlabel Mails per minute'
	echo 'graph_args --base 1000 -l 0'

	echo 'graph_order clean p_spam b_spam virus total'
	
	echo 'clean.min 0'
	echo 'clean.type ABSOLUTE'
	#echo 'clean.cdef clean,60,*'
	echo 'clean.draw AREA'

		for i in p_spam b_spam virus;
		do
			echo "$i.min 0"
			echo "$i.type ABSOLUTE";
			#echo "$i.cdef $i,60,*";
			echo "$i.draw STACK";
		done

	echo 'clean.label Passed CLEAN'
	echo 'p_spam.label Passed SPAMMY'
	echo 'b_spam.label Blocked SPAMMY'
	echo 'virus.label Blocked INFECTED'
	echo 'total.label Total'
	echo 'total.graph no'
	echo 'clean.colour 00ff00'
	echo 'p_spam.colour ff4000'
	echo 'b_spam.colour ff0000'
	echo 'virus.colour 000000'
        exit 0

fi




$LOGTAIL ${MAIL_LOG} $STATEFILE | \
awk 'BEGIN { na= 0; nb= 0; nc= 0; nd= 0; total= 0 } 

	{       
	
               if (index($0, "Passed CLEAN")) { na++ ; total++ } 
               else if (index($0, "Passed SPAMMY")) { nb++ ; total++ }    
               else if (index($0, "Blocked SPAMMY")) { nc++ ; total++ }
               else if (index($0, "INFECTED")) { nd++ ; total++ }
	}
	END { print "clean.value " na"\np_spam.value " nb"\nb_spam.value " nc"\nvirus.value " nd"\ntotal.value " total }'


