#!/bin/bash # # copyleft Whit Blauvelt 17 Sept. 2012 # # This script sends 5 TCP SYN pings to a host and compares critical and # warning tresholds against average round trip time (ms). It also reports # critical on 100% packet loss. The user (e.g. "nagios") must have sudoers # permission to run hping3. The port should be that used by some service on # the host. # ########################################################################### VERSION="check_hping v0.01" # edit to match location on system; hping2 should also work HPING="/usr/sbin/hping3" usage() { cat << EOF usage: $0 [host] [warning max rtt] [critical max rtt] [port] This script sends 5 TCP SYN pings to a host and compares critical and warning tresholds against average round trip time (ms). It also reports critical on 100% packet loss. The user (e.g. "nagios") must have sudoers permission to run hping3. The port should be that used by some service on the host. EOF } RET="0" if [ ! -n "$1" ] then echo "- Missing [host]" RET="1" fi if [ ! -n "$2" ] then echo "- Missing [warning max rtt]" RET="1" fi if [ ! -n "$3" ] then echo "- Missing [critical max rtt]" RET="1" fi if [ ! -n "$4" ] then echo "- Missing [port]" RET="1" fi if [ $RET == "1" ] then usage exit 3 fi PRE=`sudo ${HPING} -q -n -p $4 -c 5 -S $1 2>&1` AVG=`echo $PRE | awk -F/ '{ print $4 }' | awk 'BEGIN{RS=FS="";OFS=""} $1=$1' | awk -F "." '{ print $1 }'` PKT=`echo $PRE | awk -F, '{ print $3 }' | awk -F " " '{ print $1 }'` if [ ! -n "$AVG" ] then if [ $PKT == "100%" ] then echo "CRITICAL: packet loss $PKT" else echo "CRITICAL: Error pinging" fi exit 2 elif [ $AVG -le "$2" ] then SC="OK" EX=0 elif [ $AVG -le "$3" ] then SC="WARNING" EX=1 else SC="CRITICAL" EX=2 fi echo "$SC: Average response time $AVG ms; packet loss $PKT" exit $EX