Skip to content

Instantly share code, notes, and snippets.

@adri72
Last active August 29, 2015 13:57
Show Gist options
  • Save adri72/9666869 to your computer and use it in GitHub Desktop.
Save adri72/9666869 to your computer and use it in GitHub Desktop.
Nagios Plugin allowing to check if an host is alive using nmap (i.e. works even if the host doesn't reply to pings)
#!/bin/bash
# Command paths
CMDNMAP=/usr/bin/nmap
CMDGREP=/bin/grep
# Return codes:
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
HELP () {
echo "Usage: check_host_alive_nmap -H [host or ip] [-t timeout]"
exitstatus=$STATE_UNKNOWN
exit $exitstatus
}
while test $# -ne 0; do
case "$1" in
-H)
shift
HOST_TO_SCAN=$1
shift
;;
-t)
shift
HOST_TIMEOUT=$1
shift
;;
-h|--help|*)
shift
HELP
;;
esac
done
# Right number of args?
if [ "x$HOST_TO_SCAN" == "x" ]
then
echo "Usage: check_host_alive_nmap -H [host or ip] [-t timeout]"
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi
# Do we have grep installed?
if [ ! -f "$CMDGREP" ]; then
echo "Error, $CMDGREP doesn't exist."
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi
# Do we have nmap installed?
if [ ! -f "$CMDNMAP" ]; then
echo "Error, $CMDNMAP doesn't exist."
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi
NMAP_RESULT=`$CMDNMAP -sP -P0 $HOST_TO_SCAN | grep -i "host is up"`
if [ ! -z "$NMAP_RESULT" ]
then
echo "OK: $HOST_TO_SCAN - $NMAP_RESULT"
exitstatus=$STATE_OK
exit $exitstatus
else
echo "CRITICAL: $HOST_TO_SCAN - $NMAP_RESULT"
exitstatus=$STATE_CRITICAL
exit $exitstatus
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment