#!/bin/bash
#
# Nikolay Diulgerov
#


CLIMIT=1000
WLIMIT=2000
STATE_OK=0
STATE_CRITICAL=2
STATE_WARNING=1
STATE_UNKNOWN=3
print_help() {
	echo ""
	echo "Usage: check_areca -w <warning> -c <critical>"
	echo ""
	echo "This plugin checks the areca controller fan speed and raid volume status."
	echo "Needs /usr/bin/cli32 to be installed on the linux machine."
	echo "cli32 should be owned by root.nagios with mode 750 and setuserid on execution"
	echo "cli32 can be downloaded from areca web site"
	echo "ftp://ftp.areca.com.tw/RaidCards/AP_Drivers/Linux/CLI"
	exit 0
}

case "$1" in
	-w)
		WLIMIT=$2
		CLIMIT=$4
		;;
	-c)
		CLIMIT=$2
		WLIMIT=$4
		;;
	--help)
		print_help
		exit $STATE_OK
		;;
	-h)
		print_help
		exit $STATE_OK
		;;
	*)
esac

if [ ! -f /usr/bin/cli32 ];
then
    echo "Missing cli32 file."
    print_help
    exit $STATE_UNKNOWN
fi




if [ `echo $WLIMIT $CLIMIT |awk '{print ($1 < $2)  ? "true" : "false" }'` = "true" ];
then
	echo "Error: WARNING value must be above CRITICAL value"
	print_help
	exit $STATE_UNKNOWN
fi

volumes=`cli32 vsf info|grep ARC-|wc -l`
if [ $volumes -ne 1 ];
then
echo "More than one volumes or controllers"
exit $STATE_UNKNOWN
fi


fan_speed_areca=`cli32 hw info|grep Fan#1|awk '{print $5}'`
volume_status=`cli32 vsf info|grep ARC-|awk '{print $10}'`


check_areca ()
{
if [[ ! "$fan_speed_areca" =~ ^[0-9]+$ ]];
then
    echo "No data about fan speed. Check your controller. Fan speed:$fan_speed_areca Volume status:$volume_status"
    exit $STATE_CRITICAL
fi

if [ `echo $fan_speed_areca $CLIMIT |awk '{print ($1 < $2) ? "true" : "false" }'` = "true" ];
then
    echo "Problems with fan speed. Fan speed:$fan_speed_areca Volume status:$volume_status"
    exit $STATE_CRITICAL
fi

volume_ok="Normal"

if [ ! "$volume_status" = "$volume_ok" ];
then
echo "Problems with raid volume. Fan speed:$fan_speed_areca Volume status:$volume_status"
exit $STATE_CRITICAL
fi

if [ `echo $fan_speed_areca $WLIMIT |awk '{print ($1 < $2) ? "true" : "false" }'` = "true" ];
then
    echo "Problems with fan speed. Fan speed:$fan_speed_areca Volume status:$volume_status"
    exit $STATE_WARNING
fi
}

check_areca

echo "System OK: Fan speed:$fan_speed_areca Volume status:$volume_status"
exit $STATE_OK

