#!/bin/sh down=0 result='' # # Loop while there are command line arguments avaliable, # that is $1, $2, $3, etc... $* starts at $1, $0 is the # script name itself # for host in $* do # Ping, wait up to 5 seconds for a return, only # send 1 ping, host is created from command line, # and redirect the standard output in to /dev/null # and redirect standard error into standard output. ping -W5 -c1 $host >/dev/null 2>&1 # $? is the exit status from the last command # executed, or 'ping' in this case. # # So, if exit code was not zero (success), then... if [ $? != 0 ]; then down=`expr $down + 1` result="$result $host" fi done # Remember to concatenate $result so that if more than one # host is down they'll end up in the result string. result="$result" # So, if $down is greater than 2 we have "plural" hosts down. if [ $down -ge 2 ]; then echo "Return code: $down. $result are down." # Exit the script with '2'. Not 3, 4, etc... as Nagios does # not know what this means. 2 means 'failure' and sets the # service status to 'critical'. exit 2 elif [ $down = 1 ]; then # Only one host down, error message is singular and we exit # to Nagios with '1', which results in a warning state. echo "Return code: $down $result is down." exit 1 else echo "Return code: $down. All hosts are up." exit 0 fi