Table of Contents

Ping / TCP Ping Test

Script to check for ping or TCP in a loop and email a limited number of times an error occurs

#!/bin/bash
#
TIMEOUT=2
PORT=443
MAXEMAILS=40
IP=xxx.xxx.xxx.xxx
EMAIL=someone@example.com
SUBJECT=ping_test
#
errcnt=0
while true; do
  /usr/lib/nagios/plugins/check_tcp -H $IP -p $PORT -t $TIMEOUT
  if [ $? -ne 0 ]
         then
         (( errcnt += 1 ))
         if [ $errcnt -lt $MAXEMAILS ]
           then
           echo "Error Count: $errcnt"|mailx -s $SUBJECT $EMAIL
         else
           echo "Reached max emails"
         fi
     date >> ch_${PORT}_${TIMEOUT}.log
  fi
  sleep 1
done