#!/bin/sh
#
# HP Smart Array Hardware status plugin for Nagios
# ( With HP Array Configuration Utility CLI hpacucli-x.y.*.rpm )
#
# Written by Simone Rosa (info@simonerosa.it)
# Extended by Carsten Schoene (cs@linux-administrator.com)
#
# Description:
#
# This plugin checks hardware status for Smart Array Controllers,
# using the HP Array Configuration Utility CLI.
# (Array, controller, cache, battery, etc...)
#
#
# Usage: ./check_cciss [-v] [-d] [-e <number>] [-E <name>]
#
#  -v                   = output status and informations about RAID
#  -e <number>          = exclude slot number
#  -E <name>            = exclude chassis name
#  -d                   = use for debug ( command line mode )
#
#
# !!!!!! NOTE: !!!!!!
#
# HP Array Configuration Utility CLI (hpacucli-x.y.*.rpm) need administrator rights.
# Please add this line to /etc/sudoers :
# --------------------------------------------------
# nagios      ALL=NOPASSWD: /usr/sbin/hpacucli
#
# !!!!!!!!!!!!!!!!!!!
#
# NB: Please update the hpacucli to the current version (8.10)
#
# !!!!!!!!!!!!!!!!!!!
#
# Examples:
#
#   ./check_cciss -v
# RAID OK:  Smart Array 6i in Slot 0 array A logicaldrive 1 (67.8 GB, RAID 1+0, OK)
#           [Controller Status: OK  Cache Status: OK  Battery Status: OK]
#
#   ./check_cciss
# RAID OK
#
#
# Another Examples:
#
# RAID CRITICAL - HP Smart Array Failed:  Smart Array 6i in Slot 0 array \
#          A (failed) logicaldrive 1 (67.8 GB, 1+0, Interim Recovery Mode)
#
# RAID WARNING - HP Smart Array Rebuilding:  Smart Array 6i in Slot 0 array \
#           A logicaldrive 1 (67.8 GB, 1+0, Rebuilding)
#
#
# ChangeLog:
#
# 04/06/12 (1.13)
#	   - Added support for kernels greater 3.x (prepend uname26)
#
# 07/10/11 (1.12)
#	   - Added support for newer kernels with hpsa driver
#
# 19/08/11 (1.11)
#	   - Added support to disable chache check (-c)
#
# 10/06/08 (1.8)
#          - Added support for chassis name (example MSA500)
#          - Added arguments to exclude slot number (-e <n>) and chassis name (-E <name>)
#          - Added "Recovering" status reporting
#          - Updated for hpacucli 8.10 version
#
# 07/12/08 (1.7)
#          - Changed command argument support to use getopts and dropped the now unnecessary -N and -c options
#            ( thanks to Reed Loden )
#
# 07/06/08 (1.6)
#          - Added support for multiple arrays
#            ( thanks to Reed Loden )
#
# 05/30/08 (1.5)
#          - Added support for checking cache and battery status; added autodetection of slot number; corrected typo
#            ( thanks to Reed Loden )
#
# 01/25/06 (1.4)
#          - Changed "Rebuilding" grep with "Rebuild" for capture "Ready for Rebuild" status
#            ( thanks to Loris A. )
#
# 12/24/05 (1.3)
#          - Added STATE_UNKNOWN when hpacucli command failed ( if sudo exit with error )
#            ( thanks to Tim Hughes )
#
# 10/10/05 (1.2)
#          - Now it's compatible with "Compaq Smart Array"
#            ( suggested by Hans Engelen )
#
# 10/07/05 (1.1)
#          - Debug messages
#
# 10/06/05 (1.0)
#          - First production version
# 

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

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.8 $' | sed -e 's/[^0-9.]//g'`
DEBUG="0"
VERBOSE="0"
NO_CACHE="0"
HPPROC="/proc/driver/cciss/cciss"
COMPAQPROC="/proc/driver/cpqarray/ida"
HPSASYS="/sys/bus/pci/drivers/hpsa"
if [ ! -x /usr/bin/uname26 ] ; then
	hpacucli="/usr/sbin/hpacucli"
else
	hpacucli="/usr/bin/uname26 /usr/sbin/hpacucli"
fi

. $PROGPATH/utils.sh

print_usage() {
        echo ""
        echo "Usage: $PROGNAME [-v] [-e <number>] [-E <name>] [-c] [-d]"
        echo "Usage: $PROGNAME [-h]"
        echo "Usage: $PROGNAME [-V]"
        echo ""
        echo "  -v                   = output status and informations about RAID"
        echo "  -e <number>          = exclude slot number"
        echo "  -E <name>            = exclude chassis name"
	echo "  -c                   = exclude controller cache check"
        echo "  -d                   = use for debug ( command line mode )"
        echo "  -h                   = help information"
        echo "  -V                   = version information"
        echo ""
        echo " NOTE:"
        echo ""
        echo " HP Array Configuration Utility CLI (hpacucli-x.y.*.rpm) need administrator rights."
        echo " Please add this line to /etc/sudoers"
        echo " --------------------------------------------------"
        echo " nagios      ALL=NOPASSWD: /usr/sbin/hpacucli"
        echo ""
        echo " NB: Please update the hpacucli to the current version (8.10)"
}

