luci-app-mwan3: use mwan3 ubus call for status information

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
This commit is contained in:
Florian Eckert 2018-01-12 16:12:02 +01:00
parent 72bcd39b9f
commit 504fdad14b
3 changed files with 50 additions and 111 deletions

View file

@ -23,7 +23,7 @@ function index()
entry({"admin", "status", "mwan", "troubleshooting"},
template("mwan/status_troubleshooting"))
entry({"admin", "status", "mwan", "interface_status"},
call("interfaceStatus"))
call("mwan_Status"))
entry({"admin", "status", "mwan", "detailed_status"},
call("detailedStatus"))
entry({"admin", "status", "mwan", "diagnostics_display"},
@ -56,65 +56,15 @@ function index()
_("Notification"), 50).leaf = true
end
function getInterfaceStatus(ruleNumber, interfaceName)
if ut.trim(sys.exec("uci -q -p /var/state get mwan3." .. interfaceName .. ".enabled")) == "1" then
if ut.trim(sys.exec(ip .. "route list table " .. ruleNumber)) ~= "" then
if ut.trim(sys.exec("uci -q -p /var/state get mwan3." .. interfaceName .. ".track_ip")) ~= "" then
return "online"
else
return "notMonitored"
end
else
return "offline"
end
else
return "notEnabled"
end
end
function getInterfaceName()
local ruleNumber, status = 0, ""
uci.cursor():foreach("mwan3", "interface",
function (section)
ruleNumber = ruleNumber+1
status = status .. section[".name"] .. "[" .. getInterfaceStatus(ruleNumber, section[".name"]) .. "]"
end
)
return status
end
function interfaceStatus()
local ntm = require "luci.model.network".init()
local mArray = {}
-- overview status
local statusString = getInterfaceName()
if statusString ~= "" then
mArray.wans = {}
wansid = {}
for wanName, interfaceState in string.gfind(statusString, "([^%[]+)%[([^%]]+)%]") do
local wanInterfaceName = ut.trim(sys.exec("uci -q -p /var/state get network." .. wanName .. ".ifname"))
if wanInterfaceName == "" then
wanInterfaceName = "X"
end
local wanDeviceLink = ntm:get_interface(wanInterfaceName)
wanDeviceLink = wanDeviceLink and wanDeviceLink:get_network()
wanDeviceLink = wanDeviceLink and wanDeviceLink:adminlink() or "#"
wansid[wanName] = #mArray.wans + 1
mArray.wans[wansid[wanName]] = { name = wanName, link = wanDeviceLink, ifname = wanInterfaceName, status = interfaceState }
end
end
-- overview status log
local mwanLog = ut.trim(sys.exec("logread | grep mwan3 | tail -n 50 | sed 'x;1!H;$!d;x' 2>/dev/null"))
if mwanLog ~= "" then
mArray.mwanlog = { mwanLog }
end
function mwan_Status()
local status = ut.ubus("mwan3", "status", {})
luci.http.prepare_content("application/json")
luci.http.write_json(mArray)
if status ~= nil then
luci.http.write_json(status)
else
luci.http.write_json({})
end
end
function detailedStatus()

View file

@ -1,44 +1,46 @@
<script type="text/javascript">//<![CDATA[
XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "status", "mwan", "interface_status")%>', null,
function(x, mArray)
XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "status", "mwan", "interface_status")%>', null,
function(x, status)
{
var status = document.getElementById('mwan_status_text');
if (mArray.wans)
var statusDiv = document.getElementById('mwan_status_text');
if (status.interfaces)
{
var temp = '';
for ( var i = 0; i < mArray.wans.length; i++ )
var statusview = '';
for ( var iface in status.interfaces)
{
var stat = '';
var cssc = '';
switch (mArray.wans[i].status)
var state = '';
var css = '';
switch (status.interfaces[iface].status)
{
case 'online':
stat = '<%:Online (tracking active)%>';
cssc = 'wanon';
state = '<%:Online (tracking active)%>';
css = 'wanon';
break;
case 'notMonitored':
stat = '<%:Online (tracking off)%>';
cssc = 'wanon';
state = '<%:Online (tracking off)%>';
css = 'wanon';
break;
case 'offline':
stat = '<%:Offline%>';
cssc = 'wanoff';
state = '<%:Offline%>';
css = 'wanoff';
break;
case 'notEnabled':
stat = '<%:Disabled%>';
cssc = 'wanoff';
default:
state = '<%:Disabled%>';
css = 'wanoff';
break;
}
temp += String.format(
'<span class="%s"><strong>%s (<a href="%q">%s</a>)</strong><br />%s</span>',
cssc, mArray.wans[i].name, mArray.wans[i].link, mArray.wans[i].ifname, stat
statusview += String.format(
'<span class="%s"><strong>%s</strong><br />%s</span>',
css,
iface,
state
);
}
status.innerHTML = temp;
statusDiv.innerHTML = statusview;
}
else
{
status.innerHTML = '<strong><%:No MWAN interfaces found%></strong>';
statusDiv.innerHTML = '<strong><%:No MWAN interfaces found%></strong>';
}
}
);

View file

@ -10,57 +10,48 @@
<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
<script type="text/javascript">//<![CDATA[
XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "status", "mwan", "interface_status")%>', null,
function(x, mArray)
function(x, status)
{
var statusDiv = document.getElementById('mwan_status_text');
if (mArray.wans)
if (status.interfaces)
{
var interfaceStatus = '';
for ( var i = 0; i < mArray.wans.length; i++ )
var statusview = '';
for ( var iface in status.interfaces)
{
var status = '';
var state = '';
var css = '';
switch (mArray.wans[i].status)
switch (status.interfaces[iface].status)
{
case 'online':
status = '<%:Online (tracking active)%>';
state = '<%:Online (tracking active)%>';
css = 'wanon';
break;
case 'notMonitored':
status = '<%:Online (tracking off)%>';
state = '<%:Online (tracking off)%>';
css = 'wanon';
break;
case 'offline':
status = '<%:Offline%>';
state = '<%:Offline%>';
css = 'wanoff';
break;
case 'notEnabled':
status = '<%:Disabled%>';
default:
state = '<%:Disabled%>';
css = 'wanoff';
break;
}
interfaceStatus += String.format(
'<span class="%s"><strong>%s (<a href="%q">%s</a>)</strong><br />%s</span>',
css, mArray.wans[i].name, mArray.wans[i].link, mArray.wans[i].ifname, status
statusview += String.format(
'<span class="%s"><strong>%s</strong><br />%s</span>',
css,
iface,
state
);
}
statusDiv.innerHTML = interfaceStatus;
statusDiv.innerHTML = statusview;
}
else
{
statusDiv.innerHTML = '<strong><%:No MWAN interfaces found%></strong>';
}
var logs = document.getElementById('mwan_statuslog_text');
if (mArray.mwanlog)
{
var mwanLog = '<%:Last 50 MWAN systemlog entries. Newest entries sorted at the top :%>';
logs.innerHTML = String.format('<pre>%s<br /><br />%s</pre>', mwanLog, mArray.mwanlog[0]);
}
else
{
logs.innerHTML = '<strong><%:No MWAN systemlog history found%></strong>';
}
}
);
//]]></script>
@ -70,10 +61,6 @@
<legend><%:MWAN Interface Live Status%></legend>
<div id="mwan_status_text"><img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /><%:Collecting data...%></div>
</fieldset>
<fieldset class="cbi-section">
<legend><%:MWAN Interface Systemlog%></legend>
<div id="mwan_statuslog_text"><img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /><%:Collecting data...%></div>
</fieldset>
</div>
<style type="text/css">