Commit cdb700b5 authored by Vitaly Lipatov's avatar Vitaly Lipatov

refactoring check_connectivity script

parent 5eb57fee
...@@ -2,8 +2,20 @@ ...@@ -2,8 +2,20 @@
cd $(dirname $0) cd $(dirname $0)
write_log()
{
echo "$*" | tee -a /var/log/check_connectivity.log | mail -s "Etersoft router state is changed" vitaly.lipatov@gmail.com
}
fatal()
{
write_log "$(date) FATAL: $@"
exit 1
}
if [ "$1" = "stop" ] ; then if [ "$1" = "stop" ] ; then
echo "$(date) Watcher is stopped" >>/var/log/check_connectivity.log write_log "$(date) Watcher is stopped"
kill $(cat /var/run/check_connectivity.pid) kill $(cat /var/run/check_connectivity.pid)
rm -f /var/run/check_connectivity.pid rm -f /var/run/check_connectivity.pid
exit exit
...@@ -12,7 +24,7 @@ fi ...@@ -12,7 +24,7 @@ fi
# ya.ru google.com # ya.ru google.com
HOSTS="93.158.134.3 173.194.71.139" HOSTS="93.158.134.3 173.194.71.139"
# return false if both failed # return false if all targets failed
check_hosts() check_hosts()
{ {
local iface="$1" local iface="$1"
...@@ -32,67 +44,68 @@ check_bird() ...@@ -32,67 +44,68 @@ check_bird()
STATE=both STATE=both
set_state() get_state()
{ {
# Ekvant failed SHEKVANT=
if ! check_hosts 212.176.192.225 ; then SHPROMETEY=
NEWSTATE=prometey check_hosts 212.176.192.225 && SHEKVANT=ok
return check_hosts 85.235.198.26 && SHPROMETEY=ok
fi
[ "$SHEKVANT$SHPROMETEY" = "okok" ] && NEWSTATE=both && return
# Prometey [ "$SHEKVANT$SHPROMETEY" = "" ] && NEWSTATE=none && return
if ! check_hosts 85.235.198.26 ; then [ "$SHEKVANT" = "ok" ] && NEWSTATE=ekvant && return
NEWSTATE=ekvant [ "$SHPROMETEY" = "ok" ] && NEWSTATE=prometey && return
return fatal "No such state!"
fi
NEWSTATE=both
} }
set_state_by_bgp() get_state_by_bgp()
{ {
# check bgp only have both uplink works
[ "$NEWSTATE" = "both" ] || return [ "$NEWSTATE" = "both" ] || return
local SEKVANT= SBEKVANT=
local SPROMETEY= SBPROMETEY=
check_bird bgpAS2854 && SEKVANT=ok check_bird bgpAS2854 && SBEKVANT=ok
check_bird bgpAS35000 && SPROMETEY=ok check_bird bgpAS35000 && SBPROMETEY=ok
[ "$SEKVANT$SPROMETEY" = "okok" ] && return [ "$SBEKVANT$SBPROMETEY" = "okok" ] && return
[ "$SEKVANT$SPROMETEY" = "" ] && NEWSTATE=none && return [ "$SBEKVANT$SBPROMETEY" = "" ] && NEWSTATE=none && return
[ "$SEKVANT" = "ok" ] && NEWSTATE=ekvant && return [ "$SBEKVANT" = "ok" ] && NEWSTATE=ekvant && return
[ "$SPROMETEY" = "ok" ] && NEWSTATE=prometey && return [ "$SBPROMETEY" = "ok" ] && NEWSTATE=prometey && return
fatal "No such bgp state!"
}
apply_state()
{
case "$1" in
prometey)
./only_prometey.sh
;;
ekvant)
./only_ekvant.sh
;;
both)
./both_provs.sh
;;
none)
write_log "Sorry, no connection!"
;;
*)
fatal "Unknown state"
;;
esac
} }
main_cycle() main_cycle()
{ {
./both_provs.sh ./both_provs.sh
echo "$(date) Start with both state" >>/var/log/check_connectivity.log write_log "$(date) Start with both state"
while true ; do while true ; do
set_state get_state
set_state_by_bgp get_state_by_bgp
if [ "$STATE" != "$NEWSTATE" ] ; then if [ "$STATE" != "$NEWSTATE" ] ; then
apply_state "$NEWSTATE"
case "$NEWSTATE" in write_log "$(date) State changed to $NEWSTATE (Ekvant: $SHEKVANT-$SBEKVANT, Prometey: $SHPROMETEY-$SBPROMETEY)"
prometey)
./only_prometey.sh
;;
ekvant)
./only_ekvant.sh
;;
both)
./both_provs.sh
;;
none)
echo "Sorry, no connection!" >&2
;;
*)
echo "Unknown state" >&2
exit 1
;;
esac
echo "$(date) State changed to $NEWSTATE" >>/var/log/check_connectivity.log
STATE=$NEWSTATE STATE=$NEWSTATE
fi fi
sleep 7 sleep 7
......
...@@ -6,6 +6,7 @@ ip route replace default via 212.176.192.226 ...@@ -6,6 +6,7 @@ ip route replace default via 212.176.192.226
# Ekvant exports default to us, but we will repeat it # Ekvant exports default to us, but we will repeat it
ip route replace default scope global table common via 212.176.192.226 ip route replace default scope global table common via 212.176.192.226
# TODO: enable shaper for torrent and our remote backup servers
# remove hops for torrent client # remove hops for torrent client
ip rule del from 91.232.225.21 lookup hop ip rule del from 91.232.225.21 lookup hop
ip rule del from 91.232.225.21 lookup hop ip rule del from 91.232.225.21 lookup hop
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment