#!/bin/bash
#
# Plugin to monitor Exim's mail queue
#
# Usage: Link into /etc/munin/plugins/
#
# Requirements:
#
# 	Needs to be run as user mail
#
# Parameters:
#
# 	config (required)
#
# Configurable variables:
#
# 	exim		- exim binary location
#	queuewarn	- warning limit
#	queuecrit	- critical limit
#
# Magic markers:
#%# family=contrib
#%# capabilities=

PATH='/bin:/sbin:/usr/bin:/usr/sbin'
EXIM=`which exim 2>/dev/null || which exim4 2>/dev/null`
QUEUEWARN=100
QUEUECRIT=200

[ "$exim" ] && EXIM=$exim
[ "$queuewarn" ] && QUEUEWARN=$queuewarn
[ "$queuecrit" ] && QUEUECRIT=$queuecrit

if [ "$1" == "config" ]; then
	echo 'graph_title Exim mailqueue'
	echo 'graph_args --base 1000 -l 0'
	echo 'graph_vlabel mails in queue'
	echo 'graph_order frozen active bounces total'
	echo 'graph_category exim'
	echo 'active.label active'
	echo 'active.draw STACK'
	echo 'bounces.label bounces'
	echo 'bounces.draw LINE2'
	echo 'frozen.label frozen'
	echo 'frozen.draw AREA'
	echo 'total.label total'
	echo 'total.draw LINE2'
	echo "total.warning $QUEUEWARN"
	echo "total.critical $QUEUECRIT"
	exit 0
fi

$EXIM -bpr | awk 'BEGIN { bounces = 0; frozen = 0; total = 0 }
                  $4 == "<>" { bounces++; }
                  $6 == "frozen" { frozen++ }
                  /<[^>]*>/ { total++ }
                  END {
                      print "frozen.value " frozen;
                      print "bounces.value " bounces;
                      print "total.value " total;
                      print "active.value " total - frozen;
                  }'
