#!/bin/sh
#
# Plugin to monitor the amavis mail filter.
#
# Usage: Place in /etc/lrrd/client.d/ (or link it there  using ln -s)
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional)
#
# Config variables:
#
#       amavislog    - file where amavis logs are written
#

TEMP_FILE=`tempfile`
trap "rm -f ${TEMP_FILE}" EXIT
AMAVIS_LOG=/var/log/mail.info
OFFSET=/var/lib/munin/plugin-state/amavis.offset
LOGTAIL=`which logtail`

if [ "$amavislog"  ]; then AMAVIS_LOG=$amavislog ; fi

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

if [ "$1" = "config" ]; then
	echo 'graph_title Amavis filter statistics'
	echo 'graph_vlabel nb'
	echo 'total.label Mails scannés'
	echo "total.draw AREA"
	echo 'virus.label Mails vérolés'
	echo "virus.draw AREA"
	echo 'blocked.label Mails bloqués'
	echo "blocked.draw AREA"
	exit 0
fi

logtail -o ${OFFSET} -f ${AMAVIS_LOG} | grep 'amavis\[.*\]:' > ${TEMP_FILE}

echo "total.value `cat ${TEMP_FILE} | wc -l`"
echo "virus.value `grep -c infected ${TEMP_FILE}`"
echo "blocked.value `grep -c Blocked  ${TEMP_FILE}`"
