#!/bin/sh
# -*- sh -*-
#
# 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'
# You cannot trust the exit status of which
EXIM=$(which exim 2>/dev/null)
case $EXIM:$? in
    *:1|no*) EXIM=$(which exim4 2>/dev/null)
esac
case $EXIM:$? in
    *:1|no*) EXIM=''
esac

QUEUECRIT=200

EXIM=${exim:-$EXIM}
QUEUEWARN=${queuewarn:-100}
QUEUECRIT=${queuecrit:-200}

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;
                  }'
