#
# This script can be run to notify someone when
#important connections go down.
#
# Edit the email addresses list and the list of IPs to ping.
# The script counts the number of IPs, but see the part where
# it sends emails to change number of emails sent.
#
email_1="alton@alton-moore.net"
email_2="9565551212@messaging.nextel.com"
#
#------------------------------------------
#
# Specify the IP to ping and the number of seconds before we report it down.
ping_ip_1="123.123.123.123"; ping_timeout_1=60
ping_ip_2="12.34.56.78";     ping_timeout_2=60
ping_ip_3="192.168.1.6";     ping_timeout_3=60
#
# Initialize the $last_ping_time_# variable for each item.
ip=1
while (true)
  do
  varname=ping_ip_${ip}
  ip_to_ping=${!varname}
  #
  if [ "$ip_to_ping" == "" ]
    then
    break
    fi
  #
  echo "Setting last ping time for IP $ip_to_ping"
  ((last_ping_time_$ip=`date +%s`))
  ((ip += 1))
  done
echo "Done initializing variables."
#
#-----------------------------
while (true)
  do
  echo "Starting a cycle of pings."
  sleep 1
  #
  ip=1
  while (true)
    do
    varname=ping_ip_${ip}
    ip_to_ping=${!varname}
    #
    if [ "$ip_to_ping" == "" ]
      then
      break
      fi
    #
    echo -n "$ip_to_ping: "
    varname=ping_timeout_${ip}
    timeout_seconds=${!varname}
    #
    # Make sure our own connection is up first.
    if ping -c 5 123.123.123.1  >/dev/null
      then
      echo -n "our connection is up; "
     else
      echo -n "our connection is down!"
      ((last_ping_time_$ip=`date +%s`))
      ((ip += 1))
      continue;
      fi
    if ping -c 5 $ip_to_ping  >/dev/null
      then
      echo -n "ping successful; "
      if ((last_ping_time_$ip))
        then
        echo "continuing."
        ((last_ping_time_$ip=`date +%s`))
       else
        echo "notifying that connection is back up."
        echo "******* IP $ip_to_ping is back up! *******" | mail -s "ALTON1 connection_monitor_5" $email_1
        echo "******* IP $ip_to_ping is back up! *******" | mail -s "ALTON1 connection_monitor_5" $email_2
        ((last_ping_time_$ip=`date +%s`))
        fi
      #
     else
      echo -n "ping unsuccessful; "
      if (( (`date +%s` - last_ping_time_$ip) > timeout_seconds))
        then
        if ((last_ping_time_$ip))
          then
          echo "notifying that connection is down."
          echo "******* IP $ip_to_ping is down! *******" | mail -s "ALTON1 connection_monitor_5" $email_1
          echo "******* IP $ip_to_ping is down! *******" | mail -s "ALTON1 connection_monitor_5" $email_2
          ((last_ping_time_$ip=0))
         else
          echo "waiting for this connection to come back up."
          fi
        #
       else
        echo "connection not down long enough to notify."
        fi
      fi
    ((ip += 1))
    done
  done
#
#
