#!/bin/bash
#
################################################################################
#
# check_esx_wbem: A Nagios plugin to check a VMware ESX server's hardware status
#                 via WBEM (Web Based Enterprise Management) by using the SBLIM
#                 wbemcli command.
#
# Version:        1.0
#
# Copyright (C) 2012  Daniel Himler  <d.himler@netsense.at>
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
################################################################################

NAME="$(basename "$0")"
VERSION="1.0"

HOST="localhost"
VERBOSE="no"

print_version () {
	echo "$NAME $VERSION"
	echo "Copyright (C) 2012 Daniel Himler <d.himler@netsense.at>"
	echo "This is free software; see the source for copying conditions.  There is NO"
	echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
}

print_usage() {
	print_version
	echo
	echo "This Nagios plugin checks a VMware ESX server's hardware status via WBEM"
	echo "(Web Based Enterprise Management) by using the SBLIM wbemcli command."
	echo
	echo "Usage:"
	echo "  $(basename "$0") [-H|--host <hostname/ip address>] [-h|--help]"
	echo "    [-P|--password <password>] [-p|--port <port> [-U|--user <username>]"
	echo "    [-S|--ssl] [-V|--version] [-v|--verbose]"
	echo
	echo "Options:"
	echo "  -H|--host <hostname/ip address>"
	echo "     Hostname or IP address to connect to (Default: localhost)"
	echo "  -h|--help"
	echo "     Print detailed help screen"
	echo "  -P|--password <password>"
	echo "     Password to authenticate with (Default: None)"
	echo "  -p|--port <port number>"
	echo "     TCP port to connect to (Default: 5988 / 5989 (SSL))"
	echo "  -S|--ssl"
	echo "     Use SSL encrypted connection"
	echo "  -U|--user <username>"
	echo "     Username to authenticate with (Default: None)"
	echo "  -V|--version"
	echo "     Print version information"
	echo "  -v|--verbose"
	echo "     Show details for command-line debugging"
}

while [ "$#" -gt "0" ]; do
	case "$1" in
		-H|--host)
			if [ -z "$2" ]; then
				print_usage
				exit 3
			else
				HOST="$2"
				shift 2
			fi
			;;
		-h|--help)
			print_usage
			exit 0
			;;
		-P|--password)
			if [ -z "$2" ]; then
				print_usage
				exit 3
			else
				PASSWORD="$2"
				shift 2
			fi
			;;
		-p|--port)
			if [ -z "$2" ]; then
				print_usage
				exit 3
			else
				PORT="$2"
				shift 2
			fi
			;;
		-S|--ssl)
			SSL="yes"
			shift
			;;
		-U|--user)
			if [ -z "$2" ]; then
				print_usage
				exit 3
			else
				USERNAME="$2"
				shift 2
			fi
			;;
		-V|--version)
			print_version
			exit 0
			;;
		-v|--verbose)
			VERBOSE="yes"
			shift
			;;
		*)
			print_usage
			exit 3
			;;
	esac
done

if [ "$#" -gt "0" ]; then
	print_usage
	exit 3
fi

if [ -x "$(type -p "wbemcli" 2> /dev/null)" ]; then
	if [ "$SSL" = "yes" ]; then
		COMMAND="wbemcli -noverify"
		PROTOCOL="https"
		[ -n "$PORT" ] || PORT=5989
	else
		COMMAND="wbemcli"
		PROTOCOL="http"
		[ -n "$PORT" ] || PORT=5988
	fi
	if [ -n "$USERNAME" ]; then
		if [ -n "$PASSWORD" ]; then
			URL="$PROTOCOL://$USERNAME:$PASSWORD@$HOST:$PORT/root/cimv2"
		else
			URL="$COMMAND $PROTOCOL://$USERNAME@$HOST:$PORT/cimv2"
		fi
	else
		URL="$PROTOCOL://$HOST:$PORT/root/cimv2"
	fi
	OUTPUT="$($COMMAND ei $URL:CIM_NumericSensor)"
	RESULT="$?"
	if [ "$RESULT" -ne "0" -a "$RESULT" -ne "139" ]; then
		echo "wbemcli failed to run (error code: $RESULT)."
		exit 3
	fi
else
	echo "Unable to execute wbemcli! Exiting..."
	exit 3
fi

if [ -n "$OUTPUT" ]; then
	GLOBAL_STATUS="0"
	ERRORS=""
	while read LINE; do
		unset HEALTH HEALTHSTATE ELEMENTNAME CURRENTSTATE RESULT STATUS
		eval "$(echo $LINE | sed -e "s/.*,HealthState=\([^,]*\),.*,.*,ElementName=\([^,]*\),.*,CurrentState=\([^,]*\),.*/HEALTHSTATE=\1 ELEMENTNAME=\2 CURRENTSTATE=\3/")"
		case "$HEALTHSTATE" in
			0)	STATUS="WARNING";	HEALTH="Unknown";;
			5)	STATUS="OK";		HEALTH="OK";;
			10)	STATUS="WARNING";	HEALTH="Degraded/Warning";;
			15)	STATUS="WARNING";	HEALTH="Minor Failure";;
			20)	STATUS="CRITICAL";	HEALTH="Major Failure";;
			25)	STATUS="CRITICAL";	HEALTH="Critical Failure";;
			30)	STATUS="CRITICAL";	HEALTH="Non-recoverable Error";;
			*)	STATUS="UNKNOWN";	HEALTH="Unknown";;
		esac
		RESULT="$ELEMENTNAME: $HEALTH ($CURRENTSTATE)"
		[ "$VERBOSE" = "yes" ] && echo "$RESULT"
		if [ "$STATUS" != "OK" ]; then
			ERRORS="$ERRORS${ERRORS:+,} $RESULT"
			case "$STATUS" in
				"CRITICAL")	GLOBAL_STATUS="2";;
				"WARNING")	[ "$GLOBAL_STATUS" -eq "0" -o "$GLOBAL_STATUS" -eq "3" ] && GLOBAL_STATUS="1";;
				*)		[ "$GLOBAL_STATUS" -eq "0" ] && GLOBAL_STATUS="3";;
			esac
		fi
	done << EOF
	$(echo "$OUTPUT")
EOF
	[ "$VERBOSE" = "yes" ] && echo
	case "$GLOBAL_STATUS" in
		0)	echo "WBEM OK: All components are healthy.";;
		1)	echo "WBEM WARNING: Component status degraded:$ERRORS";;
		2)	echo "WBEM CRITICAL: Component status degraded:$ERRORS";;
		*)	echo "WBEM UNKNOWN: Component status unknown:$ERRORS";;
	esac
	exit $GLOBAL_STATUS
else
	echo "wbemcli generated no output!"
	exit 3
fi
