[packages] multiwan: Modified failover, implemented a check to see if resolv.conf.auto is current, added a cache file

git-svn-id: svn://svn.openwrt.org/openwrt/packages@21273 3c298f89-4303-0410-b956-a3cf2f4a3e73
This commit is contained in:
craigc 2010-04-30 16:01:50 +00:00
parent 2a25ce62b5
commit d8c95e808b
3 changed files with 71 additions and 21 deletions

View File

@ -8,7 +8,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=multiwan
PKG_VERSION:=1.0.1
PKG_VERSION:=1.0.2
PKG_RELEASE:=1
include $(INCLUDE_DIR)/package.mk

View File

@ -15,7 +15,7 @@ config 'interface' 'wan'
config 'interface' 'wan2'
option 'weight' '10'
option 'health_interval' '10'
option 'icmp_hosts' 'dns'
option 'icmp_hosts' 'gateway'
option 'timeout' '3'
option 'health_fail_retries' '3'
option 'health_recovery_retries' '5'

View File

@ -18,9 +18,13 @@ failover() {
local failover_to
local failover_to_wanid
local failchk
local recovrychk
local wanid
local existing_failover
failchk=$(query_config failchk $2)
recvrychk=$(query_config recvrychk $2)
wanid=$(query_config wanid $2)
failover_to=`uci -q -P /var/state get multiwan.${2}.failover_to`
failover_to_wanid=$(query_config wanid $failover_to)
@ -28,6 +32,11 @@ failover_to_wanid=$(query_config wanid $failover_to)
existing_failover=$(iptables -n -L FW${wanid}MARK -t mangle | echo $(expr $(wc -l) - 2))
add() {
wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]//g")
wan_fail_map=$(echo $wan_fail_map $1[x])
wan_recovery_map=$(echo $wan_recovery_map | sed -e "s/${1}\[${recvrychk}\]//g")
if [ "$existing_failover" == "2" ]; then
if [ "$failover_to" != "balancer" -a "$failover_to" != "disable" -a "$failover_to_wanid" != "$wanid" ]; then
iptables -I FW${wanid}MARK 2 -t mangle -j FW${failover_to_wanid}MARK
@ -36,13 +45,21 @@ if [ "$existing_failover" == "2" ]; then
fi
fi
mwnote "$1 has failed and is currently offline."
query_config update
}
del() {
wan_recovery_map=$(echo $wan_recovery_map | sed -e "s/${1}\[${recvrychk}\]//g")
wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]//g")
if [ "$existing_failover" == "3" ]; then
iptables -D FW${wanid}MARK 2 -t mangle
fi
mwnote "$1 has recovered and is back online!"
query_config update
}
case $1 in
@ -76,15 +93,17 @@ else
if [ "$new_fail_count" -lt "$health_fail_retries" ]; then
wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]/$1\[${new_fail_count}\]/g")
else
wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]/$1\[x\]/g")
failover add $1
refresh_dns
if [ "$weight" != "disable" ]; then
refresh_loadbalancer
fi
refresh_dns
failover add $1
fi
fi
fi
query_config update
}
recover_wan() {
@ -113,16 +132,16 @@ if [ "$failchk" == "x" ]; then
if [ "$new_recovery_count" -lt "$health_recovery_retries" ]; then
wan_recovery_map=$(echo $wan_recovery_map | sed -e "s/${1}\[${recvrychk}\]/$1\[${new_recovery_count}\]/g")
else
wan_recovery_map=$(echo $wan_recovery_map | sed -e "s/${1}\[${recvrychk}\]//g")
wan_fail_map=$(echo $wan_fail_map | sed -e "s/${1}\[${failchk}\]//g")
failover del $1
refresh_dns
if [ "$weight" != "disable" ]; then
refresh_loadbalancer
fi
refresh_dns
failover del $1
fi
fi
fi
query_config update
}
acquire_wan_data() {
@ -155,10 +174,10 @@ check_old_map=`echo $wan_id_map 2>&1 | grep -o "$1\["`
if [ -z $check_old_map ]; then
wancount=`expr $wancount + 1`
wan_if_map="$wan_if_map $1[${ifname}] "
wan_id_map="$wan_id_map $1[${wancount}] "
wan_gw_map="$wan_gw_map $1[${gateway}] "
wan_ip_map="$wan_ip_map $1[${ipaddr}] "
wan_if_map="$wan_if_map $1[${ifname}]"
wan_id_map="$wan_id_map $1[${wancount}]"
wan_gw_map="$wan_gw_map $1[${gateway}]"
wan_ip_map="$wan_ip_map $1[${ipaddr}]"
else
old_ipaddr=$(query_config ipaddr $1)
old_gateway=$(query_config gateway $1)
@ -176,25 +195,43 @@ check_old_map=`echo $wan_id_map 2>&1 | grep -o "$1\["`
iptables -A MultiWanPostHandler -t mangle -o $ifname -m mark --mark 0x123 -j FW${get_wanid}MARK
fi
if [ "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
failover del $1
iprules_config $get_wanid
qos_init $1 $get_wanid
else
failover add $1
fi
refresh_routes
refresh_loadbalancer
refresh_dns
if [ "$ifname" != "x" -a "$ipaddr" != "x" -a "$gateway" != "x" ]; then
iprules_config $get_wanid
qos_init $1 $get_wanid
failover del $1
else
failover add $1
fi
fi
else
wancount=9
fi
query_config update
}
query_config() {
update_cache_data() {
rm /tmp/.mwan.cache > /dev/null 2>&1
touch /tmp/.mwan.cache
echo "# Automatically Generated by Multi-WAN Agent Script. Do not modify. #" > /tmp/.mwan.cache
echo "wan_if_map=\"$wan_if_map\"" >> /tmp/.mwan.cache
echo "wan_ip_map=\"$wan_ip_map\"" >> /tmp/.mwan.cache
echo "wan_gw_map=\"$wan_gw_map\"" >> /tmp/.mwan.cache
echo "wan_id_map=\"$wan_id_map\"" >> /tmp/.mwan.cache
echo "wan_fail_map=\"$wan_fail_map\"" >> /tmp/.mwan.cache
echo "wan_recovery_map=\"$wan_recovery_map\"" >> /tmp/.mwan.cache
}
case $1 in
update) update_cache_data;;
ifname) echo $wan_if_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
ipaddr) echo $wan_ip_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
gateway) echo $wan_gw_map | grep -o "$2\[\w*.*\]" | awk -F "[" '{print $2}' | awk -F "]" '{print $1}';;
@ -249,6 +286,9 @@ fi
ip route flush cache
rm $jobfile > /dev/null 2>&1
rm $jobfile.work > /dev/null 2>&1
rm /tmp/.mwan.cache > /dev/null 2>&1
exit
}
@ -482,6 +522,8 @@ do
done
fi
done
last_resolv_update=$(ls -l -e /tmp/resolv.conf.auto | awk -F " " '{print $5, $9}')
}
iptables_init() {
@ -873,11 +915,14 @@ bg_task() {
local check_iptables
local queued_task
local bg_counter
local current_resolv_file
bg_counter=0
while [ 1 ]; do
. /tmp/.mwan.cache
if [ "$bg_counter" -eq 5 ]; then
check_iptables=$(iptables -n -L MultiWan -t mangle | grep "references" | awk -F "(" '{print $2}' | cut -d " " -f 1)
@ -888,6 +933,12 @@ check_iptables=$(iptables -n -L MultiWan -t mangle | grep "references" | awk -F
exit
fi
current_resolv_file=$(ls -l -e /tmp/resolv.conf.auto | awk -F " " '{print $5, $9}')
if [ "$last_resolv_update" != "$current_resolv_file" ]; then
refresh_dns
fi
bg_counter=0
fi
@ -937,7 +988,6 @@ gateway=$(query_config gateway $group)
if [ "$ifname" == "x" -o "$ipaddr" == "x" -o "$gateway" == "x" ]; then
failover add $group
wan_fail_map="echo $wan_fail_map $group[x]"
fi
done
}