#!/bin/sh

# $Id: mailqsize,v 1.4 2002/05/07 17:47:30 khera Exp $

# output the number of messages in the incoming, active, and deferred
# queues of postfix one per line suitable for use with snmpd/cricket
#
# mailqsize was originally written by Vivek Khera.  All I did was
# make it update an rrd.
# 2003/01/24 01:19:37  Mike Saunders <method at method DOT cx>
# I bundled this with a modified mailgraph
# 2003/04/14           Ralf Hildebrandt <ralf.hildebrandt at charite DOT de>

# change this to the location of rrdtool
PATH=/sbin:/bin:/usr/bin:/usr/sbin
RRDTOOL=rrdtool
# change this to the location you want to store the rrd
RRDFILE=/var/lib/queuegraph/mailqueues.rrd

if test ! -f $RRDFILE ; then
	echo "Creating RRD file $RRDFILE"

	su - _queuegraph -s /bin/sh -c "$RRDTOOL create $RRDFILE --step 60 \
		DS:active:GAUGE:900:0:U \
		DS:deferred:GAUGE:900:0:U \
		RRA:AVERAGE:0.5:1:20160 \
		RRA:AVERAGE:0.5:30:2016 \
		RRA:AVERAGE:0.5:60:105120 \
		RRA:MAX:0.5:1:1440 \
		RRA:MAX:0.5:30:2016 \
		RRA:MAX:0.5:60:105120"
fi

#set -x
qdir=`postconf -h queue_directory`
active=`find $qdir/incoming $qdir/active $qdir/maildrop -type f -print | wc -l | awk '{print $1}'`
deferred=`find $qdir/deferred -type f -print | wc -l | awk '{print $1}'`
#printf "active: %d\ndeferred: %d\n" $active $deferred

su - _queuegraph -s /bin/sh -c "$RRDTOOL update $RRDFILE \"N:$active:$deferred\""
