contrib/package: Add freifunk-gwcheck to replace the gateway-check in modules/freifunk
[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 ucisec = "ff_olsr_test_gw"
11 local section = nil
12
13 uci:foreach("olsrd", "LoadPlugin", function(s)
14 if s.library == "olsrd_dyn_gw_plain.so.0.4" and s.ignore ~= "1" then
15 section = s[".name"]
16 end
17 end)
18
19 local droute = sys.net.defaultroute()
20 if section and droute then -- dyn_gw is enabled and we have a defaultroute, test it
21 local state = false
22
23 for _, host in ipairs(PINGTRG) do
24 state = state or (sys.call(PINGCMD % {droute.device, host}) == 0)
25 end
26
27 if not state and (not STRICT or tonumber(droute.metric) == 0) then
28 local count = tonumber(uci:get("olsrd", ucisec, "noinet_count"))
29 if not THRESHOLD or (count and count >= THRESHOLD) then
30 sys.call(ROUTECMD % droute.gateway:string())
31 else
32 if not count then
33 uci:set("olsrd", ucisec, "state")
34 end
35 uci:set("olsrd", ucisec, "noinet_count", (count or 0) + 1)
36 uci:save("olsrd")
37 end
38 else
39 uci:revert("olsrd", ucisec)
40 end
41 end