#!/bin/bash
#
# hpacucli check plugin for Nagios
# Last Modified: 20080923
#
# Usage: ./check_hpacucli 
#
# you need a sudoers-entry:    nagios ALL = NOPASSWD: /usr/sbin/hpacucli ctrl all show config

tmpfile="/tmp/hpacucli.txt"
hpacucli="sudo /usr/sbin/hpacucli ctrl all show config"
ok_regex="OK)$"

GREP=`type -p grep` || exit 1
CAT=`type -p cat` || exit 1
SED=`type -p sed` || exit 1
BASENAME=`type -p basename` || echo "command basename not found"
DIRNAME=`type -p dirname` || echo "command dirname not found"

PROGNAME=`${BASENAME} $0`
PROGPATH=`${DIRNAME} $0`
#PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.1 $' | sed -e 's/[^0-9.]//g'`

. $PROGPATH/utils.sh

print_usage() {
    echo "Usage: $PROGNAME "
    echo "Usage: $PROGNAME --help"
    echo "Usage: $PROGNAME --version"
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    print_usage
    echo ""
    echo "hpacucli disk health plugin for Nagios"
    echo ""
}


exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done


${hpacucli} > ${tmpfile}
if [ $? -ne 0 ]; then
	rm ${tmpfile}
	echo "I'am `/usr/bin/whoami` - hpacucli returns an error! Is sudoers configured?"
	exit $STATE_UNKNOWN
fi

${CAT} ${tmpfile} | ${GREP} drive | ${GREP} -v ${ok_regex}
if [ $? -eq 0 ]; then
	exitstatus=$STATE_CRITICAL
fi
${CAT}  ${tmpfile} | ${GREP} drive > /dev/null
if [ $? -eq 0 ]; then
	exitstatus=$STATE_OK
else
	exitstatus=$STATE_WARNING
fi
msg
msg=`${CAT}  ${tmpfile} | ${GREP} drive | ${SED} 's/$/ -- /g'`
rm ${tmpfile}



echo $msg
# Perfdata support
#echo "$msg|errors=${errors};${CRITICAL};${WARNING};0; \n"
exit ${exitstatus}
