6bba7f0b0d00fabe604df466dcf95ce159906e8b
[project/luci.git] / contrib / package / freifunk-gwcheck / files / usr / sbin / ff_olsr_test_gw.sh
1 #!/bin/sh
2 # Copyright 2013 Manuel Munz <freifunk at somakoma dot de>
3 # Licensed under the GNU General Public License (GPL) v3
4 # This script monitors the local internet gateway
5
6 . /lib/functions.sh
7 . /lib/functions/network.sh
8
9 # exit if dyngw_plain is not enabled or RtTable is not (254 or unset)
10 config_load olsrd
11
12 check_dyngw_plain()
13 {
14         local cfg="$1"
15         config_get library "$cfg" library
16         if [ "${library#olsrd_dyn_gw_plain}" != "$library" ]; then
17                 config_get ignore "$cfg" ignore
18                 config_get RtTable "$cfg" RtTable
19                 if [ "$ignore" != "1" ] && [ -z "$RtTable" -o "$RtTable" = "254" ]; then
20                         exit=0
21                 fi
22         fi
23 }
24
25 exit=1
26 config_foreach check_dyngw_plain LoadPlugin
27 [ "$exit" = "1" ] && exit 1
28
29 #Exit if this script is already running
30 pid="$(pidof ff_olsr_test_gw.sh)"
31 if [ ${#pid} -gt 5 ]; then
32         logger -p debug -t gwcheck "Gateway check script is already running, exit now"
33         exit 1
34 fi
35
36 # exit if there is no defaultroute with metric=0 in main or gw-check table.
37 defroutemain="$(ip route show |grep default |grep -v metric)"
38 defroutegwcheck="$(ip route show table gw-check |grep default |grep -v metric)"
39 if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then
40         exit 1
41 fi
42
43 # get and shuffle list of testservers
44 testserver="$(uci -q get freifunk-gwcheck.hosts.host)"
45 [ -z "$testserver" ] && echo "No testservers found, exit" && exit
46
47 testserver="$(for t in $testserver; do echo $t; done | awk 'BEGIN {
48         srand();
49 }
50 {
51         l[NR] = $0;
52 }
53
54 END {
55         for (i = 1; i <= NR; i++) {
56                 n = int(rand() * (NR - i + 1)) + i;
57                 print l[n];
58                 l[n] = l[i];
59         }
60 }')"
61
62 check_internet() {
63         for t in $testserver; do
64                 local test
65                 test=$(wget -q http://$t/conntest.html -O -| grep "Internet_works")
66                 if [ "$test" == "Internet_works" ]; then
67                         echo 0
68                         break
69                 else
70                         logger -p debug -t gw-check "Could not fetch http://$t/conntest.html"
71                 fi
72         done
73 }
74
75 resolve() {
76         echo "$(nslookup $1 2>/dev/null |grep 'Address' |grep -v '127.0.0.1' |awk '{ print $3 }')"
77 }
78
79 get_dnsservers() {
80         # this gets all dns servers for the interface which has the default route
81
82         dns=""
83         if [ ! -x /bin/ubus ]; then
84                 # ubus not present (versions before Attitude): fallback to get these from /var/state/network.
85                 # We always assume that wan is the default route interface here
86                 dns="$(grep network.wan.resolv_dns /var/state/network | cut -d "=" -f 2)"
87         else
88                 network_find_wan wan
89                 network_get_dnsserver dns $wan
90         fi
91 }
92
93 iw=$(check_internet)
94
95 if [ "$iw" == 0 ]; then
96         # Internet available again, restore default route and remove ip rules
97         if [ -n "$defroutegwcheck" ]; then
98                 ip route add $defroutegwcheck
99                 ip route del $defroutegwcheck table gw-check
100                 for host in $testserver; do
101                         ips="$(resolve $host)"
102                         for ip in $ips; do
103                                 [ -n "$(ip rule show | grep "to $ip lookup gw-check")" ] && ip rule del to $ip table gw-check
104                         done
105                 done
106                 get_dnsservers
107                 for d in $dns; do
108                         [ -n "$(ip rule show | grep "to $d lookup gw-check")" ] && ip rule del to $d table gw-check
109                 done
110                 logger -p err -t gw-check "Internet is available again, default route restored ( $defroutegwcheck)"
111         fi
112
113 else
114         # Check failed. Move default route to table gw-check and setup ip rules.
115         if [ -z "$(ip rule show | grep gw-check)" -a -n "$defroutemain" ]; then
116                 ip route add $defroutemain table gw-check
117                 ip route del $defroutemain
118                 logger -p err -t gw-check "Internet is not available, default route deactivated ( $defroutemain)"
119         fi
120         for host in $testserver; do
121                 ips="$(resolve $host)"
122                 for ip in $ips; do
123                         [ -z "$(ip rule show | grep "to $ip lookup gw-check")" ] && ip rule add to $ip table gw-check
124                 done
125         done
126         get_dnsservers
127         for d in $dns; do
128                 [ -z "$(ip rule show | grep "to $d lookup gw-check")" ] && ip rule add to $d table gw-check
129         done
130         logger -p err -t gw-check "Check your internet connection!"
131 fi