#!/bin/bash
#
# test_tcp_connect
#
# tcp-Verbindung aufbauen an Hosts, die in test_tcp_connect.conf eingetragen sind
#
if which netcat >/dev/null 2>&1
 then
  NETCAT=netcat
elif which nc >/dev/null 2>&1
 then
  NETCAT=nc
else
  echo "Error: netcat not found."
  exit 1
fi
#
if [[ $# -lt 2 ]]
 then
  DT=`date '+%F %T%:z'`
else
  DT="$1 $2"
fi
#
TMPDAT=/tmp/tcpconnecttest.$$.tmp
export TIMEFORMAT="%3R"
#
if [[ -f /etc/uls/test_tcp_connect.conf ]]
 then
  grep -v '^ *#' /etc/uls/test_tcp_connect.conf | while read H P U D T S
   do
    if [[ "$U" = "." ]]
     then
      U=`hostname`
    fi
    if (time $NETCAT -w 5 -z $H $P >/dev/null 2>&1) 2>$TMPDAT
     then
      send_test_value -h ${U:-$H} ${S:-System} ${T:-TCP-Connect} ${D:-$H} $DT `awk '{printf("%.0f", $1*1000)}' $TMPDAT` ms
      send_test_value -h ${U:-$H} ${S:-System} ${T:-TCP-Connect} ${D:-$H}-Return-Code $DT 0 '#'
    else
      send_test_value -h ${U:-$H} ${S:-System} ${T:-TCP-Connect} ${D:-$H}-Return-Code $DT $? '#'
    fi
  done
fi
rm -f $TMPDAT
