Friday, January 5, 2018

Use ssh instead of ping to check server status

Server Availability cannot always be checked by using ping, since often a secured subnet will not allow traffic back.  That common practice of dropping packets is an attempt to hide servers and provide additional security, but it can make troubleshooting more difficult.

Since ssh is typcially running and responding on any linux servers that need to be managed, check_ssh seems to be more practical than the default check_ping as the check_command for each host definition in Nagios.

This simple shell script uses a Nagios plugin /usr/local/nagios/libexec/check_ssh to quickly check the status of a server.  One thing to note is that a Nagios installation is not required, just the plugins have to be compiled so the binary can be available to the wrapper script.

#!/bin/sh

# checkssh.sh
#
# Author: Scott McClelland
# 2017-03-29
#
# checkssh, uses Nagios plugin check_ssh
# /usr/local/nagios/libexec/check_ssh
#
# nagios-plugins are downloadable from:
# https://www.nagios.org/downloads/nagios-plugins/

if [ "$1" = "" ]
then
echo enter host name
 exit 1
fi
host=$1

checkssh()
{
chkssh=/usr/local/nagios/libexec/check_ssh

$chkssh $host  2>&1 >/dev/null
RESULT=$?

if [ "$RESULT" -eq "0" ]

then
        echo $host : OK
else
        echo $host : DOWN
fi

        return $RESULT
}

Run the command with:


   checkssh $host

To run the command on a group of servers, use a for loop.

$ for s in web0{1..3} db0{1,2} xyz01; do ./chkssh.sh $s;done
web01 : OK
web02 : OK
web03 : DOWN
db01 : OK
db02 : OK
xyz01 : DOWN

$

Related article:  http://systemnotesorg.blogspot.com/2007/12/ping-multiple-hosts-using-bash-nmap-and.html

No comments: