#!/bin/sh
#
# Plugin for Nagios to monitor the hylafax-queue
# Written by Guentehr Orth (orth@enbiz.de)
# Date: 20041211 
# License: GPL
#
# Usage: ./check_faxqueue -w <warn> -c <crit>
#
# This plug check the faxqueue of hylafax an 
# compares it with the supplied thresholds.
#
# Output:
# The plugin prints the number of faxes in the queue followed
# by the report "ok", "critial" or "warning"
#
# Exit Codes:
# 0 OK       The number of faxes in the queue are under the warning
#            or under the critical threshold
# 1 Warning  Number of faxes in the queue above "warning" threshold
# 2 Critical Number of faxes in the queue above "critical" threshold
# 3 Unknown  Invalid command line arguments or could not determinate the number
#            of faxes in the fax queue

# Path definition

FAXSTAT="/usr/bin/faxstat"
GREP="/bin/grep"
WC="/usr/bin/wc"

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

# Print infos
PROGNAME=`/usr/bin/basename $0`
REVISION="Revision 0.9,"
AUTHOR="(c) Guenther Orth,"
LICENSE="License: GPL"

print_revision()(
	echo "$REVISION $AUTHOR $LICENSE"
)

print_usage()	(
	echo "Usage: $PROGNAME -w <warn> -c <crit>"
	echo "Usage: $PROGNAME --help"
	echo "Usage: $PROGNAME --version"
)

print_help()	(
	print_revision 
	echo
	echo "Hylafax Faxqueue monitor plugin for Nagios"
	echo
	print_usage
	echo
)

# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 1 ]; then
	print_usage
	exit $STATE_UNKNOWN
fi

# Grab the command line arguments

read_warn=""
read_crit=""
exitstatus=$STATE_WARNING	#default message

while test -n "$1"; do
	case "$1" in 
		--help)
			print_help
			exit $STATE_OK
		;;
		-h)
			print_help
			exit $STATE_OK
		;;
		--version)
			print_revision
			exit $STATE_OK
		;;
		-V)
			print_revision
			exit $STATE_OK
		;;
		-w)
			read_warn=$2
			shift
		;;
		-c)
			read_crit=$2
			shift
		;;
		*)
			echo "Unknown argument: $1"
			exit $STATUS_UNKNOWN
		;;
	esac
	shift
done

##### Get the number of faxes from faxqueue

#cnt=`/usr/bin/faxstat -s -a | grep -v "Modem" | wc -l` cnt=$[$cnt - 1]
cnt=`$FAXSTAT -s -a | $GREP -v "Modem" | $WC -l` cnt=$[$cnt - 1]

##### Compare the threshold with  the queue

exitstatus=$STATE_OK
result="ok"

if [ "$read_warn" != "" ]; then
	if [ $cnt -ge $read_warn ]; then
		result="warning"
		exitstatus=$STATE_WARNING
	fi
fi

if [ "$read_crit" != "" ]; then
	if [ $cnt -ge $read_crit ]; then
		result="critical"
		exitstatus=$STATE_CRITICAL
	fi
fi

echo "$cnt Faxes - $result"
exit $exitstatus

