#!/bin/sh

TFILE="/tmp/$(basename $0).$$.tmp"

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

ENCLOSURE=0
DISK=0

STATE=STATE_OK

if test -x /usr/bin/printf; then
        ECHO=/usr/bin/printf
else
        ECHO=echo
fi

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`

print_usage() {
  echo "Usage:"
  echo "  $PROGNAME --hostname <Dorado IP address>"
  echo "  $PROGNAME --login <Dorado Login>"
  echo "  $PROGNAME --password <Dorado Password>"
  echo "  $PROGNAME --check [power|coffer|controller|disk|alarms]"
  echo "  $PROGNAME --enclosure <id>"
  echo "  $PROGNAME --disk <id>"
  echo "  $PROGNAME --help"
}

print_help() {
  echo ""
  print_usage
  echo ""
  echo "Check Dorado status"
  echo ""
  echo "--hostname <Hostname>"
  echo "   Set remote Dorado IP address or hostname"
  echo "--login <Login>"
  echo "   Set remote Dorado login"
  echo "--password <Password>"
  echo "   Set remote Dorado password"
  echo "--check [power|coffer|controller|disk|alarms]"
  echo "   Check Dorado for power, disk errors or alarms"
  echo "--enclosure <id>"
  echo "   Check disk using enclosure id <id>"
  echo "--disk <id>"
  echo "   Check disk <id>, need also enclosure id to be specified"
  echo "--help"
  echo "   Print this help screen"
  echo ""
  support
}


while :; do
    case "$1" in
--help|-h)
        print_help
	exit $STATE_OK
	;;
--hostname)
	if [ "$2" ]; then
	    # echo "Setting hostname to $2"
    	    HOST=$2
            shift 2
            continue
        else
    	    echo 'ERROR: Must specify a non-empty "--hostname <Hostname>" argument.' >&2
            exit $STATE_OK
	fi
        ;;
--hostname=?*)
	# echo "Setting hostname to ${1#*=}"
	HOST=${1#*=}
	;;
--login)
	if [ "$2" ]; then
	    # echo "Setting login to $2"
    	    LOGIN=$2
            shift 2
            continue
        else
    	    echo 'ERROR: Must specify a non-empty "--login <Login>" argument.' >&2
            exit $STATE_OK
	fi
        ;;
--login=?*)
	# echo "Setting login to ${1#*=}"
	LOGIN=${1#*=}
	;;
--password)
	if [ "$2" ]; then
	    # echo "Setting password to *******"
    	    PASSWORD=$2
            shift 2
            continue
        else
    	    echo 'ERROR: Must specify a non-empty "--password <Password>" argument.' >&2
            exit $STATE_OK
	fi
        ;;
--password=?*)
	# echo "Setting password to *******"
	PASSWORD=${1#*=}
	;;
--check)
	if [ "$2" ]; then
	    # echo "Setting check to $2"
    	    OPERATION=$2
            shift 2
            continue
        else
    	    echo 'ERROR: Must specify a non-empty "--check <power|disks|alarms>" argument.' >&2
            exit $STATE_OK
	fi
        ;;
--enclosure)
	if [ "$2" ]; then
	    # echo "Setting enclosure to $2"
    	    ENCLOSURE=$2
            shift 2
            continue
        else
    	    echo 'ERROR: Must specify a non-empty "--enclosure <id>" argument.' >&2
            exit $STATE_OK
	fi
        ;;
--enclosure=?*)
	# echo "Setting enclosure to ${1#*=}"
	ENCLOSURE=${1#*=}
	;;
--disk)
	if [ "$2" ]; then
	    # echo "Setting disk to $2"
    	    DISK=$2
            shift 2
            continue
        else
    	    echo 'ERROR: Must specify a non-empty "--disk <id>" argument.' >&2
            exit $STATE_OK
	fi
        ;;
--disk=?*)
	# echo "Setting disk to ${1#*=}"
	DISK=${1#*=}
	;;
--check=?*)
	# echo "Setting check to ${1#*=}"
	OPERATION=${1#*=}
	;;
--)     # End of all options.
	shift
	break
        ;;
-?*)
	echo -e "WARN: Unknown option (ignored): $1" >&2
	;;
*)      # Default case: If no more options then break out of the loop.
	break
	;;
    esac

shift
done

case $OPERATION in
power|POWER|showpower)
	/usr/bin/expect -i > $TFILE << EOM
spawn ssh $HOST -l $LOGIN
expect "password*"
send "$PASSWORD\r"
expect "*$LOGIN:/>"
send "showpower\r"
expect "*$LOGIN:/>"
send "exit\r"
EOM

command='
    BEGIN { x = 0; fail = 0; error = "" }
    {
        if ( x == 1 && $2 != "" )
        {
            if ( $3 != "Normal" ) { fail = 1; error = error " " $1 ":" $2 " " $3 }
        }
	if ( x == 1 && $0 ~ /====/) { x = 2 }
        if ( $1 == "Enclosure" ) { x = 1 }
    }
    END { if ( fail == 0 ) { print "OK Normal\n" } else { print "CRITICAL ", error; exit 2 } }'

	cat $TFILE | awk "$command"
	STATE=$?
	;;
disk|DISK|showdisk)
	/usr/bin/expect -i > $TFILE << EOM
spawn ssh $HOST -l $LOGIN
expect "password*"
send "$PASSWORD\r"
expect "*$LOGIN:/>"
send "showdisk -e $ENCLOSURE -s $DISK\r"
expect "*$LOGIN:/>"
send "exit\r"
EOM

command='
    BEGIN { x = 0; fail = 0; msg = ""; error = "" }
    {
        if ( x == 1 && $2 != "" )
        {
            gsub(/[^A-Za-z0-9 \t(,)-]/, "");
            $1 = $1

            if ( $2 == "Location" || $1 == "Running" || $1 ~ /Temperature/ || $1 == "Reconstruction" || $2 == "Status")
            {
                msg = msg "  " $0
            }
            error = error "  " $0
            if ( $2 == "Status" && $3 != "Normal" && $3 != "Free") { fail = 1 }
        }
        if ( x == 1 && $0 ~ /====/) { x = 2 }
        if ( $1 == "Disk" ) { x = 1 }
    }
    END { if ( fail == 0 ) { print "OK Normal ", msg, " \n" } else { print "CRITICAL ", error; exit 2 } }'

	cat $TFILE | awk "$command"
	STATE=$?
	;;
controller|CONTROLLER|showcontroller)
        /usr/bin/expect -i > $TFILE << EOM
spawn ssh $HOST -l $LOGIN
expect "password*"
send "$PASSWORD\r"
expect "*$LOGIN:/>"
send "showcontroller\r"
expect "*$LOGIN:/>"
send "exit\r"
EOM

command='
    BEGIN { x = 0; fail = 0; error = "" }
    {
        if ( x == 1 && $2 == "Status" && $1 != "Primary/Secondary" )
        {
	    gsub(/[^A-Za-z0-9 \t|]/, "");
            if ( $4 != "Normal" ) { fail = 1; error = error " " $1 " " $2 " " $4 " " }
        }
        if ( x == 1 && $0 ~ /====/) { x = 2 }
        if ( $1 == "Controller" ) { x = 1 }
    }
    END { if ( fail == 0 ) { print "OK Normal\n" } else { print "CRITICAL ", error; exit 2 } }'

	cat $TFILE | awk "$command"
	STATE=$?
	;;
coffer|COFFER|showcoffer)
	/usr/bin/expect -i > $TFILE << EOM
spawn ssh $HOST -l $LOGIN
expect "password*"
send "$PASSWORD\r"
expect "*$LOGIN:/>"
send "showcofferdisk\r"
expect "*$LOGIN:/>"
send "exit\r"
EOM

command='
    BEGIN { x = 0; fail = 0; error = "" }
    {
        if ( x == 1 && $2 != "" )
        {
            if ( $2 != "Normal" ) { fail = 1; error = error " " $1 ":" $2 " " $3 }
        }
        if ( x == 1 && $0 ~ /====/) { x = 2 }
        if ( $1 == "Disk" ) { x = 1 }
    }
    END { if ( fail == 0 ) { print "OK Normal\n" } else { print "CRITICAL ", error; exit 2 } }'

	cat $TFILE | awk "$command"
	STATE=$?
	;;
alarms|ALARMS|showalarms)
	/usr/bin/expect -i > $TFILE << EOM
spawn ssh $HOST -l $LOGIN
expect "password*"
send "$PASSWORD\r"
expect "*$LOGIN:/>"
send "showalarm\r"
expect "*$LOGIN:/>"
send "exit\r"
EOM

command='
    BEGIN { x = 0; fail = 0; error = "" }
    {
        if ( x == 1 && $2 != "" )
        {
            fail = 1; error = error " " $0
        }
        if ( x == 1 && $0 ~ /====/) { x = 2 }
        if ( $1 == "Sequence" ) { x = 1 }
    }
    END { if ( fail == 0 ) { print "OK Normal\n" } else { print "CRITICAL ", error; exit 2 } }'

	cat $TFILE | awk "$command"
	STATE=$?
	;;
esac

rm $TFILE
exit $STATE
