#!/usr/bin/bash
### 
# Alexandru Ionica - alexandru.ionica@gmail.com
# Version 0.1 - 06 March 2010
# Plugin to check the status of Sun StorageTek SAS RAID HBA and it's attached disks.
#
# Created and tested on OpenSolaris 2009.06 x86 .
# This plugin was created for Adulmec s.r.l. (http://corporate.adulmec.ro) and they agreed to make it public.
#
# License: Gnu Public License - version 2
#
###
#replace below with the path to the arcconf binary, you must configure sudo 
#to be able to run arcconf as a non privileged user
#you need to add to /etc/sudoers something like:     nagios ALL = NOPASSWD: /opt/storagetek/bin/arcconf
ARCCONF="/opt/storagetek/bin/arcconf"
TMPFILE=/tmp/storagetek-$$-$(date +%s)
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
RESULT_MSG=""
RESULT=0


function print_help {
	echo "Usage: $0 -c controller_number"
	exit
}

function print_arcconf_err {
	echo "Error code returned after running $ARCCONF"
	rm $TMPFILE
	exit $STATE_UNKNOWN
}

if [ ! -x $ARCCONF ] ; then echo "ARCCONF has value $ARCCONF"
				echo "$ARCCONF is not executable";
				exit $STATE_UNKNOWN;
fi
ARCCONF="sudo $ARCCONF"
#check if we have any user input, if not exit
if [ $# -lt 1 ] ; then print_help ;fi

#read parameters
while [ "$1" ] ; do
	case "$1" in 
		--help | -h )	print_help 
				exit $STATE_OK
				;;	
		-c | --controller )	if [ "$2" ] ; then CONTROLLER=$2
						else echo "Error: you didn't specify the controller number"
							print_help
							exit $STATE_UNKNOWN
					fi
					;;
		*)		print_help
				exit $STATE_UNKNOWN
				;;
	esac
	shift;shift;	#we shift to the next two parameters, if any
done

#get the physical devices status 
$ARCCONF GETCONFIG $CONTROLLER PD > $TMPFILE
if [ $? -ne 0 ] ; then print_arcconf_err;fi

#check output and see if anything is not ok
if [ "$(grep State $TMPFILE | grep -v Online)" ] ; then RESULT_MSG="Physical device(s) NOT OK: $(grep State $TMPFILE | grep -v Online | awk {'print $3'});"
							 RESULT=$STATE_CRITICAL
						else RESULT_MSG="Physical devices OK;"
fi

#get controller and battery status
$ARCCONF GETCONFIG $CONTROLLER AD > $TMPFILE
if [ $? -ne 0 ] ; then print_arcconf_err;fi

#check output for controller information and see if anything is not ok
if [ "$(grep 'Controller Status' $TMPFILE | grep -v Optimal)" ] ; then RESULT_MSG="${RESULT_MSG}Controller $CONTROLLER NOT OK: $(grep 'Controller Status' $TMPFILE | grep -v Optimal | awk {'print $4'})"
							 		if [ $RESULT -lt $STATE_CRITICAL ] ; then RESULT=$STATE_CRITICAL; fi
								  else RESULT_MSG="${RESULT_MSG}Controller $CONTROLLER OK;"
fi

#check output for battery information and see if anything is not ok
if [ "$(grep -A 2 'Controller Battery Information' $TMPFILE | grep Status | grep -v Optimal)" ] ; then RESULT_MSG="${RESULT_MSG}Battery NOT OK: $(grep -A 2 'Controller Battery Information' $TMPFILE | grep Status | grep -v Optimal | awk {'print $3'})" 
												if [ $RESULT -lt $STATE_CRITICAL ] ; then RESULT=$STATE_CRITICAL; fi
											else RESULT_MSG="${RESULT_MSG}Battery OK;"
fi

#get logical devices status
$ARCCONF GETCONFIG $CONTROLLER LD > $TMPFILE

#check logical devices status
if [ "$(grep Status $TMPFILE | grep -v Optimal)" ] ; then RESULT_MSG="${RESULT_MSG}Logical Device(s) NOT OK: $(grep Status $TMPFILE | grep -v Optimal | awk {'print $6'})"
								if [ $RESULT -lt $STATE_CRITICAL ] ; then RESULT=$STATE_CRITICAL; fi
							else RESULT_MSG="${RESULT_MSG}Logical Devices OK"
fi

rm $TMPFILE #we remove the temp file
echo $RESULT_MSG
exit $RESULT
