#!/bin/sh


PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

ECHO="/bin/echo"
GREP="/bin/grep"
CAT="/bin/cat"
CUT="/bin/cut"


PROGNAME=`/bin/basename $0`
PROGPATH=`$ECHO $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
VERSION="CopyRight Richard NAGY for Nameshield (2004) - V 1.2\n"

. $PROGPATH/utils.sh



   
print_usage() {
    $ECHO "\n"
    $ECHO "Usage: $PROGNAME --help\n"
    $ECHO "Usage: $PROGNAME --version\n"
    $ECHO "Usage: $PROGNAME --id <ID number>\n"
}

print_help() {
#    print_revision $PROGNAME $REVISION
    $ECHO ""
    print_usage
    $ECHO ""
#    support
}


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

exitstatus=$STATE_WARNING #default
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            $ECHO "$VERSION"
            exit $STATE_OK
            ;;
        -V)
            $ECHO "$VERSION"
            exit $STATE_OK
            ;;
        --id)
		if [ $# -lt 2 ]; then
		    print_usage
		    exit $STATE_UNKNOWN
		fi
	    ;;
        *)
            $ECHO "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac

shift

if [ ! -e /proc/hba0 ]; then
    $ECHO "RAID check : /proc/hba0 not found\n"
    exit $STATE_UNKNOWN
fi

if [ ! -r /proc/hba0/diskdrives-ch0 ]; then
    $ECHO "RAID check : /proc/hba0/diskdrives-ch0 not readable\n"
    exit $STATE_UNKNOWN
fi

ID_NUMBER=$1


#echo "$CAT /proc/hba0/diskdrives-ch0 | $GREP \"Channel: 0 Id: $ID_NUMBER\" | $CUT -d: -f 4 | $CUT -c 2-"
#exit $STATE_WARNING

RES=`$CAT /proc/hba0/diskdrives-ch0 | $GREP "Channel: 0 Id: $ID_NUMBER" | $CUT -d: -f 4 | $CUT -c 2-`
#if [ "$RES" == "" ]; then
#	$ECHO "RAID check CRITICAL : ID $ID_NUMBER not responding\n"
#	exit $STATE_CRITICAL
#else
    		case "$RES" in
		        "Online.")
				$ECHO "RAID check OK : ID $ID_NUMBER Online\n"
				exit $STATE_OK
			        ;;
		        "Rebuild.")
				$ECHO "RAID check WARNING : ID $ID_NUMBER Rebuild\n"
				exit $STATE_WARNING
				;;
		        "Failed.")
				$ECHO "RAID check CRITICAL : ID $ID_NUMBER Failed\n"
				exit $STATE_CRITICAL
				;;
			*)
				$ECHO "RAID check CRITICAL : ID $ID_NUMBER not responding\n"
				exit $STATE_CRITICAL
				;;
		esac
#fi	


