luci-app-mwan3: refactoring diagnostics view and controller

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
Florian Eckert 2018-01-18 13:11:03 +01:00
parent 278b3f53db
commit 5b92193978
2 changed files with 142 additions and 110 deletions

View file

@ -80,9 +80,10 @@ function detailedStatus()
luci.http.write_json(mArray) luci.http.write_json(mArray)
end end
function diagnosticsData(interface, tool, task) function diagnosticsData(interface, task)
function getInterfaceNumber() function getInterfaceNumber(interface)
local number = 0 local number = 0
local interfaceNumber
uci.cursor():foreach("mwan3", "interface", uci.cursor():foreach("mwan3", "interface",
function (section) function (section)
number = number+1 number = number+1
@ -91,71 +92,111 @@ function diagnosticsData(interface, tool, task)
end end
end end
) )
return interfaceNumber
end
function diag_command(cmd, addr)
if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
local util = io.popen(cmd % addr)
if util then
while true do
local ln = util:read("*l")
if not ln then break end
luci.http.write(ln)
luci.http.write("\n")
end
util:close()
end
return
end
end
function get_gateway(inteface)
local dump = require("luci.util").ubus("network.interface.%s" % interface, "status", {})
local gateway
if dump then
local _, route
for _, route in ipairs(dump.route) do
if dump.route[_].target == "0.0.0.0" then
gateway = dump.route[_].nexthop
end
end
end
return gateway
end end
local mArray = {} local mArray = {}
local results = "" local results = ""
local interfaceDevice = ut.trim(sys.exec("uci -q -p /var/state get network." .. interface .. ".ifname")) local number = getInterfaceNumber(interface)
if interfaceDevice ~= "" then
if tool == "ping" then local uci = uci.cursor(nil, "/var/state")
local gateway = ut.trim(sys.exec("route -n | awk '{if ($8 == \"" .. interfaceDevice .. "\" && $1 == \"0.0.0.0\" && $3 == \"0.0.0.0\") print $2}'")) local device = uci:get("network", interface, "ifname")
if gateway ~= "" then
if task == "gateway" then luci.http.prepare_content("text/plain")
local pingCommand = "ping -c 3 -W 2 -I " .. interfaceDevice .. " " .. gateway if device ~= "" then
results = pingCommand .. "\n\n" .. sys.exec(pingCommand) if task == "ping_gateway" then
else local gateway = get_gateway(interface)
local tracked = ut.trim(sys.exec("uci -q -p /var/state get mwan3." .. interface .. ".track_ip")) if gateway ~= nil then
if tracked ~= "" then diag_command("ping -c 5 -W 1 %q 2>&1", gateway)
for z in tracked:gmatch("[^ ]+") do else
local pingCommand = "ping -c 3 -W 2 -I " .. interfaceDevice .. " " .. z luci.http.prepare_content("text/plain")
results = results .. pingCommand .. "\n\n" .. sys.exec(pingCommand) .. "\n\n" luci.http.write(string.format("No gateway for interface %s found.", interface))
end end
else elseif task == "ping_trackips" then
results = "No tracking IP addresses configured on " .. interface local trackips = uci:get("mwan3", interface, "track_ip")
end if #trackips > 0 then
for i in pairs(trackips) do
diag_command("ping -c 5 -W 1 %q 2>&1", trackips[i])
end end
else else
results = "No default gateway for " .. interface .. " found. Default route does not exist or is configured incorrectly" luci.http.write(string.format("No tracking Hosts for interface %s defined.", interface))
end end
elseif tool == "rulechk" then elseif task == "check_rules" then
getInterfaceNumber() local number = getInterfaceNumber(interface)
local rule1 = sys.exec(ip .. "rule | grep $(echo $((" .. interfaceNumber .. " + 1000)))") local iif = 1000 + number
local rule2 = sys.exec(ip .. "rule | grep $(echo $((" .. interfaceNumber .. " + 2000)))") local fwmark = 2000 + number
if rule1 ~= "" and rule2 ~= "" then local iif_rule = sys.exec(string.format("ip rule | grep %d", iif))
results = "All required interface IP rules found:\n\n" .. rule1 .. rule2 local fwmark_rule = sys.exec(string.format("ip rule | grep %d", fwmark))
elseif rule1 ~= "" or rule2 ~= "" then if iif_rule ~= "" and fwmark_rule ~= "" then
results = "Missing 1 of the 2 required interface IP rules\n\n\nRules found:\n\n" .. rule1 .. rule2 luci.http.write(string.format("All required IP rules for interface %s found", interface))
luci.http.write("\n")
luci.http.write(fwmark_rule)
luci.http.write(iif_rule)
elseif iif_rule == "" and fwmark_rule ~= "" then
luci.http.write(string.format("Only one IP rules for interface %s found", interface))
luci.http.write("\n")
luci.http.write(fwmark_rule)
elseif iif_rule ~= "" and fwmark_rule == "" then
luci.http.write(string.format("Only one IP rules for interface %s found", interface))
luci.http.write("\n")
luci.http.write(iif_rule)
else else
results = "Missing both of the required interface IP rules" luci.http.write(string.format("Missing both IP rules for interface %s", interface))
end end
elseif tool == "routechk" then elseif task == "check_routes" then
getInterfaceNumber() local number = getInterfaceNumber(interface)
local routeTable = sys.exec(ip .. "route list table " .. interfaceNumber) local routeTable = sys.exec(string.format("ip route list table %s", number))
if routeTable ~= "" then if routeTable ~= "" then
results = "Interface routing table " .. interfaceNumber .. " was found:\n\n" .. routeTable luci.http.write(string.format("Routing table %s for interface %s found", number, interface))
luci.http.write("\n")
luci.http.write(routeTable)
else else
results = "Missing required interface routing table " .. interfaceNumber luci.http.write(string.format("Routing table %s for interface %s not found", number, interface))
end
elseif tool == "hotplug" then
if task == "ifup" then
os.execute("/usr/sbin/mwan3 ifup " .. interface)
results = "Hotplug ifup sent to interface " .. interface .. "..."
else
os.execute("/usr/sbin/mwan3 ifdown " .. interface)
results = "Hotplug ifdown sent to interface " .. interface .. "..."
end end
elseif task == "hotplug_ifup" then
os.execute(string.format("/usr/sbin/mwan3 ifup %s", interface))
luci.http.write(string.format("Hotplug ifup sent to interface %s", interface))
elseif task == "hotplug_ifdown" then
os.execute(string.format("/usr/sbin/mwan3 ifdown %s", interface))
luci.http.write(string.format("Hotplug ifdown sent to interface %s", interface))
else
luci.http.write("Unknown task")
end end
else else
results = "Unable to perform diagnostic tests on " .. interface .. ". There is no physical or virtual device associated with this interface" luci.http.write(string.format("Unable to perform diagnostic tests on %s.", interface))
luci.http.write("\n")
luci.http.write("There is no physical or virtual device associated with this interface.")
end end
if results ~= "" then
results = ut.trim(results)
mArray.diagnostics = { results }
end
luci.http.prepare_content("application/json")
luci.http.write_json(mArray)
end end
function troubleshootingData() function troubleshootingData()

