Merge pull request #278 from nmav/ocserv
[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.dependent = true
13         
14         entry({"admin", "network", "multiwan", "status"}, call("multiwan_status"))
15
16         page = entry({"mini", "network", "multiwan"}, cbi("multiwan/multiwanmini", {autoapply=true}), _("Multi-WAN"))
17         page.dependent = true
18 end
19 function multiwan_status()
20         local nfs = require "nixio.fs"
21         local cachefile = "/tmp/.mwan/cache"
22
23         local rv = {    }
24
25         cachefile = nfs.readfile(cachefile)
26         if cachefile then
27                 local ntm = require "luci.model.network".init()
28                 _, _, wan_if_map = string.find(cachefile, "wan_if_map=\"([^\"]*)\"")
29                 _, _, wan_fail_map = string.find(cachefile, "wan_fail_map=\"([^\"]*)\"")
30                 _, _, wan_recovery_map = string.find(cachefile, "wan_recovery_map=\"([^\"]*)\"")
31                 
32                 rv.wans = { }
33                 wansid = {}
34
35                 for wanname, wanifname in string.gfind(wan_if_map, "([^%[]+)%[([^%]]+)%]") do
36                         local wanlink = ntm:get_interface(wanifname)
37                               wanlink = wanlink and wanlink:get_network()
38                               wanlink = wanlink and wanlink:adminlink() or "#"
39                         wansid[wanname] = #rv.wans + 1
40                         rv.wans[wansid[wanname]] = { name = wanname, link = wanlink, ifname = wanifname, status = "ok", count = 0 }
41                 end
42
43                 for wanname, failcount in string.gfind(wan_fail_map, "([^%[]+)%[([^%]]+)%]") do
44                         if failcount == "x" then
45                                 rv.wans[wansid[wanname]].status = "ko"
46                         else
47                                 rv.wans[wansid[wanname]].status = "failing"
48                                 rv.wans[wansid[wanname]].count = failcount
49                         end
50                 end
51
52                 for wanname, recoverycount in string.gfind(wan_recovery_map, "([^%[]+)%[([^%]]+)%]") do
53                         rv.wans[wansid[wanname]].status = "recovering"
54                         rv.wans[wansid[wanname]].count = recoverycount
55                 end
56         end
57
58         luci.http.prepare_content("application/json")
59         luci.http.write_json(rv)
60 end