print_help() {
        print_revision $PROGNAME $REVISION
        echo ""
        print_usage
        echo ""
        echo "This plugin checks hardware status for Smart Array Controllers, using the HP Array Configuration Utility CLI."
        echo ""
        support
        exit 0
}

while getopts "N:cvde:E:Vh" options
do
    case $options in
      N)  ;;
      c)  NO_CACHE=1;;
      v)  VERBOSE=1;;
      d)  DEBUG=1;;
      e)  EXCLUDE_SLOT=1
          excludeslot="$OPTARG";;
      E)  EXCLUDE_CH=1
          excludech="$OPTARG";;
      V)  print_revision $PROGNAME $REVISION
          exit 0;;
      h)  print_help
          exit 0;;
      \?) print_usage
          exit 0;;
      *)  print_usage
          exit 0;;
  esac
done

raid=`cat $HPPROC* 2>&1`
status=$?
if [ "$DEBUG" = "1" ]; then
        echo ${raid}
fi
if test ${status} -eq 1; then
	if [ -n "` ls -1A $COMPAQPROC* 2>/dev/null`" ] ; then
	        raid=`cat $COMPAQPROC* 2>&1`
        	status=$?
	elif [ -d "$HPSASYS" ] ; then
		status=0
	fi
        if [ "$DEBUG" = "1" ]; then
                echo ${raid}
        fi
        if test ${status} -eq 1; then
                echo "RAID UNKNOWN - HP Smart Array not found"
                exit $STATE_UNKNOWN
        fi
fi

status=$?
if [ "$DEBUG" = "1" ]; then
        echo ${hpacucli}
fi
if test ${status} -eq 1; then
        echo "RAID UNKNOWN - HP Array Utility CLI not found"
        exit $STATE_UNKNOWN
fi

if [ "$NO_CACHE" = "1" ] ; then
	check=`$hpacucli controller all show status | grep -v "Cache Status" 2>&1`
else
	check=`$hpacucli controller all show status 2>&1`
fi

status=$?
if test ${status} -ne 0; then
        echo "RAID UNKNOWN - $hpacucli did not execute properly : "${check}
        exit $STATE_UNKNOWN
fi


if [ "$EXCLUDE_SLOT" = "1" ]; then
        slots=`echo ${check} | egrep -o "Slot \w" | awk '{print $NF}' | grep -v "$excludeslot"`
else
        slots=`echo ${check} | egrep -o "Slot \w" | awk '{print $NF}'`
fi
for slot in $slots
do
        check2b=`$hpacucli controller slot=$slot logicaldrive all show 2>&1`
        status=$?
        if test ${status} -ne 0; then
                echo "RAID UNKNOWN - $hpacucli did not execute properly : "${check2b}
                exit $STATE_UNKNOWN
        fi
        check2="$check2$check2b"
done

if [ "$EXCLUDE_CH" = "1" ]; then
        chassisnames=`echo ${check} | egrep -o "in \w+" | egrep -v "Slot" | awk '{print $NF}' | grep -v "$excludech"`
else
        chassisnames=`echo ${check} | egrep -o "in \w+" | egrep -v "Slot" | awk '{print $NF}'`
fi
for chassisname in $chassisnames
do
        check2b=`$hpacucli controller chassisname="$chassisname" logicaldrive all show 2>&1`
        status=$?
        if test ${status} -ne 0; then
                echo "RAID UNKNOWN - $hpacucli did not execute properly : "${check2b}
                exit $STATE_UNKNOWN
        fi
        check2="$check2$check2b"
done

if echo ${check} | egrep Failed >/dev/null; then
        echo "RAID CRITICAL - HP Smart Array Failed: "${check} | egrep Failed
        exit $STATE_CRITICAL
elif echo ${check} | egrep Disabled >/dev/null; then
        echo "RAID CRITICAL - HP Smart Array Problem: "${check} | egrep Disabled
        exit $STATE_CRITICAL
elif echo ${check2} | egrep Rebuild >/dev/null; then
        echo "RAID WARNING - HP Smart Array Rebuilding: "${check2} | egrep Rebuild
        exit $STATE_WARNING
elif echo ${check2} | egrep Recover >/dev/null; then
        echo "RAID WARNING - HP Smart Array Recovering: "${check2} | egrep Recover
        exit $STATE_WARNING
elif echo ${check2} | egrep Failed >/dev/null; then
        echo "RAID CRITICAL - HP Smart Array Failed: "${check2} | egrep Failed
        exit $STATE_CRITICAL
else
        if [ "$DEBUG" = "1" -o "$VERBOSE" = "1" ]; then
                check3=`echo "${check}" | egrep Status`
                check3=`echo ${check3}`
                echo "RAID OK: "${check2}" ["${check3}"]"
        else
                echo "RAID OK"
        fi
        exit $STATE_OK
fi

exit $STATE_UNKNOWN
