Simple up check of ip address

This script simply pings the given ip-address and every time there is a change in response status it shows the date and the status. This is nice to keep track of conections that flap a lot.


#!/bin/bash

if [ "$#" -ne 1 ]; then
  echo "Usage: $0 <host>"
  exit
fi

HOST=$1

trap ctrl_c INT
function ctrl_c() {
  echo
  echo "$STATUS $DATE, $HOST: Total=$totalCount, Errors=$errorCount => $errorPercentage% failed"
  exit
}

totalCount=0
errorCount=0
previousPingStatus=-1
while [ 1 ]; do
  totalCount=$(( $totalCount + 1 ))
  ping -c 1 -W 1 -q $HOST > /dev/null
  pingStatus=$?
  if [ $pingStatus -ne 0 ]; then
    errorCount=$(( $errorCount + 1 ))
  fi
  errorPercentage=$(( $errorCount * 100 / $totalCount ))
  DATE=`date +%Y-%m-%d %H:%M`
  if [ $previousPingStatus -ne $pingStatus ]; then
    STATUS="ERROR"
    if [ $pingStatus -eq 0 ]; then
      STATUS="OK   "
    fi
    echo "$STATUS $DATE, $HOST: Total=$totalCount, Errors=$errorCount => $errorPercentage% failed"
  fi
  previousPingStatus=$pingStatus
  if [ $pingStatus -eq 0 ]; then
    sleep 1
  fi
done