Add OLSR dyn_gw_plain gateway test
[project/luci.git] / modules / freifunk / root / usr / sbin / ff_olsr_test_gw
1 #!/usr/bin/lua
2 local STRICT = true -- Only delete metric 0 routes
3 local PINGCMD = "ping -q -I%s -c3 -w3 '%s' >/dev/null 2>&1"
4 local PINGTRG = {"google.de", "www.de.debian.org", "eu.kernel.org", "freifunk.net"}
5 local ROUTECMD = "route del default gw '%s' >/dev/null 2>&1"
6 local THRESHOLD = 3 -- Maximum number of failed tests before dropping the route
7
8 local sys = require "luci.sys"
9 local uci = require "luci.model.uci".cursor_state()
10 local section = nil
11
12 uci:foreach("olsrd", "LoadPlugin", function(s)
13 if s.library == "olsrd_dyn_gw_plain.so.0.4" and s.ignore ~= "1" then
14 section = s[".name"]
15 end
16 end)
17
18 local droute = sys.net.defaultroute()
19 if section and droute then -- dyn_gw is enabled and we have a defaultroute, test it
20 local state = false
21
22 for _, host in ipairs(PINGTRG) do
23 state = state or (sys.call(PINGCMD % {droute.device, host}) == 0)
24 end
25
26 if not state and (not STRICT or tonumber(droute.metric) == 0) then
27 local count = tonumber(uci:get("olsrd", section, "noinet_count"))
28 if not THRESHOLD or (count and count > THRESHOLD) then
29 sys.call(ROUTECMD % droute.gateway:string())
30 else
31 uci:set("olsrd", section, "noinet_count", (count or 0) + 1)
32 end
33 else
34 uci:set("olsrd", section, "noinet_count", "0")
35 end
36 uci:save("olsrd")
37 end