Commit cdb700b5 authored by Vitaly Lipatov's avatar Vitaly Lipatov

refactoring check_connectivity script

parent 5eb57fee
......@@ -2,8 +2,20 @@
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
echo "$(date) Watcher is stopped" >>/var/log/check_connectivity.log
write_log "$(date) Watcher is stopped"
kill $(cat /var/run/check_connectivity.pid)
rm -f /var/run/check_connectivity.pid
exit
......@@ -12,7 +24,7 @@ fi
# ya.ru google.com
HOSTS="93.158.134.3 173.194.71.139"
# return false if both failed
# return false if all targets failed
check_hosts()
{
local iface="$1"
......@@ -32,67 +44,68 @@ check_bird()
STATE=both
set_state()
get_state()
{
# Ekvant failed
if ! check_hosts 212.176.192.225 ; then
NEWSTATE=prometey
return
fi
# Prometey
if ! check_hosts 85.235.198.26 ; then
NEWSTATE=ekvant
return
fi
NEWSTATE=both
SHEKVANT=
SHPROMETEY=
check_hosts 212.176.192.225 && SHEKVANT=ok
check_hosts 85.235.198.26 && SHPROMETEY=ok
[ "$SHEKVANT$SHPROMETEY" = "okok" ] && NEWSTATE=both && return
[ "$SHEKVANT$SHPROMETEY" = "" ] && NEWSTATE=none && return
[ "$SHEKVANT" = "ok" ] && NEWSTATE=ekvant && return
[ "$SHPROMETEY" = "ok" ] && NEWSTATE=prometey && return
fatal "No such state!"
}
set_state_by_bgp()
get_state_by_bgp()
{
# check bgp only have both uplink works
[ "$NEWSTATE" = "both" ] || return
local SEKVANT=
local SPROMETEY=
check_bird bgpAS2854 && SEKVANT=ok
check_bird bgpAS35000 && SPROMETEY=ok
[ "$SEKVANT$SPROMETEY" = "okok" ] && return
[ "$SEKVANT$SPROMETEY" = "" ] && NEWSTATE=none && return
[ "$SEKVANT" = "ok" ] && NEWSTATE=ekvant && return
[ "$SPROMETEY" = "ok" ] && NEWSTATE=prometey && return
SBEKVANT=
SBPROMETEY=
check_bird bgpAS2854 && SBEKVANT=ok
check_bird bgpAS35000 && SBPROMETEY=ok
[ "$SBEKVANT$SBPROMETEY" = "okok" ] && return
[ "$SBEKVANT$SBPROMETEY" = "" ] && NEWSTATE=none && return
[ "$SBEKVANT" = "ok" ] && NEWSTATE=ekvant && 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()
{
./both_provs.sh
echo "$(date) Start with both state" >>/var/log/check_connectivity.log
write_log "$(date) Start with both state"
while true ; do
set_state
set_state_by_bgp
get_state
get_state_by_bgp
if [ "$STATE" != "$NEWSTATE" ] ; then
case "$NEWSTATE" in
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
apply_state "$NEWSTATE"
write_log "$(date) State changed to $NEWSTATE (Ekvant: $SHEKVANT-$SBEKVANT, Prometey: $SHPROMETEY-$SBPROMETEY)"
STATE=$NEWSTATE
fi
sleep 7
......
......@@ -6,6 +6,7 @@ ip route replace default via 212.176.192.226
# Ekvant exports default to us, but we will repeat it
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
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