#!/bin/bash

CRM_RESOURCE=p_pingnode
CHECK_CMD="/usr/lib/nagios/plugins/check_ping -H %s -w 5000,100%% -c 5000,100%% -p 1"
WARNING=1
CRITICAL=0

HOST_LIST=$( crm resource param $CRM_RESOURCE show host_list )
[ $? -ne 0 ] && echo "UNKNOWN - Can't retreive host_list param of resource $CRM_RESOURCE" && exit 3

DEBUG=0
[ "$1" == "-v" -o "$1" == "-d" ] && DEBUG=1
function debug() {
	[ $DEBUG -eq 1 ] && echo "$1"
}

ok=0
nok=0
nok_list=""
count=0
for host in $HOST_LIST
do
	let count=count+1
	printf -v cmd "$CHECK_CMD" "$host"

	debug "Check $host with command : $cmd"	
	$cmd > /dev/null 2>&1
	res=$?
	debug "done. Result : $res"
	case $res in
		0)
			let ok=ok+1
			;;
		*)
			let nok=nok+1
			nok_list="$nok_list $host"
			;;
	esac
done

if [ $ok -le $CRITICAL ]
then
	msg="CRITICAL - only $ok ping node(s) available (down :$nok_list)"
	ret=2
elif [ $ok -le $WARNING ]
then
	msg="WARNING - only $ok ping node(s) available (down :$nok_list)"
	ret=3
else
	msg="OK - $ok ping node(s) available"
	ret=0
fi

echo "$msg | pingnodes=$ok;$WARNING;$CRITICAL;0;$count"
