#!/bin/bash ### Curby's Connection Tracker ### by Michael Lee ### v1.00.00, 2007-12-23 ### License # # Copyright (C) 2007 Michael Lee # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # This script tracks network connectivity via pings to PING_TARGET with a # timeout of PING_TIMEOUT seconds. PING_TIMEOUT=1 PING_TARGET="67.18.176.31" # Failure tracking: # Upon a failed ping, the state becomes "warning" and the next WARNING_COUNT # pings are sent WARNING_PERIOD seconds apart. If WARNING_THRESHOLD or more # pings fail, state becomes "down", and pings are sent DOWN_PERIOD apart. WARNING_COUNT=10 WARNING_PERIOD=1 WARNING_THRESHOLD=3 DOWN_PERIOD=1 # Success tracking: # Upon returning from down or warning, state becomes "recover" and the next # RECOVER_COUNT pings are sent RECOVER_PERIOD seconds apart. If no failures, # the state becomes "stabilize" and the next STABILIZE_COUNT pings are sent # STABILIZE_PERIOD seconds apart. Finally, the state becomes "up" and pings # are sent UP_PERIOD seconds apart. RECOVER_COUNT=10 RECOVER_PERIOD=1 STABILIZE_COUNT=6 STABILIZE_PERIOD=10 UP_PERIOD=60 # Setup up initial state state="RECOVER" laststate="DOWN" echo `date +%s`" ("`date`") starting up in state $state, pinging $PING_TARGET" # Run forever while true; do if ping -c 1 -t $PING_TIMEOUT $PING_TARGET > /dev/null 2>&1 ; then # Pings work case "$state" in # Stay UP "UP" ) sleep $UP_PERIOD ;; "WARNING" ) echo -n "." tempcount=$((tempcount+1)) if [[ "$tempcount" -ge "$WARNING_COUNT" ]]; then if [[ "$laststate" == "DOWN" ]]; then state="RECOVER" else state=$laststate fi echo "]" echo `date +%s`" ("`date`") WARNING --> $state (after $failcount failures)" sleep $UP_PERIOD else sleep $WARNING_PERIOD fi ;; "DOWN" ) state="RECOVER" tempcount=1 echo `date +%s`" ("`date`") DOWN --> RECOVER" sleep $RECOVER_PERIOD ;; "RECOVER" ) tempcount=$((tempcount+1)) if [[ "$tempcount" -ge "$RECOVER_COUNT" ]]; then state="STABILIZE" tempcount=0 echo `date +%s`" ("`date`") RECOVER --> STABILIZE" sleep $STABILIZE_PERIOD else sleep $RECOVER_PERIOD fi ;; "STABILIZE" ) tempcount=$((tempcount+1)) if [[ "$tempcount" -ge "$STABILIZE_COUNT" ]]; then state="UP" echo `date +%s`" ("`date`") STABILIZE --> UP" sleep $UP_PERIOD else sleep $STABILIZE_PERIOD fi ;; * ) echo "uh-oh - unknown case for good ping '$state'";exit ;; esac else # Pings fail case "$state" in "RECOVER"|"STABILIZE"|"UP" ) tempcount=1 failcount=1 echo -n `date +%s`" ("`date`") $state --> WARNING [X" laststate=$state state="WARNING" sleep $WARNING_PERIOD ;; "WARNING" ) echo -n "X" tempcount=$((tempcount+1)) failcount=$((failcount+1)) if [[ "$failcount" -ge "$WARNING_THRESHOLD" ]]; then state="DOWN" echo "]" echo `date +%s`" ("`date`") WARNING --> DOWN ($WARNING_THRESHOLD failures after $tempcount packets)" sleep $DOWN_PERIOD else sleep $WARNING_PERIOD fi ;; "DOWN" ) sleep $DOWN_PERIOD ;; * ) echo "uh-oh - unknown case for bad ping '$state'" ;; esac fi done