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", "status", "mwan"},
14 alias("admin", "status", "mwan", "overview"),
15 _("Load Balancing"), 600)
17 entry({"admin", "status", "mwan", "overview"},
18 template("mwan/status_interface"))
19 entry({"admin", "status", "mwan", "detail"},
20 template("mwan/status_detail"))
21 entry({"admin", "status", "mwan", "diagnostics"},
22 template("mwan/status_diagnostics"))
23 entry({"admin", "status", "mwan", "troubleshooting"},
24 template("mwan/status_troubleshooting"))
25 entry({"admin", "status", "mwan", "interface_status"},
27 entry({"admin", "status", "mwan", "detailed_status"},
28 call("detailedStatus"))
29 entry({"admin", "status", "mwan", "diagnostics_display"},
30 call("diagnosticsData"), nil).leaf = true
31 entry({"admin", "status", "mwan", "troubleshooting_display"},
32 call("troubleshootingData"))
35 entry({"admin", "network", "mwan"},
36 alias("admin", "network", "mwan", "interface"),
37 _("Load Balancing"), 600)
39 entry({"admin", "network", "mwan", "globals"},
40 cbi("mwan/globalsconfig"),
41 _("Globals"), 5).leaf = true
42 entry({"admin", "network", "mwan", "interface"},
43 arcombine(cbi("mwan/interface"), cbi("mwan/interfaceconfig")),
44 _("Interfaces"), 10).leaf = true
45 entry({"admin", "network", "mwan", "member"},
46 arcombine(cbi("mwan/member"), cbi("mwan/memberconfig")),
47 _("Members"), 20).leaf = true
48 entry({"admin", "network", "mwan", "policy"},
49 arcombine(cbi("mwan/policy"), cbi("mwan/policyconfig")),
50 _("Policies"), 30).leaf = true
51 entry({"admin", "network", "mwan", "rule"},
52 arcombine(cbi("mwan/rule"), cbi("mwan/ruleconfig")),
53 _("Rules"), 40).leaf = true
54 entry({"admin", "network", "mwan", "notify"},
56 _("Notification"), 50).leaf = true
59 function mwan_Status()
60 local status = ut.ubus("mwan3", "status", {})
62 luci.http.prepare_content("application/json")
64 luci.http.write_json(status)
66 luci.http.write_json({})
70 function detailedStatus()
73 -- detailed mwan status
74 local detailStatusInfo = ut.trim(sys.exec("/usr/sbin/mwan3 status"))
75 if detailStatusInfo ~= "" then
76 mArray.mwandetail = { detailStatusInfo }
79 luci.http.prepare_content("application/json")
80 luci.http.write_json(mArray)
83 function diagnosticsData(interface, tool, task)
84 function getInterfaceNumber()
86 uci.cursor():foreach("mwan3", "interface",
89 if section[".name"] == interface then
90 interfaceNumber = number
99 local interfaceDevice = ut.trim(sys.exec("uci -q -p /var/state get network." .. interface .. ".ifname"))
100 if interfaceDevice ~= "" then
101 if tool == "ping" then
102 local gateway = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $2}'"))
103 if gateway ~= "" then
104 if task == "gateway" then
105 local pingCommand = "ping -c 3 -W 2 -I " .. interfaceDevice .. " " .. gateway
106 results = pingCommand .. "\n\n" .. sys.exec(pingCommand)
108 local tracked = ut.trim(sys.exec("uci -q -p /var/state get mwan3." .. interface .. ".track_ip"))
109 if tracked ~= "" then
110 for z in tracked:gmatch("[^ ]+") do
111 local pingCommand = "ping -c 3 -W 2 -I " .. interfaceDevice .. " " .. z
112 results = results .. pingCommand .. "\n\n" .. sys.exec(pingCommand) .. "\n\n"
115 results = "No tracking IP addresses configured on " .. interface
119 results = "No default gateway for " .. interface .. " found. Default route does not exist or is configured incorrectly"
121 elseif tool == "rulechk" then
123 local rule1 = sys.exec(ip .. "rule | grep $(echo $((" .. interfaceNumber .. " + 1000)))")
124 local rule2 = sys.exec(ip .. "rule | grep $(echo $((" .. interfaceNumber .. " + 2000)))")
125 if rule1 ~= "" and rule2 ~= "" then
126 results = "All required interface IP rules found:\n\n" .. rule1 .. rule2
127 elseif rule1 ~= "" or rule2 ~= "" then
128 results = "Missing 1 of the 2 required interface IP rules\n\n\nRules found:\n\n" .. rule1 .. rule2
130 results = "Missing both of the required interface IP rules"
132 elseif tool == "routechk" then
134 local routeTable = sys.exec(ip .. "route list table " .. interfaceNumber)
135 if routeTable ~= "" then
136 results = "Interface routing table " .. interfaceNumber .. " was found:\n\n" .. routeTable
138 results = "Missing required interface routing table " .. interfaceNumber
140 elseif tool == "hotplug" then
141 if task == "ifup" then
142 os.execute("/usr/sbin/mwan3 ifup " .. interface)
143 results = "Hotplug ifup sent to interface " .. interface .. "..."
145 os.execute("/usr/sbin/mwan3 ifdown " .. interface)
146 results = "Hotplug ifdown sent to interface " .. interface .. "..."
150 results = "Unable to perform diagnostic tests on " .. interface .. ". There is no physical or virtual device associated with this interface"
152 if results ~= "" then
153 results = ut.trim(results)
154 mArray.diagnostics = { results }
157 luci.http.prepare_content("application/json")
158 luci.http.write_json(mArray)
161 function troubleshootingData()
162 local ver = require "luci.version"
167 local wrtRelease = ut.trim(ver.distversion)
168 if wrtRelease ~= "" then
169 wrtRelease = "OpenWrt - " .. wrtRelease
171 wrtRelease = "OpenWrt - unknown"
173 local luciRelease = ut.trim(ver.luciversion)
174 if luciRelease ~= "" then
175 luciRelease = "\nLuCI - " .. luciRelease
177 luciRelease = "\nLuCI - unknown"
179 local mwanVersion = ut.trim(sys.exec("opkg info mwan3 | grep Version | awk '{print $2}'"))
180 if mwanVersion ~= "" then
181 mwanVersion = "\n\nmwan3 - " .. mwanVersion
183 mwanVersion = "\n\nmwan3 - unknown"
185 local mwanLuciVersion = ut.trim(sys.exec("opkg info luci-app-mwan3 | grep Version | awk '{print $2}'"))
186 if mwanLuciVersion ~= "" then
187 mwanLuciVersion = "\nmwan3-luci - " .. mwanLuciVersion
189 mwanLuciVersion = "\nmwan3-luci - unknown"
191 mArray.versions = { wrtRelease .. luciRelease .. mwanVersion .. mwanLuciVersion }
194 local mwanConfig = ut.trim(sys.exec("cat /etc/config/mwan3"))
195 if mwanConfig == "" then
196 mwanConfig = "No data found"
198 mArray.mwanconfig = { mwanConfig }
201 local networkConfig = ut.trim(sys.exec("cat /etc/config/network | sed -e 's/.*username.*/ USERNAME HIDDEN/' -e 's/.*password.*/ PASSWORD HIDDEN/'"))
202 if networkConfig == "" then
203 networkConfig = "No data found"
205 mArray.netconfig = { networkConfig }
208 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/'"))
209 if wirelessConfig == "" then
210 wirelessConfig = "No data found"
212 mArray.wificonfig = { wirelessConfig }
215 local ifconfig = ut.trim(sys.exec("ifconfig"))
216 if ifconfig == "" then
217 ifconfig = "No data found"
219 mArray.ifconfig = { ifconfig }
222 local routeShow = ut.trim(sys.exec("route -n"))
223 if routeShow == "" then
224 routeShow = "No data found"
226 mArray.routeshow = { routeShow }
229 local ipRuleShow = ut.trim(sys.exec(ip .. "rule show"))
230 if ipRuleShow == "" then
231 ipRuleShow = "No data found"
233 mArray.iprule = { ipRuleShow }
235 -- ip route list table 1-250
236 local routeList, routeString = ut.trim(sys.exec(ip .. "rule | sed 's/://g' 2>/dev/null | awk '$1>=2001 && $1<=2250' | awk '{print $NF}'")), ""
237 if routeList ~= "" then
238 for line in routeList:gmatch("[^\r\n]+") do
239 routeString = routeString .. line .. "\n" .. sys.exec(ip .. "route list table " .. line)
241 routeString = ut.trim(routeString)
243 routeString = "No data found"
245 mArray.routelist = { routeString }
247 -- default firewall output policy
248 local firewallOut = ut.trim(sys.exec("uci -q -p /var/state get firewall.@defaults[0].output"))
249 if firewallOut == "" then
250 firewallOut = "No data found"
252 mArray.firewallout = { firewallOut }
255 local iptables = ut.trim(sys.exec("iptables -L -t mangle -v -n"))
256 if iptables == "" then
257 iptables = "No data found"
259 mArray.iptables = { iptables }
261 luci.http.prepare_content("application/json")
262 luci.http.write_json(mArray)