#!/bin/sh
#
# Plugin to monitor sendmail statistics.
#
# Usage: Place in /etc/munin/plugins/ (or link it there  using ln -s)
#
# Config variables:
#
#	mailstats - path to mailstats command
#
# Parameters understood:
#
# 	config   (required)
# 	autoconf (optional)
#
# Magic markers (used by munin-node-configure)
#%# capabilities=autoconf
#%# family=auto

if [ -n "$mailstats" ]; then MAILSTATS=$mailstats; else MAILSTATS=`which mailstats`; fi

if [ "$1" = "autoconf" ]; then
	if [ -n "$MAILSTATS" -a -x $MAILSTATS ]; then
		echo yes
		exit 0
	else
		echo "no (no mailstats command)"
		exit 1
	fi
fi

if [ "$1" = "config" ]; then

	echo "graph_title Sendmail email traffic"
	echo "graph_order received sent rejected discarded"
	echo 'graph_vlabel messages/${graph_period}'
	echo "graph_category sendmail"
	echo "discarded.label discarded"
	echo "discarded.max 1000000"
	echo "discarded.min 0"
	echo "discarded.type DERIVE"
	echo "received.label received"
	echo "received.max 1000000"
	echo "received.min 0"
	echo "received.type DERIVE"
	echo "rejected.label rejected"
	echo "rejected.max 1000000"
	echo "rejected.min 0"
	echo "rejected.type DERIVE"
	echo "sent.label sent"
	echo "sent.max 1000000"
	echo "sent.min 0"
	echo "sent.type DERIVE"
	exit 0
fi

$MAILSTATS -P | awk '/^ *T/ {
  received = received + $4 ;
  sent = sent + $2
  rejected = rejected + $6
  discarded = discarded + $7
}
END {
  print "received.value", received
  print "sent.value", sent
  print "rejected.value", rejected
  print "discarded.value", discarded
}'
