6aa4cf454a1f49a2fc58243bd1d8225774bf5443
[project/luci.git] / applications / luci-multiwan / luasrc / controller / multiwan.lua
1 module("luci.controller.multiwan", package.seeall)
2
3 function index()
4 local fs = luci.fs or nixio.fs
5 if not fs.access("/etc/config/multiwan") then
6 return
7 end
8
9 local page
10
11 page = entry({"admin", "network", "multiwan"}, cbi("multiwan/multiwan"), _("Multi-WAN"))
12 page.i18n = "multiwan"
13 page.dependent = true
14
15 entry({"admin", "network", "multiwan", "status"}, call("multiwan_status"))
16
17 page = entry({"mini", "network", "multiwan"}, cbi("multiwan/multiwanmini", {autoapply=true}), _("Multi-WAN"))
18 page.i18n = "multiwan"
19 page.dependent = true
20 end
21 function multiwan_status()
22 local nfs = require "nixio.fs"
23 local cachefile = "/tmp/.mwan/cache"
24
25 local rv = { }
26
27 cachefile = nfs.readfile(cachefile)
28 if cachefile then
29 local ntm = require "luci.model.network".init()
30 _, _, wan_if_map = string.find(cachefile, "wan_if_map=\"([^\"]*)\"")
31 _, _, wan_fail_map = string.find(cachefile, "wan_fail_map=\"([^\"]*)\"")
32 _, _, wan_recovery_map = string.find(cachefile, "wan_recovery_map=\"([^\"]*)\"")
33
34 rv.wans = { }
35 wansid = {}
36
37 for wanname, wanifname in string.gfind(wan_if_map, "([^%[]+)%[([^%]]+)%]") do
38 local wanlink = ntm:get_interface(wanifname)
39 wanlink = wanlink and wanlink:get_network()
40 wanlink = wanlink and wanlink:adminlink() or "#"
41 wansid[wanname] = #rv.wans + 1
42 rv.wans[wansid[wanname]] = { name = wanname, link = wanlink, ifname = wanifname, status = "ok", count = 0 }
43 end
44
45 for wanname, failcount in string.gfind(wan_fail_map, "([^%[]+)%[([^%]]+)%]") do
46 if failcount == "x" then
47 rv.wans[wansid[wanname]].status = "ko"
48 else
49 rv.wans[wansid[wanname]].status = "failing"
50 rv.wans[wansid[wanname]].count = failcount
51 end
52 end
53
54 for wanname, recoverycount in string.gfind(wan_recovery_map, "([^%[]+)%[([^%]]+)%]") do
55 rv.wans[wansid[wanname]].status = "recovering"
56 rv.wans[wansid[wanname]].count = recoverycount
57 end
58 end
59
60 luci.http.prepare_content("application/json")
61 luci.http.write_json(rv)
62 end