#!/bin/sh

# Simple shell script to check whether chennels to outside world are up.
# Licence is current GNU GPL.
#(c) Light Olli <olli@digger.org.ru>

# timeout between ping's.
pingtimeout=1

# our default gateways
gw1=195.209.37.25
gw2=212.38.105.1

#### output modifiers.
# enable/disable debugging.
debug=1
# whether we wanna see output from ping & other utils.
silence=0
# wether we wanna see human readable output generated by this script
verbose=1

# paths for utils
bc=/usr/bin/bc
ping=/bin/ping

# null device
null=/dev/null

# done (some 2do are still awaiting their time though)
function check_gateways ()
{
 local funcdebug ; funcdebug=0; summ=0;
 for HOST in $gw1 $gw2; do
    if [ "$silence" = "1" ]; then
      if [ "$HOST" = "$gw2" ]; then 
	$ping -i $pingtimeout -c 3 -I 212.38.105.2 $HOST 1>$null 2>$null 3>$null;
	else
	$ping -i $pingtimeout -c 3 -I 195.209.37.26 $HOST 1>$null 2>$null 3>$null;
      fi # [  "$HOST" = "$gw2" ]
     else
      if [ "$HOST" = "$gw2" ]; then 
       $ping -i $pingtimeout -c 3  -I 212.38.105.2 $HOST;
       else $ping -i $pingtimeout -c 3 -I 195.209.37.26 $HOST;
      fi # [ "$HOST" = "$gw2" ]
    fi # [ "$silence" = "1" ]
    result=$?; summ=`echo "$summ + $result" | $bc -l`;
    if [ "$result" = "1" ]; then echo \
     "the gateway $HOST is either unreachable either blocking our pings.";
     if [ "$HOST" = "$gw1" ]; then down=1;
      else if [ "$HOST" = "$gw2" ]; then down=2; fi
     fi
     
     else
      [ "$verbose" = "1" ] && echo -e "\t\tGateway HOST $HOST seem to be OK.\n"
    fi
 done
 [ "$funcdebug" = "1" ] && echo "\$summ=$summ";
 return $summ;
}

# should be silent if param is 0(all OK), should warn if param is 2(both down),
# should warn if param is 1(1 of 2 down)
function send_alert()
{
 #2do: email & syslogd notifications.
    if [ "$1" = "0" ]; then return 0; fi
    
    if [ "$1" = "2" ]; then
     echo -e "\n(!)\tAlert: Both your gateways seem to be down!\n";
    fi
    
    if [ "$1" = "1" ]; then 
     echo -e "\n(!)\tAlert: One of your gateways seem to be down:\n";
     if [ "$down" = "1" ]; then
      echo -e "\n(!)\tThe gateway $gw1 doesn't answer for our ping probes!\n";
     fi
     if [ "$down" = "2" ]; then
      echo -e "\n(!)\tThe gateway $gw2 doesn't answer for our ping probes!\n";
     fi
    fi
}

# Just changes routing. NAT has to be already configured to accept
# connections on both external interface for this to work transparently.
# Elsewhere scripts shell change.
function change_default_link()
{
 # 2do: add some intellect to detect what line is in use currently & if its
 # state didn't change - do nothing.
 if [ "$down" = "1" ]; then
  cp	/etc/sysconfig/network.def_gw=212.38.105.1		\
	/etc/sysconfig/network
  cp	/etc/sysconfig/static-routes.primary=212.38.105.1	\
	/etc/sysconfig/static-routes
  /etc/rc.d/init.d/network restart
  route add default gw 195.209.37.25 metric 1
 fi

 if [ "$down" = "2" ]; then
  cp	/etc/sysconfig/network.def_gw=195.209.37.25		\
	/etc/sysconfig/network
  cp	/etc/sysconfig/static-routes.primary=195.209.37.25	\
	/etc/sysconfig/static-routes
  /etc/rc.d/init.d/network restart
  route add default gw 212.38.105.1 metric 1
 fi
}

############ programm work start.
[ "$verbose" = "1" ] && echo
check_gateways; 
retcode=$?; 
send_alert $retcode;
if [ "$retcode" = "1" ]; then change_default_link; fi
