#! /bin/bash
#
# Use -h for description of what it does.


allowed_unit_stats="OK"
allowed_disk_stats="OK NOT-PRESENT"


############################################################################

#0 OK
#1 warning
#2 critical
#3 unknown

needed_apps="tw_cli sudo egrep"
verbose=0


if [ "$1" = "-h" ] || [ "$1" = "-H" ] ; then
    echo ""
    echo -e "This nrpe-check was written for checking the health of  3ware-controllers. It uses the tw_cli \
tool from 3ware and probes units und disks. Multiple controllers are supportes. \n \nHave a look into the head of the file to set the states you accept. \n \n"
    echo -e "-v sets verbosity \n-h shows this help"
    echo ""
    exit 0
fi

[ "$1" = "-v" ] && verbose=1

function debug_func
{
    [ $verbose -eq 0 ] || echo "$1" ;
}

health="bad"

for prog in $needed_apps; do
    UPPER=$(echo $prog | tr  \"[:lower:]\" \"[:upper:]\")
    which $prog > /dev/null
	if [ $? -ne 0 ]; then
		echo "Could not find needed app: \"$prog\" - can't work.";
		exit 2
	fi
    eval "$UPPER=$(which $prog)";
done

debug_func "### now counting controllers"
controller_count=$(( $(echo "$(sudo $TW_CLI info | egrep -c '^c[0-9] ')") ))

if [ $controller_count -le 0 ]; then
    echo "No controllers found - exiting."
    exit 2
else
    debug_func "### Found $controller_count controller."
fi

debug_func "### creating temporary files"
tw_names=$(mktemp /tmp/tw-names.XXXXXXXX)
tw_states=$(mktemp /tmp/tw-states.XXXXXXXX)


debug_func "### now trying to get controller names"
sudo $TW_CLI info | egrep '^c[0-9] ' | cut -d ' ' -f1 >> $tw_names


for controller_num in $(cat $tw_names); do
    sudo $TW_CLI info ${controller_num} > $tw_states

    #count units, start counting with 0
    unit_count=$(( $(echo "$(cat $tw_states |  awk '{print $1}' |  egrep -c '^u[0-9]') - 1") ))
    #count disks, start counting with 0
    disk_count=$(( $(echo "$(cat $tw_states |  awk '{print $1}' |  egrep -c '^p[0-9]') - 1") ))

    debug_func "### Found ($unit_count+1) units, ($disk_count+1) disks on controller-no: $controller_num"
    debug_func "### now checking units"


    for unit in $(seq 0 $unit_count); do
	status=$(egrep "^u${unit}" $tw_states | awk '{print $3}')
	#debug_func "allowed_unit_stats $allowed_unit_stats"
	debug_func "### status of unit u${unit} $status"
	found_status=$(echo "$allowed_unit_stats" | grep "$status" | wc -l)
	if [ "$found_status" != "1" ]; then
	   echo "Damaged Unit: u${unit} on 3ware-RC. "
	   health="bad"
        else
	   health="good"
	fi
    done

    debug_func "### now checking disks"

    for disk in $(seq 0 $disk_count); do
	status=$(egrep "^p${disk}" $tw_states | awk '{print $2}')
	debug_func "### status of disk p${disk}: $status"
	found_status=$(echo $allowed_disk_stats | grep $status | wc -l)
	if [ "$found_status" != "1" ] ; then
	    echo "Damaged Disk: p${disk} 3ware-RC. "
	    health="bad"
	else
	    health="good"
	fi
    done
done

debug_func "### our status is: $health"


debug_func "### deleting temporary files"
rm $tw_names && rm $tw_states && debug_func "### ...deleted"

if [ "$health" = "good" ]; then
    echo "All OK"
    exit 0
else
    exit 2
fi

