#! /bin/sh
######################################################################
# Name: check_esxcli_hparray
# By: Copyright (C) 2012 iceburn
# Credits to: andreiw, Magnus Glantz
######################################################################
# Licence: GPL 2.0
######################################################################
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
######################################################################
# Description:
#
#  A Nagios plugin that checks HP Proliant hardware raid via the 
# VMWare ESXCLI tool on ESXi servers with the HP Util Blunde installed.
#
#
# This is based on the check_hparray plugin but with the checking
# order corrected in order to avoid false positives.
#
######################################################################

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=v0.9
ESXCLI=/usr/bin/esxcli

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

print_usage() {
        echo ""
        echo "Usage: $PROGNAME -h <host> -u <username> -p <password> -s <slot-number>"
        echo "Usage: $PROGNAME [--help]"
        echo "Usage: $PROGNAME [-V | --version]"
        echo ""
}

print_help() {
        print_revision $PROGNAME $REVISION
        echo ""
        print_usage
        echo ""
        echo "This plugin checks hardware status for HP Proliant running ESXi 5.0u1 servers using ESXCLI utility."
        echo ""
        exit 0
}

print_revision() {
	echo $1" "$2
}

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

check_raid()
{
        raid_ok=`echo $check|grep -i ok|wc -l`
        raid_warning=`echo $check|grep -i rebuild|wc -l`
        raid_critical_1=`echo $check|grep -i failed|wc -l`
        raid_critical_2=`echo $check|grep -i recovery|wc -l`

        err_check=`expr $raid_ok + $raid_warning + $raid_critical_1 + $raid_critical_2`

        if [ $err_check -eq "0" ]; then
                checkm=`echo $check|sed -e '/^$/ d'`
                echo "$PROGNAME Error. $checkm"
                exit 2
        fi

        if [ $raid_critical_1 -ge "1" ]; then
                exit_status=$STATE_CRITICAL
        elif [ $raid_critical_2 -ge "1" ]; then
                exit_status=$STATE_CRITICAL
        elif [ $raid_warning -ge "1" ]; then
                exit_status=$STATE_WARNING
        elif [ $raid_ok -eq "0" ]; then
		exit_status=$STATE_UNKNOWN	
	else
                exit_status=$STATE_OK
        fi

        if [ $exit_status -eq "0" ]; then
                msg_ok=`echo $check|grep -i ok`
                echo "RAID OK - ($msg_ok)"
                exit $exit_status
        elif [ $exit_status -eq "1" ]; then
                msg_warning=`echo $check|grep -i rebuild`
                echo "RAID WARNING - ($msg_warning)"
                exit $exit_status
        elif [ $exit_status -eq "2" ]; then
                msg_critical1=`echo $check|grep -i failed`
                msg_critical2=`echo $check|grep -i recovery`
                echo "RAID CRITICAL - ($msg_critical1 $msg_critical2)"
                exit $exit_status
        fi
}


case "$1" in
        --help)
                print_help
                exit 0
                ;;
        --version)
                print_revision $PROGNAME $REVISION
                exit 0
                ;;
        -V)
                print_revision $PROGNAME $REVISION
                exit 0
                ;;
        -h)
                check=`$ESXCLI -s $2 -u $4 -p $6 hpacucli cmd -q "controller slot=$8 ld all show"`
                check_raid
                ;;
        *)
                print_usage
                ;;
esac
