contrib/freifunk-gwcheck: check which interface is wan when we get the upstream dns servers, add priorities to logger calls, check for dyngw_plain plugin before checking if script already runs, add license/copyright

This commit is contained in:
Manuel Munz 2013-01-19 20:32:06 +00:00
parent b59375ccc5
commit c9525b9b22

View file

@ -1,28 +1,28 @@
#!/bin/sh #!/bin/sh
# Copyright 2013 Manuel Munz <freifunk at somakoma dot de>
# Licensed under the GNU General Public License (GPL) v3
# This script monitors the local internet gateway
. /lib/functions/network.sh . /lib/functions/network.sh
# exit if dyngw_plain is not installed or enabled
dgwlib=`uci show olsrd |grep dyn_gw_plain |awk {' FS="."; print $1"."$2 '}`
if [ -n "$dgwlib" ]; then
if [ "$(uci -q get $dgwlib.ignore)" == 1 ]; then
exit 1
fi
else
exit 1
fi
#Exit if this script is already running #Exit if this script is already running
pid="$(pidof ff_olsr_test_gw.sh)" pid="$(pidof ff_olsr_test_gw.sh)"
if [ ${#pid} -gt 5 ]; then if [ ${#pid} -gt 5 ]; then
logger -t gwcheck "Gateway check script is already running, exit now" logger -p debug -t gwcheck "Gateway check script is already running, exit now"
exit 1 exit 1
fi fi
#check if dyngw_plain is installed and enabled, else exit # exit if there is no defaultroute with metric=0 in main or gw-check table.
dyngwplainlib=`uci show olsrd |grep dyn_gw_plain |awk {' FS="."; print $1"."$2 '}`
if [ -n "$dyngwplainlib" ]; then
if [ "$(uci -q get $dyngwplainlib.ignore)" == 1 ]; then
exit 1
fi
else
echo "dyngw_plain not found in olsrd config, exit"
exit 1
fi
# check if we have a defaultroute with metric=0 in one of these tables: main table and gw-check table.
# If not exit here.
defroutemain="$(ip r s |grep default |grep -v metric)" defroutemain="$(ip r s |grep default |grep -v metric)"
defroutegwcheck="$(ip r s t gw-check |grep default |grep -v metric)" defroutegwcheck="$(ip r s t gw-check |grep default |grep -v metric)"
if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then
@ -56,7 +56,7 @@ check_internet() {
echo 0 echo 0
break break
else else
logger -t gw-check "Could not get test file from http://$t/conntest.html" logger -p debug -t gw-check "Could not fetch http://$t/conntest.html"
fi fi
done done
} }
@ -66,54 +66,45 @@ resolve() {
} }
get_dnsservers() { get_dnsservers() {
# this gets all dns servers for the wan interface. If ubus is not present (like on older # this gets all dns servers for the interface which has the default route
# openwrt versions before Attitude fallback to get these from /var/state/network.
dns="" dns=""
if [ -x /bin/ubus ]; then if [ ! -x /bin/ubus ]; then
network_get_dnsserver dns wan # ubus not present (versions before Attitude): fallback to get these from /var/state/network.
else # We always assume that wan is the default route interface here
dns="$(grep network.wan.resolv_dns /var/state/network | cut -d "=" -f 2)" dns="$(grep network.wan.resolv_dns /var/state/network | cut -d "=" -f 2)"
else
network_find_wan wan
network_get_dnsserver dns $wan
fi fi
} }
iw=$(check_internet) iw=$(check_internet)
if [ "$iw" == 0 ]; then if [ "$iw" == 0 ]; then
# check if we have a seperate routing table for our tests. # Internet available again, restore default route and remove ip rules
# If yes, move defaultroute to normal table and delete table gw-check
# Also delete ip rules to use table gw-check for our testhosts and wan dns servers
if [ -n "$defroutegwcheck" ]; then if [ -n "$defroutegwcheck" ]; then
ip r a $defroutegwcheck ip r a $defroutegwcheck
ip r d $defroutegwcheck t gw-check ip r d $defroutegwcheck t gw-check
ip ru del fwmark 0x2 lookup gw-check
for host in $testserver; do for host in $testserver; do
ips="$(resolve $host)" ips="$(resolve $host)"
for ip in $ips; do for ip in $ips; do
[ -n "$(ip ru s | grep "to $ip lookup gw-check")" ] && ip rule del to $ip table gw-check [ -n "$(ip ru s | grep "to $ip lookup gw-check")" ] && ip rule del to $ip table gw-check
done done
done done
get_dnsservers get_dnsservers
for d in $dns; do for d in $dns; do
[ -n "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule del to $d table gw-check [ -n "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule del to $d table gw-check
done done
logger -p err -t gw-check "Internet is available again, default route restored ( $defroutegwcheck)"
#ip r d default via 127.0.0.1 metric 100000
logger -t gw-check "Internet is available again, restoring default route ( $defroutegwcheck)"
fi fi
else else
# Check failed. If we have a defaultroute with metric=0 and it is already in table gw-check then do nothing. # Check failed. Move default route to table gw-check and setup ip rules.
# If there is a defaultroute with metric=0 then remove it from the main routing table and add to table gw-check.
# Also setup ip rules to use table gw-check for our testhosts and wan dns servers
if [ -z "$(ip ru s | grep gw-check)" -a -n "$defroutemain" ]; then if [ -z "$(ip ru s | grep gw-check)" -a -n "$defroutemain" ]; then
ip rule add fwmark 0x2 lookup gw-check
ip r a $defroutemain table gw-check ip r a $defroutemain table gw-check
ip r d $defroutemain ip r d $defroutemain
logger -p err -t gw-check "Internet is not available, default route deactivated ( $defroutemain)"
fi fi
for host in $testserver; do for host in $testserver; do
ips="$(resolve $host)" ips="$(resolve $host)"
@ -125,6 +116,5 @@ else
for d in $dns; do for d in $dns; do
[ -z "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule add to $d table gw-check [ -z "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule add to $d table gw-check
done done
#ip r a default via 127.0.0.1 metric 100000 logger -p err -t gw-check "Check your internet connection!"
logger -t gw-check "Internet is not available, deactivating the default route ( $defroutemain)"
fi fi