1 module("luci.controller.mwan3", package.seeall)
3 sys = require "luci.sys"
4 ut = require "luci.util"
9 if not nixio.fs.access("/etc/config/mwan3") then
13 entry({"admin", "network", "mwan"},
14 alias("admin", "network", "mwan", "overview"),
15 _("Load Balancing"), 600)
17 entry({"admin", "network", "mwan", "overview"},
18 alias("admin", "network", "mwan", "overview", "overview_interface"),
20 entry({"admin", "network", "mwan", "overview", "overview_interface"},
21 template("mwan/overview_interface"))
22 entry({"admin", "network", "mwan", "overview", "interface_status"},
23 call("interfaceStatus"))
24 entry({"admin", "network", "mwan", "overview", "overview_detailed"},
25 template("mwan/overview_detailed"))
26 entry({"admin", "network", "mwan", "overview", "detailed_status"},
27 call("detailedStatus"))
29 entry({"admin", "network", "mwan", "configuration"},
30 alias("admin", "network", "mwan", "configuration", "interface"),
31 _("Configuration"), 20)
32 entry({"admin", "network", "mwan", "configuration", "interface"},
33 arcombine(cbi("mwan/interface"), cbi("mwan/interfaceconfig")),
34 _("Interfaces"), 10).leaf = true
35 entry({"admin", "network", "mwan", "configuration", "member"},
36 arcombine(cbi("mwan/member"), cbi("mwan/memberconfig")),
37 _("Members"), 20).leaf = true
38 entry({"admin", "network", "mwan", "configuration", "policy"},
39 arcombine(cbi("mwan/policy"), cbi("mwan/policyconfig")),
40 _("Policies"), 30).leaf = true
41 entry({"admin", "network", "mwan", "configuration", "rule"},
42 arcombine(cbi("mwan/rule"), cbi("mwan/ruleconfig")),
43 _("Rules"), 40).leaf = true
45 entry({"admin", "network", "mwan", "advanced"},
46 alias("admin", "network", "mwan", "advanced", "hotplugscript"),
48 entry({"admin", "network", "mwan", "advanced", "hotplugscript"},
49 form("mwan/advanced_hotplugscript"))
50 entry({"admin", "network", "mwan", "advanced", "mwanconfig"},
51 form("mwan/advanced_mwanconfig"))
52 entry({"admin", "network", "mwan", "advanced", "networkconfig"},
53 form("mwan/advanced_networkconfig"))
54 entry({"admin", "network", "mwan", "advanced", "wirelessconfig"},
55 form("mwan/advanced_wirelessconfig"))
56 entry({"admin", "network", "mwan", "advanced", "diagnostics"},
57 template("mwan/advanced_diagnostics"))
58 entry({"admin", "network", "mwan", "advanced", "diagnostics_display"},
59 call("diagnosticsData"), nil).leaf = true
60 entry({"admin", "network", "mwan", "advanced", "troubleshooting"},
61 template("mwan/advanced_troubleshooting"))
62 entry({"admin", "network", "mwan", "advanced", "troubleshooting_display"},
63 call("troubleshootingData"))
66 function getInterfaceStatus(ruleNumber, interfaceName)
67 if ut.trim(sys.exec("uci -q -p /var/state get mwan3." .. interfaceName .. ".enabled")) == "1" then
68 if ut.trim(sys.exec(ip .. "route list table " .. ruleNumber)) ~= "" then
69 if ut.trim(sys.exec("uci -q -p /var/state get mwan3." .. interfaceName .. ".track_ip")) ~= "" then
82 function getInterfaceName()
83 local ruleNumber, status = 0, ""
84 uci.cursor():foreach("mwan3", "interface",
86 ruleNumber = ruleNumber+1
87 status = status .. section[".name"] .. "[" .. getInterfaceStatus(ruleNumber, section[".name"]) .. "]"
93 function interfaceStatus()
94 local ntm = require "luci.model.network".init()
99 local statusString = getInterfaceName()
100 if statusString ~= "" then
104 for wanName, interfaceState in string.gfind(statusString, "([^%[]+)%[([^%]]+)%]") do
105 local wanInterfaceName = ut.trim(sys.exec("uci -q -p /var/state get network." .. wanName .. ".ifname"))
106 if wanInterfaceName == "" then
107 wanInterfaceName = "X"
109 local wanDeviceLink = ntm:get_interface(wanInterfaceName)
110 wanDeviceLink = wanDeviceLink and wanDeviceLink:get_network()
111 wanDeviceLink = wanDeviceLink and wanDeviceLink:adminlink() or "#"
112 wansid[wanName] = #mArray.wans + 1
113 mArray.wans[wansid[wanName]] = { name = wanName, link = wanDeviceLink, ifname = wanInterfaceName, status = interfaceState }
117 -- overview status log
118 local mwanLog = ut.trim(sys.exec("logread | grep mwan3 | tail -n 50 | sed 'x;1!H;$!d;x' 2>/dev/null"))
119 if mwanLog ~= "" then
120 mArray.mwanlog = { mwanLog }
123 luci.http.prepare_content("application/json")
124 luci.http.write_json(mArray)
127 function detailedStatus()
130 -- detailed mwan status
131 local detailStatusInfo = ut.trim(sys.exec("/usr/sbin/mwan3 status"))
132 if detailStatusInfo ~= "" then
133 mArray.mwandetail = { detailStatusInfo }
136 luci.http.prepare_content("application/json")
137 luci.http.write_json(mArray)
140 function diagnosticsData(interface, tool, task)
141 function getInterfaceNumber()
143 uci.cursor():foreach("mwan3", "interface",
146 if section[".name"] == interface then
147 interfaceNumber = number
156 if tool == "service" then
157 os.execute("/usr/sbin/mwan3 " .. task)
158 if task == "restart" then
159 results = "MWAN3 restarted"
160 elseif task == "stop" then
161 results = "MWAN3 stopped"
163 results = "MWAN3 started"
166 local interfaceDevice = ut.trim(sys.exec("uci -q -p /var/state get network." .. interface .. ".ifname"))
167 if interfaceDevice ~= "" then
168 if tool == "ping" then
169 local gateway = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $2}'"))
170 if gateway ~= "" then
171 if task == "gateway" then
172 local pingCommand = "ping -c 3 -W 2 -I " .. interfaceDevice .. " " .. gateway
173 results = pingCommand .. "\n\n" .. sys.exec(pingCommand)
175 local tracked = ut.trim(sys.exec("uci -q -p /var/state get mwan3." .. interface .. ".track_ip"))
176 if tracked ~= "" then
177 for z in tracked:gmatch("[^ ]+") do
178 local pingCommand = "ping -c 3 -W 2 -I " .. interfaceDevice .. " " .. z
179 results = results .. pingCommand .. "\n\n" .. sys.exec(pingCommand) .. "\n\n"
182 results = "No tracking IP addresses configured on " .. interface
186 results = "No default gateway for " .. interface .. " found. Default route does not exist or is configured incorrectly"
188 elseif tool == "rulechk" then
190 local rule1 = sys.exec(ip .. "rule | grep $(echo $((" .. interfaceNumber .. " + 1000)))")
191 local rule2 = sys.exec(ip .. "rule | grep $(echo $((" .. interfaceNumber .. " + 2000)))")
192 if rule1 ~= "" and rule2 ~= "" then
193 results = "All required interface IP rules found:\n\n" .. rule1 .. rule2
194 elseif rule1 ~= "" or rule2 ~= "" then
195 results = "Missing 1 of the 2 required interface IP rules\n\n\nRules found:\n\n" .. rule1 .. rule2
197 results = "Missing both of the required interface IP rules"
199 elseif tool == "routechk" then
201 local routeTable = sys.exec(ip .. "route list table " .. interfaceNumber)
202 if routeTable ~= "" then
203 results = "Interface routing table " .. interfaceNumber .. " was found:\n\n" .. routeTable
205 results = "Missing required interface routing table " .. interfaceNumber
207 elseif tool == "hotplug" then
208 if task == "ifup" then
209 os.execute("/usr/sbin/mwan3 ifup " .. interface)
210 results = "Hotplug ifup sent to interface " .. interface .. "..."
212 os.execute("/usr/sbin/mwan3 ifdown " .. interface)
213 results = "Hotplug ifdown sent to interface " .. interface .. "..."
217 results = "Unable to perform diagnostic tests on " .. interface .. ". There is no physical or virtual device associated with this interface"
220 if results ~= "" then
221 results = ut.trim(results)
222 mArray.diagnostics = { results }
225 luci.http.prepare_content("application/json")
226 luci.http.write_json(mArray)
229 function troubleshootingData()
230 local ver = require "luci.version"
235 local wrtRelease = ut.trim(ver.distversion)
236 if wrtRelease ~= "" then
237 wrtRelease = "OpenWrt - " .. wrtRelease
239 wrtRelease = "OpenWrt - unknown"
241 local luciRelease = ut.trim(ver.luciversion)
242 if luciRelease ~= "" then
243 luciRelease = "\nLuCI - " .. luciRelease
245 luciRelease = "\nLuCI - unknown"
247 local mwanVersion = ut.trim(sys.exec("opkg info mwan3 | grep Version | awk '{print $2}'"))
248 if mwanVersion ~= "" then
249 mwanVersion = "\n\nmwan3 - " .. mwanVersion
251 mwanVersion = "\n\nmwan3 - unknown"
253 local mwanLuciVersion = ut.trim(sys.exec("opkg info luci-app-mwan3 | grep Version | awk '{print $2}'"))
254 if mwanLuciVersion ~= "" then
255 mwanLuciVersion = "\nmwan3-luci - " .. mwanLuciVersion
257 mwanLuciVersion = "\nmwan3-luci - unknown"
259 mArray.versions = { wrtRelease .. luciRelease .. mwanVersion .. mwanLuciVersion }
262 local mwanConfig = ut.trim(sys.exec("cat /etc/config/mwan3"))
263 if mwanConfig == "" then
264 mwanConfig = "No data found"
266 mArray.mwanconfig = { mwanConfig }
269 local networkConfig = ut.trim(sys.exec("cat /etc/config/network | sed -e 's/.*username.*/ USERNAME HIDDEN/' -e 's/.*password.*/ PASSWORD HIDDEN/'"))
270 if networkConfig == "" then
271 networkConfig = "No data found"
273 mArray.netconfig = { networkConfig }
276 local wirelessConfig = ut.trim(sys.exec("cat /etc/config/wireless | sed -e 's/.*username.*/ USERNAME HIDDEN/' -e 's/.*password.*/ PASSWORD HIDDEN/' -e 's/.*key.*/ KEY HIDDEN/'"))
277 if wirelessConfig == "" then
278 wirelessConfig = "No data found"
280 mArray.wificonfig = { wirelessConfig }
283 local ifconfig = ut.trim(sys.exec("ifconfig"))
284 if ifconfig == "" then
285 ifconfig = "No data found"
287 mArray.ifconfig = { ifconfig }
290 local routeShow = ut.trim(sys.exec("route -n"))
291 if routeShow == "" then
292 routeShow = "No data found"
294 mArray.routeshow = { routeShow }
297 local ipRuleShow = ut.trim(sys.exec(ip .. "rule show"))
298 if ipRuleShow == "" then
299 ipRuleShow = "No data found"
301 mArray.iprule = { ipRuleShow }
303 -- ip route list table 1-250
304 local routeList, routeString = ut.trim(sys.exec(ip .. "rule | sed 's/://g' 2>/dev/null | awk '$1>=2001 && $1<=2250' | awk '{print $NF}'")), ""
305 if routeList ~= "" then
306 for line in routeList:gmatch("[^\r\n]+") do
307 routeString = routeString .. line .. "\n" .. sys.exec(ip .. "route list table " .. line)
309 routeString = ut.trim(routeString)
311 routeString = "No data found"
313 mArray.routelist = { routeString }
315 -- default firewall output policy
316 local firewallOut = ut.trim(sys.exec("uci -q -p /var/state get firewall.@defaults[0].output"))
317 if firewallOut == "" then
318 firewallOut = "No data found"
320 mArray.firewallout = { firewallOut }
323 local iptables = ut.trim(sys.exec("iptables -L -t mangle -v -n"))
324 if iptables == "" then
325 iptables = "No data found"
327 mArray.iptables = { iptables }
329 luci.http.prepare_content("application/json")
330 luci.http.write_json(mArray)