summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-multiwan/luasrc/controller/multiwan.lua
blob: 6aa4cf454a1f49a2fc58243bd1d8225774bf5443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module("luci.controller.multiwan", package.seeall)

function index()
	local fs = luci.fs or nixio.fs
	if not fs.access("/etc/config/multiwan") then
		return
	end

	local page

	page = entry({"admin", "network", "multiwan"}, cbi("multiwan/multiwan"), _("Multi-WAN"))
	page.i18n = "multiwan"
	page.dependent = true
	
	entry({"admin", "network", "multiwan", "status"}, call("multiwan_status"))

	page = entry({"mini", "network", "multiwan"}, cbi("multiwan/multiwanmini", {autoapply=true}), _("Multi-WAN"))
	page.i18n = "multiwan"
	page.dependent = true
end
function multiwan_status()
	local nfs = require "nixio.fs"
	local cachefile = "/tmp/.mwan/cache"

	local rv = {	}

	cachefile = nfs.readfile(cachefile)
	if cachefile then
		local ntm = require "luci.model.network".init()
		_, _, wan_if_map = string.find(cachefile, "wan_if_map=\"([^\"]*)\"")
		_, _, wan_fail_map = string.find(cachefile, "wan_fail_map=\"([^\"]*)\"")
		_, _, wan_recovery_map = string.find(cachefile, "wan_recovery_map=\"([^\"]*)\"")
		
		rv.wans = { }
		wansid = {}

		for wanname, wanifname in string.gfind(wan_if_map, "([^%[]+)%[([^%]]+)%]") do
			local wanlink = ntm:get_interface(wanifname)
			      wanlink = wanlink and wanlink:get_network()
			      wanlink = wanlink and wanlink:adminlink() or "#"
			wansid[wanname] = #rv.wans + 1
			rv.wans[wansid[wanname]] = { name = wanname, link = wanlink, ifname = wanifname, status = "ok", count = 0 }
		end

		for wanname, failcount in string.gfind(wan_fail_map, "([^%[]+)%[([^%]]+)%]") do
			if failcount == "x" then
				rv.wans[wansid[wanname]].status = "ko"
			else
				rv.wans[wansid[wanname]].status = "failing"
				rv.wans[wansid[wanname]].count = failcount
			end
		end

		for wanname, recoverycount in string.gfind(wan_recovery_map, "([^%[]+)%[([^%]]+)%]") do
			rv.wans[wansid[wanname]].status = "recovering"
			rv.wans[wansid[wanname]].count = recoverycount
		end
	end

	luci.http.prepare_content("application/json")
	luci.http.write_json(rv)
end