View file

@ -10,10 +10,11 @@
<% <%
local uci = require "luci.model.uci" local uci = require "luci.model.uci"
interfaceNames = "" local iface = {}
uci.cursor():foreach("mwan3", "interface", uci.cursor():foreach("mwan3", "interface",
function (section) function (section)
interfaceNames = interfaceNames .. section[".name"] .. " " table.insert(iface, section[".name"])
end end
) )
%> %>
@ -22,74 +23,64 @@
<script type="text/javascript">//<![CDATA[ <script type="text/javascript">//<![CDATA[
var stxhr = new XHR(); var stxhr = new XHR();
function update_status(tool, task, task_name) function update_status(iface, task)
{ {
var iface = document.getElementById('mwaniface').value; var legend = document.getElementById('diag-rc-legend');
var output = document.getElementById('diag_output'); var output = document.getElementById('diag-rc-output');
output.innerHTML = '<img src="<%=resource%>/icons/loading.gif" ' + output.innerHTML =
'alt="<%:Loading%>" ' + '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align: middle;" />' +
'style="padding: 20px; vertical-align: middle;" />' + "<%:Waiting for command to complete...%>"
"<%:Waiting for diagnostic results...%>"; ;
output.parentNode.style.display = 'block'; output.parentNode.style.display = 'block';
output.style.display = 'inline'; output.style.display = 'inline';
stxhr.get('<%=luci.dispatcher.build_url("admin", "status", "mwan")%>/diagnostics_display' + '/' + iface + '/' + tool + '/' + task, null, stxhr.post('<%=url('admin/status/mwan')%>/diagnostics_display' + '/' + iface + '/' + task, { token: '<%=token%>' },
function(x, mArray) function(x)
{ {
if (mArray.diagnostics) legend.style.display = 'none';
{ output.innerHTML = String.format('<pre>%h</pre>', x.responseText);
output.innerHTML = String.format('<pre id="diag_output_css">%h</pre>', mArray.diagnostics[0]);
}
else
{
output.innerHTML = '<pre id="diag_output_css"><strong><%:No diagnostic results returned%></strong></pre>';
}
} }
); );
} }
//]]></script> //]]></script>
<div id="mwan_diagnostics" class="cbi-map"> <form method="post" action="<%=url('admin/network/diagnostics')%>">
<fieldset id="diag_select" class="cbi-section"> <div class="cbi-map">
<legend><%:MWAN Interface Diagnostics%></legend> <h2 name="content"><%:MWAN Status - Diagnostics%></h2>
<select id="mwaniface">
<% for z in interfaceNames:gmatch("[^ ]+") do -%><option value="<%=z%>"><%=z%></option><%- end %>
</select>
<div id="buttoncss">
<input type="button" value="<%:Ping default gateway%>" class="cbi-button cbi-button-apply" onclick="update_status('ping', 'gateway', null)" />
<input type="button" value="<%:Ping tracking IP%>" class="cbi-button cbi-button-apply" onclick="update_status('ping', 'track_ip', null)" />
<input type="button" value="<%:Check IP rules%>" class="cbi-button cbi-button-apply" onclick="update_status('rulechk', null, null)" />
<input type="button" value="<%:Check routing table%>" class="cbi-button cbi-button-apply" onclick="update_status('routechk', null, null)" />
<input type="button" value="<%:Hotplug ifup%>" class="cbi-button cbi-button-apply" onclick="update_status('hotplug', 'ifup', null)" />
<input type="button" value="<%:Hotplug ifdown%>" class="cbi-button cbi-button-apply" onclick="update_status('hotplug', 'ifdown', null)" />
</div>
</fieldset>
<fieldset class="cbi-section" style="display:none">
<legend><%:Diagnostic Results%></legend>
<div id="diag_output"></div>
</fieldset>
</div>
<style type="text/css"> <fieldset class="cbi-section">
#mwaniface { <br />
float: left;
margin: 8px 20px 0px 0px; <div style="width:30%; float:left">
} <label class="cbi-value-title"><%:Interface%></label>
#buttoncss { <select name="iface" style="width:auto">
display: table; <% for _, z in ipairs(iface) do -%><option value="<%=z%>"><%=z%></option><%- end %>
float: left; </select>
text-align: left; </div>
}
.cbi-button { <div style="width:30%; float:left">
margin: 8px 20px 0px 0px; <label class="cbi-value-title"><%:Task%></label>
min-width: 153px; <select name="task" style="width:auto">
} <option value="ping_gateway"><%:Ping default gateway%></option>
#diag_output_css { <option value="ping_trackips"><%:Ping tracking IP%></option>
padding: 20px; <option value="check_rules"><%:Check IP rules%></option>
text-align: left; <option value="check_routes"><%:Check routing table%></option>
} <option value="hotplug_ifup"><%:Hotplug ifup%>"</option>
</style> <option value="hotplug_ifdown"><%:Hotplug ifdown%></option>
</select>
</div>
<div style="width:30%; float:left">
<input type="button" value="<%:Execute%>" class="cbi-button cbi-button-apply" onclick="update_status(this.form.iface.value, this.form.task.value)"/>
</div>
</fieldset>
</div>
<fieldset class="cbi-section" style="display:none">
<legend id="diag-rc-legend"><%:Collecting data...%></legend>
<span id="diag-rc-output"></span>
</fieldset>
</form>
<%+footer%> <%+footer%>