Translated using Weblate (Japanese)
[oweals/luci.git] / applications / luci-app-mwan3 / luasrc / controller / mwan3.lua
1 -- Copyright 2014 Aedan Renner <chipdankly@gmail.com>
2 -- Copyright 2018 Florian Eckert <fe@dev.tdt.de>
3 -- Licensed to the public under the GNU General Public License v2.
4
5 module("luci.controller.mwan3", package.seeall)
6
7 sys = require "luci.sys"
8 ut = require "luci.util"
9
10 ip = "ip -4 "
11
12 function index()
13         if not nixio.fs.access("/etc/config/mwan3") then
14                 return
15         end
16
17         entry({"admin", "status", "mwan"},
18                 alias("admin", "status", "mwan", "overview"),
19                 _("Load Balancing"), 600).acl_depends = { "luci-app-mwan3" }
20
21         entry({"admin", "status", "mwan", "overview"},
22                 template("mwan/status_interface"))
23         entry({"admin", "status", "mwan", "detail"},
24                 template("mwan/status_detail"))
25         entry({"admin", "status", "mwan", "diagnostics"},
26                 template("mwan/status_diagnostics"))
27         entry({"admin", "status", "mwan", "troubleshooting"},
28                 template("mwan/status_troubleshooting"))
29         entry({"admin", "status", "mwan", "interface_status"},
30                 call("mwan_Status"))
31         entry({"admin", "status", "mwan", "detailed_status"},
32                 call("detailedStatus"))
33         entry({"admin", "status", "mwan", "diagnostics_display"},
34                 call("diagnosticsData"), nil).leaf = true
35         entry({"admin", "status", "mwan", "troubleshooting_display"},
36                 call("troubleshootingData"))
37
38
39         entry({"admin", "network", "mwan"},
40                 alias("admin", "network", "mwan", "interface"),
41                 _("Load Balancing"), 600).acl_depends = { "luci-app-mwan3" }
42
43         entry({"admin", "network", "mwan", "globals"},
44                 cbi("mwan/globalsconfig"),
45                 _("Globals"), 5).leaf = true
46         entry({"admin", "network", "mwan", "interface"},
47                 arcombine(cbi("mwan/interface"), cbi("mwan/interfaceconfig")),
48                 _("Interfaces"), 10).leaf = true
49         entry({"admin", "network", "mwan", "member"},
50                 arcombine(cbi("mwan/member"), cbi("mwan/memberconfig")),
51                 _("Members"), 20).leaf = true
52         entry({"admin", "network", "mwan", "policy"},
53                 arcombine(cbi("mwan/policy"), cbi("mwan/policyconfig")),
54                 _("Policies"), 30).leaf = true
55         entry({"admin", "network", "mwan", "rule"},
56                 arcombine(cbi("mwan/rule"), cbi("mwan/ruleconfig")),
57                 _("Rules"), 40).leaf = true
58         entry({"admin", "network", "mwan", "notify"},
59                 form("mwan/notify"),
60                 _("Notification"), 50).leaf = true
61 end
62
63 function mwan_Status()
64         local status = ut.ubus("mwan3", "status", {})
65
66         luci.http.prepare_content("application/json")
67         if status ~= nil then
68                 luci.http.write_json(status)
69         else
70                 luci.http.write_json({})
71         end
72 end
73
74 function detailedStatus()
75         local statusInfo = ut.trim(sys.exec("/usr/sbin/mwan3 status"))
76         luci.http.prepare_content("text/plain")
77         if statusInfo ~= "" then
78                 luci.http.write(statusInfo)
79         else
80                 luci.http.write("Unable to get status information")
81         end
82 end
83
84 function diagnosticsData(interface, task)
85         function getInterfaceNumber(interface)
86                 local number = 0
87                 local interfaceNumber
88                 local uci = require "luci.model.uci".cursor()
89                 uci:foreach("mwan3", "interface",
90                         function (section)
91                                 number = number+1
92                                 if section[".name"] == interface then
93                                         interfaceNumber = number
94                                 end
95                         end
96                 )
97                 return interfaceNumber
98         end
99
100         function diag_command(cmd, device, addr)
101                 if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
102                         local util = io.popen(cmd %{ut.shellquote(device), ut.shellquote(addr)})
103                         if util then
104                                 luci.http.write("Command:\n")
105                                 luci.http.write(cmd %{ut.shellquote(device),
106                                         ut.shellquote(addr)} .. "\n\n")
107                                 luci.http.write("Result:\n")
108                                 while true do
109                                         local ln = util:read("*l")
110                                         if not ln then break end
111                                         luci.http.write(ln)
112                                         luci.http.write("\n")
113                                 end
114                                 util:close()
115                         end
116                         return
117                 end
118         end
119
120         function get_gateway(interface)
121                 local gateway = nil
122                 local dump = nil
123
124                 dump = require("luci.util").ubus("network.interface.%s_4" % interface, "status", {})
125                 if not dump then
126                         dump = require("luci.util").ubus("network.interface.%s" % interface, "status", {})
127                 end
128
129                 if dump and dump.route then
130                         local _, route
131                         for _, route in ipairs(dump.route) do
132                                 if dump.route[_].target == "0.0.0.0" then
133                                         gateway = dump.route[_].nexthop
134                                 end
135                         end
136                 end
137                 return gateway
138         end
139
140         local mArray = {}
141         local results = ""
142         local number = getInterfaceNumber(interface)
143
144         local uci = require "luci.model.uci".cursor(nil, "/var/state")
145         local nw = require "luci.model.network".init()
146         local i18n = require "luci.i18n"
147         local network = nw:get_network(interface)
148         local device = network and network:get_interface()
149         device = device:name()
150
151         luci.http.prepare_content("text/plain")
152         if device then
153                 if task == "ping_gateway" then
154                         local gateway = get_gateway(interface)
155                         if gateway ~= nil then
156                                 diag_command("ping -I %s -c 5 -W 1 %s 2>&1", device, gateway)
157                         else
158                                 luci.http.prepare_content("text/plain")
159                                 luci.http.write(i18n.translatef("No gateway for interface %s found.", interface))
160                         end
161                 elseif task == "ping_trackips" then
162                         local trackips = uci:get("mwan3", interface, "track_ip")
163                         if #trackips > 0 then
164                                 for i in pairs(trackips) do
165                                         diag_command("ping -I %s -c 5 -W 1 %s 2>&1", device, trackips[i])
166                                 end
167                         else
168                                 luci.http.write(i18n.translatef("No tracking Hosts for interface %s defined.", interface))
169                         end
170                 elseif task == "check_rules" then
171                         local number = getInterfaceNumber(interface)
172                         local iif = 1000 + number
173                         local fwmark = 2000 + number
174                         local iif_rule  = sys.exec(string.format("ip rule | grep %d", iif))
175                         local fwmark_rule = sys.exec(string.format("ip rule | grep %d", fwmark))
176                         if iif_rule ~= "" and fwmark_rule ~= "" then
177                                 luci.http.write(i18n.translatef("All required IP rules for interface %s found", interface))
178                                 luci.http.write("\n")
179                                 luci.http.write(fwmark_rule)
180                                 luci.http.write(iif_rule)
181                         elseif iif_rule == "" and fwmark_rule ~= "" then
182                                 luci.http.write(i18n.translatef("Only one IP rules for interface %s found", interface))
183                                 luci.http.write("\n")
184                                 luci.http.write(fwmark_rule)
185                         elseif iif_rule ~= "" and fwmark_rule == "" then
186                                 luci.http.write(i18n.translatef("Only one IP rules for interface %s found", interface))
187                                 luci.http.write("\n")
188                                 luci.http.write(iif_rule)
189                         else
190                                 luci.http.write(i18n.translatef("Missing both IP rules for interface %s", interface))
191                         end
192                 elseif task == "check_routes" then
193                         local number = getInterfaceNumber(interface)
194                         local routeTable = sys.exec(string.format("ip route list table %s", number))
195                         if routeTable ~= "" then
196                                 luci.http.write(i18n.translatef("Routing table %s for interface %s found", number, interface))
197                                 luci.http.write("\n")
198                                 luci.http.write(routeTable)
199                         else
200                                 luci.http.write(i18n.translatef("Routing table %s for interface %s not found", number, interface))
201                         end
202                 elseif task == "hotplug_ifup" then
203                         os.execute(string.format("/usr/sbin/mwan3 ifup %s", ut.shellquote(interface)))
204                         luci.http.write(string.format("Hotplug ifup sent to interface %s", interface))
205                 elseif task == "hotplug_ifdown" then
206                         os.execute(string.format("/usr/sbin/mwan3 ifdown %s", ut.shellquote(interface)))
207                         luci.http.write(string.format("Hotplug ifdown sent to interface %s", interface))
208                 else
209                         luci.http.write("Unknown task")
210                 end
211         else
212                 luci.http.write(string.format("Unable to perform diagnostic tests on %s.", interface))
213                 luci.http.write("\n")
214                 luci.http.write("There is no physical or virtual device associated with this interface.")
215         end
216 end
217
218 function troubleshootingData()
219         local ver = require "luci.version"
220         local dash = "-------------------------------------------------"
221
222         luci.http.prepare_content("text/plain")
223
224         luci.http.write("\n")
225         luci.http.write("\n")
226         luci.http.write("Software-Version")
227         luci.http.write("\n")
228         luci.http.write(dash)
229         luci.http.write("\n")
230         if ver.distversion then
231                 luci.http.write(string.format("OpenWrt - %s", ver.distversion))
232                 luci.http.write("\n")
233         else
234                 luci.http.write("OpenWrt - unknown")
235                 luci.http.write("\n")
236         end
237
238         if ver.luciversion then
239                 luci.http.write(string.format("LuCI - %s", ver.luciversion))
240                 luci.http.write("\n")
241         else
242                 luci.http.write("LuCI - unknown")
243                 luci.http.write("\n")
244         end
245
246         luci.http.write("\n")
247         luci.http.write("\n")
248         local output = ut.trim(sys.exec("ip a show"))
249         luci.http.write("Output of \"ip a show\"")
250         luci.http.write("\n")
251         luci.http.write(dash)
252         luci.http.write("\n")
253         if output ~= "" then
254                 luci.http.write(output)
255                 luci.http.write("\n")
256         else
257                 luci.http.write("No data found")
258                 luci.http.write("\n")
259         end
260
261         luci.http.write("\n")
262         luci.http.write("\n")
263         local output = ut.trim(sys.exec("ip route show"))
264         luci.http.write("Output of \"ip route show\"")
265         luci.http.write("\n")
266         luci.http.write(dash)
267         luci.http.write("\n")
268         if output ~= "" then
269                 luci.http.write(output)
270                 luci.http.write("\n")
271         else
272                 luci.http.write("No data found")
273                 luci.http.write("\n")
274         end
275
276         luci.http.write("\n")
277         luci.http.write("\n")
278         local output = ut.trim(sys.exec("ip rule show"))
279         luci.http.write("Output of \"ip rule show\"")
280         luci.http.write("\n")
281         luci.http.write(dash)
282         luci.http.write("\n")
283         if output ~= "" then
284                 luci.http.write(output)
285                 luci.http.write("\n")
286         else
287                 luci.http.write("No data found")
288                 luci.http.write("\n")
289         end
290
291         luci.http.write("\n")
292         luci.http.write("\n")
293         luci.http.write("Output of \"ip route list table 1-250\"")
294         luci.http.write("\n")
295         luci.http.write(dash)
296         luci.http.write("\n")
297         for i=1,250 do
298                 local output = ut.trim(sys.exec(string.format("ip route list table %d", i)))
299                 if output ~= "" then
300                         luci.http.write(string.format("Table %s: ", i))
301                         luci.http.write(output)
302                         luci.http.write("\n")
303                 end
304         end
305
306         luci.http.write("\n")
307         luci.http.write("\n")
308         local output = ut.trim(sys.exec("iptables -L -t mangle -v -n"))
309         luci.http.write("Output of \"iptables -L -t mangle -v -n\"")
310         luci.http.write("\n")
311         luci.http.write(dash)
312         luci.http.write("\n")
313         if output ~= "" then
314                 luci.http.write(output)
315                 luci.http.write("\n")
316         else
317                 luci.http.write("No data found")
318                 luci.http.write("\n")
319         end
320 end