#!/bin/sh
# 
# Plugin to monitor postfix mail spools
# 
# Contributed by Nicolai Langfeldt
# 
#%# family=auto
#%# capabilities=autoconf

# Can be set via environment, but default is /var/spool/postfix
SPOOLDIR=${spooldir:-/var/spool/postfix}

# Postfix mqueue management: http://www.postfix.cs.uu.nl/queuing.html
# maildrop: Localy posted mail
# incoming: Processed local mail and received from network
# active: Mails being delivered (should be small)
# deferred: Stuck mails (that will be retried later)
# corrupt: Messages found to not be in correct format (shold be 0)
# hold: Recent addition, messages put on hold indefinitly - delete of free

case $1 in
    autoconf|detect)
	if [ -d $SPOOLDIR/ ] ; then
	    echo yes
	    exit 0
        else
	    echo "no (spooldir not found)"
	    exit 1
        fi;;
    config)
	cat <<'EOF'
graph_title Postfix Mailqueue
graph_vlabel Mails in queue
graph_category postfix
graph_total Total
active.label active
deferred.label deferred
maildrop.label maildrop
incoming.label incoming
corrupt.label corrupt
hold.label held
EOF
	exit 0;;
esac

cd $SPOOLDIR >/dev/null 2>/dev/null || {
     echo "# Cannot cd to $SPOOLDIR"
     exit 1
}

cat <<EOF
deferred.value `(test -d deferred && find deferred -type f ) | wc -l`
active.value `(test -d active && find active -type f ) | wc -l`
maildrop.value `(test -d maildrop && find maildrop -type f ) | wc -l`
incoming.value `(test -d incoming && find incoming -type f ) | wc -l`
corrupt.value `(test -d corrupt && find corrupt -type f ) | wc -l`
hold.value `( test -d hold && find hold -type f ) | wc -l`
EOF
