luci-app-mwan3: fix unify luci code styling
[project/luci.git] / applications / luci-app-mwan3 / luasrc / controller / mwan3.lua
1 module("luci.controller.mwan3", package.seeall)
2
3 sys = require "luci.sys"
4 ut = require "luci.util"
5
6 ip = "ip -4 "
7
8 function index()
9         if not nixio.fs.access("/etc/config/mwan3") then
10                 return
11         end
12
13         entry({"admin", "status", "mwan"},
14                 alias("admin", "status", "mwan", "overview"),
15                 _("Load Balancing"), 600)
16
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"},
26                 call("mwan_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"))
33
34
35         entry({"admin", "network", "mwan"},
36                 alias("admin", "network", "mwan", "interface"),
37                 _("Load Balancing"), 600)
38
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"},
55                 cbi("mwan/notify"),
56                 _("Notification"), 50).leaf = true
57 end
58
59 function mwan_Status()
60         local status = ut.ubus("mwan3", "status", {})
61
62         luci.http.prepare_content("application/json")
63         if status ~= nil then
64                 luci.http.write_json(status)
65         else
66                 luci.http.write_json({})
67         end
68 end
69
70 function detailedStatus()
71         local mArray = {}
72
73         -- detailed mwan status
74         local detailStatusInfo = ut.trim(sys.exec("/usr/sbin/mwan3 status"))
75         if detailStatusInfo ~= "" then
76                 mArray.mwandetail = { detailStatusInfo }
77         end
78
79         luci.http.prepare_content("application/json")
80         luci.http.write_json(mArray)
81 end
82
83 function diagnosticsData(interface, tool, task)
84         function getInterfaceNumber()
85                 local number = 0
86                 uci.cursor():foreach("mwan3", "interface",
87                         function (section)
88                                 number = number+1
89                                 if section[".name"] == interface then
90                                         interfaceNumber = number
91                                 end
92                         end
93                 )
94         end
95
96         local mArray = {}
97
98         local results = ""
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)
107                                 else
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"
113                                                 end
114                                         else
115                                                 results = "No tracking IP addresses configured on " .. interface
116                                         end
117                                 end
118                         else
119                                 results = "No default gateway for " .. interface .. " found. Default route does not exist or is configured incorrectly"
120                         end
121                 elseif tool == "rulechk" then
122                         getInterfaceNumber()
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
129                         else
130                                 results = "Missing both of the required interface IP rules"
131                         end
132                 elseif tool == "routechk" then
133                         getInterfaceNumber()
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
137                         else
138                                 results = "Missing required interface routing table " .. interfaceNumber
139                         end
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 .. "..."
144                         else
145                                 os.execute("/usr/sbin/mwan3 ifdown " .. interface)
146                                 results = "Hotplug ifdown sent to interface " .. interface .. "..."
147                         end
148                 end
149         else
150                 results = "Unable to perform diagnostic tests on " .. interface .. ". There is no physical or virtual device associated with this interface"
151         end
152         if results ~= "" then
153                 results = ut.trim(results)
154                 mArray.diagnostics = { results }
155         end
156
157         luci.http.prepare_content("application/json")
158         luci.http.write_json(mArray)
159 end
160
161 function troubleshootingData()
162         local ver = require "luci.version"
163
164         local mArray = {}
165
166         -- software versions
167         local wrtRelease = ut.trim(ver.distversion)
168                 if wrtRelease ~= "" then
169                         wrtRelease = "OpenWrt - " .. wrtRelease
170                 else
171                         wrtRelease = "OpenWrt - unknown"
172                 end
173         local luciRelease = ut.trim(ver.luciversion)
174                 if luciRelease ~= "" then
175                         luciRelease = "\nLuCI - " .. luciRelease
176                 else
177                         luciRelease = "\nLuCI - unknown"
178                 end
179         local mwanVersion = ut.trim(sys.exec("opkg info mwan3 | grep Version | awk '{print $2}'"))
180                 if mwanVersion ~= "" then
181                         mwanVersion = "\n\nmwan3 - " .. mwanVersion
182                 else
183                         mwanVersion = "\n\nmwan3 - unknown"
184                 end
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
188                 else
189                         mwanLuciVersion = "\nmwan3-luci - unknown"
190                 end
191         mArray.versions = { wrtRelease .. luciRelease .. mwanVersion .. mwanLuciVersion }
192
193         -- mwan config
194         local mwanConfig = ut.trim(sys.exec("cat /etc/config/mwan3"))
195                 if mwanConfig == "" then
196                         mwanConfig = "No data found"
197                 end
198         mArray.mwanconfig = { mwanConfig }
199
200         -- network config
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"
204                 end
205         mArray.netconfig = { networkConfig }
206
207         -- wireless config
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"
211                 end
212         mArray.wificonfig = { wirelessConfig }
213         
214         -- ifconfig
215         local ifconfig = ut.trim(sys.exec("ifconfig"))
216                 if ifconfig == "" then
217                         ifconfig = "No data found"
218                 end
219         mArray.ifconfig = { ifconfig }
220
221         -- route -n
222         local routeShow = ut.trim(sys.exec("route -n"))
223                 if routeShow == "" then
224                         routeShow = "No data found"
225                 end
226         mArray.routeshow = { routeShow }
227
228         -- ip rule show
229         local ipRuleShow = ut.trim(sys.exec(ip .. "rule show"))
230                 if ipRuleShow == "" then
231                         ipRuleShow = "No data found"
232                 end
233         mArray.iprule = { ipRuleShow }
234
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)
240                         end
241                         routeString = ut.trim(routeString)
242                 else
243                         routeString = "No data found"
244                 end
245         mArray.routelist = { routeString }
246
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"
251                 end
252         mArray.firewallout = { firewallOut }
253
254         -- iptables
255         local iptables = ut.trim(sys.exec("iptables -L -t mangle -v -n"))
256                 if iptables == "" then
257                         iptables = "No data found"
258                 end
259         mArray.iptables = { iptables }
260
261         luci.http.prepare_content("application/json")
262         luci.http.write_json(mArray)
263 end