#!/usr/bin/ksh # checking TCP connection for group of ports # by G. Stangl, 17.7.2006, 9.12.2008 # ping=/usr/sbin/ping cd /usr/local/nagios/libexec FA=/usr/local/nagios/libexec/check_file_age checkTCP=/usr/local/nagios/libexec/check_tcp checkHTTP=/usr/local/nagios/libexec/check_http PROGNAME=`/bin/basename $0` PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'` REVISION="0.3" print_usage() { echo "Usage: $PROGNAME [-c|-p] -H Host -T|W portlist [-t duration] [-d]" echo " -c performs the check, -p prints stored results" echo " -T portlist may be comma or space separated" echo " -W portlist for Web HTTP checks, comma or space separated" echo " -t duration of testing" echo " -d debug" echo "Usage: $PROGNAME -h" echo "Usage: $PROGNAME -V" } print_help() { echo $PROGNAME $REVISION echo "" echo "Multiple TCP port check plugin for Nagios" echo "" print_usage echo "" } HOST=undef; TCP=undef; MODE=undef; DEBUG=no; WWW=undef DURATION=9 while test -n "$1"; do case "$1" in -h) print_help; exit 3 ;; -d) DEBUG=yes ;; -c) MODE=check ;; -p) MODE=print ;; -t) DURATION=$2 shift ;; -V) echo $PROGNAME $REVISION exit 3 ;; -H) HOST=$2 shift ;; -T) # list of TCP ports, comma or space separated xx=$2 TCP=`echo $xx | tr -s ',' ' '` shift ;; -W) # list of TCP ports, comma or space separated xx=$2 WWW=`echo $xx | tr -s ',' ' '` shift ;; *) echo "Unknown argument: $1" exit 3 ;; esac shift done if [ "$MODE" = "undef" ]; then echo "pls specify MODE -c or -p"; exit 3; fi if [ "$HOST" = "undef" ]; then echo "pls specify -H hostip"; exit 3; fi memory=/usr/local/nagios/tmp/TCP-conn_$HOST.out # echo "mem: $memory" if [ "$MODE" = "check" ]; then if [ "$TCP" = "undef" -a "$WWW" = "undef" ]; then echo "please specify either -T or -W"; exit 3; fi ES=0 GOOD=" port(s) OK: " BAD="" PORTS=$TCP; CH=TCP; if [ "$TCP" = "undef" ]; then PORTS=$WWW; CH=HTTP; fi for port in $PORTS do if [ "$CH" = "TCP" ]; then res=`$checkTCP -H $HOST -p $port -t $DURATION -w 1 -c 2`; exc=$?; fi if [ "$CH" = "HTTP" ]; then res=`$checkHTTP -H $HOST -p $port -t $DURATION -w 1 -c 2 -u /`; exc=$?; fi if [ $DEBUG = "yes" ]; then echo "Result: $exc - $res"; fi result=`echo port $port: $res | cut -d '|' -f 1` if [ $exc -gt 0 ]; then BAD="$BAD port $port: $result, "; ES=2; else GOOD="$GOOD $port,"; fi done if [ "$GOOD" = " port(s) OK: " ]; then GOOD=""; fi msg="$CH Connections to $HOST on${BAD}${GOOD}" echo $msg >$memory echo $ES >>$memory echo $msg exit $ES fi if [ "$MODE" = "print" ]; then if [ ! -r $memory ]; then echo "add '0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/local/nagios/libexec/check_tcp_conn.ksh -H x.x.x.x -C >/dev/null 2>&1' to crontab" exit 3 fi if [ ! -x $FA ]; then echo "Can't execute $FA!"; exit 3; fi age=`$FA -w 1200 -c 1800 -f $memory` AE=`echo $?` if [ $AE -ne 0 ]; then echo $age; exit $AE; fi msg=`cat $memory | head -1` xxx=`cat $memory | tail -1` echo $msg exit $xxx fi