Load Balancing & Failover Script for Dual/ Multi WAN / ADSL / Cable Connections on Linux with Mail on failure
By Angsuman Chakraborty, Gaea News NetworkWednesday, February 13, 2008
Please read the original article - How To: Load Balancing & Failover With Dual/ Multi WAN / ADSL / Cable Connections on Linux. It contains detailed information with a sample script. Viliam Kočinský modified my script to send email when routing table changes. Also he is starting the script as a service from /etc/init.d. Read below for his enhancements:
I’m using your gwping script. Realy usefull. I added posibility to send email, when routing table change:
# cat /usr/local/sbin/gwping
…….EMAILFROM="root@myserver.example.com"
EMAILTO="someone@example.com"
…..if [[ $CLS1 -eq 0 || $CLS2 -eq 0 ]]; then
if [[ $LLS1 -eq 1 && $LLS2 -eq 0 ]]; then
echo Switching to $NAME2
ip route replace default scope global via $GW2 dev $EXTIF2
LSROUTE=$(ip route show)
echo "$LSROUTE" | mail -s "GWPING - Routing table changed. Line $NAME1 is down." -r $EMAILFROM $EMAILTO
elif [[ $LLS1 -eq 0 && $LLS2 -eq 1 ]]; then
echo Switching to $NAME1
ip route replace default scope global via $GW1 dev $EXTIF1
LSROUTE=$(ip route show)
echo "$LSROUTE" | mail -s "GWPING - Routing table changed. Line $NAME2 is down." -r $EMAILFROM $EMAILTO
elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 ]]; then
echo Restoring default load balancing
#ip route replace default scope global via $GW1 dev $EXTIF1
ip route replace default scope global nexthop via $GW1 dev $EXTIF1 weight $W1 nexthop via $GW2 dev $EXTIF2 weight $W2
LSROUTE=$(ip route show)
echo "$LSROUTE" | mail -s "GWPING - Routing table changed. Both lines are up." -r $EMAILFROM $EMAILTO
fi
fi
sleep $SLEEPTIME
doneAlso I’m starting this script as service from /etc/init.d
# cat /etc/init.d/gwping
#!/bin/sh
# Author: Viliam Kocinsky### BEGIN INIT INFO
# Short-Description: Monitor Internet access connection links and modify kernel route if some link go down.
# Description: This is startup script for gwping script, that manage internet connection failover (if you have at least 2 providers).
# That is, it reguraly ping one IP address
# on internet through certain provider (but trying all) . If ping doesn’t come back, than it addjust kernel route table.
# See inside script for details. This script is from the website:
# https://blog.taragana.com/index.php/archive/how-to-load-balancing-failover-with-dual-multi-wan-adsl-cable-connections-on-linux/
#
### END INIT INFOcommand="$1"
case "$command" in
start)
echo "Starting gwping."
/usr/local/sbin/gwping &
echo $! > /usr/local/share/gwping_pid
;;
stop)
echo "Stoping gwping."
kill -9 $(cat /usr/local/share/gwping_pid) && rm /usr/local/share/gwping_pid
;;
*)
echo "Usage /etc/init.d/gwping start|stop"
;;
esac
May 10, 2010: 7:49 am
Your script is great, i got it working perfectly for normal sites and smtp/pop traffic. |
Ajay Bakshi BSc |
September 15, 2008: 2:23 am
hello, i have researched xrio products and they do wan load balancing. but this is only a generic routing script for distributing wan data through internet connections. thanks. |
martin hill |
Gabriel