#!/bin/sh

. /usr/lib64/nagios/plugins/utils.sh


VERSION="1.4"

EXIM=/usr/sbin/exim
SUDO=/usr/bin/sudo

FLAG_VERBOSE=FALSE
LEVEL_WARN=""
LEVEL_CRIT=""
CONFIG_FILE=""
RESULT=""
EXIT_STATUS=$STATE_OK

usage() {
        echo " check_eximailqueue $VERSION - Nagios Exim mail queue check script"
        echo ""
        echo " Usage: check_eximailqueue -w <warning queue size> -c <critical queue size> [ -v ] [ -h ]"
        echo ""
        echo "           -w  Queue size at which a warning is triggered"
        echo "           -c  Queue size at which a critical is triggered"
        echo "           -C  Exim Configuration file if required"
        echo "           -v  Verbose output (ignored for now)"
        echo "           -h  Show this page"
        echo ""
}

doopts() {
        if ( `test 0 -lt $#` )
        then
                while getopts w:c:C:vh myarg "$@"
                do
                        case $myarg in
                                h|\?)
                                        usage
                                        exit;;
                                w)
                                        LEVEL_WARN=$OPTARG;;
                                c)
                                        LEVEL_CRIT=$OPTARG;;
                                C)
                                        CONFIG_FILE=$OPTARG;;
                                v)
                                        FLAG_VERBOSE=TRUE;;
                                *)
                                        usage
                                        exit;;
                        esac
                done
        else
                usage
                exit
        fi
}

theend() {
        echo $RESULT
        exit $EXIT_STATUS
}

doopts $@

if [ "$CONFIG_FILE" == "" ]; then
        OUTPUT=`$SUDO -u root ${EXIM} -bpc`
else
        OUTPUT=`$SUDO -u root ${EXIM} -C ${CONFIG_FILE} -bpc`
fi

if test -z "$OUTPUT" ; then
        RESULT="Mailqueue WARNING - query returned no output!"
        EXIT_STATUS=$STATE_WARNING
else
        if test "$OUTPUT" -lt "$LEVEL_WARN" ; then
                RESULT="Mailqueue OK - $OUTPUT messages on queue"
                EXIT_STATUS=$STATE_OK
        else
                if test "$OUTPUT" -ge "$LEVEL_CRIT" ; then
                        RESULT="Mailqueue CRITICAL - $OUTPUT messages on queue"
                        EXIT_STATUS=$STATE_CRITICAL
                else
                        if test "$OUTPUT" -ge "$LEVEL_WARN" ; then
                                RESULT="Mailqueue WARNING - $OUTPUT messages on queue"
                                EXIT_STATUS=$STATE_WARNING
                        fi
                fi
        fi
fi

# Quit and return information and exit status
theend

