luci-app-mwan3: use mwan3 ubus call for status information
authorFlorian Eckert <fe@dev.tdt.de>
Fri, 12 Jan 2018 15:12:02 +0000 (16:12 +0100)
committerFlorian Eckert <fe@dev.tdt.de>
Thu, 18 Jan 2018 14:09:12 +0000 (15:09 +0100)
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
applications/luci-app-mwan3/luasrc/controller/mwan3.lua
applications/luci-app-mwan3/luasrc/view/mwan/overview_status_interface.htm
applications/luci-app-mwan3/luasrc/view/mwan/status_interface.htm

index f429cc60fff41ed315373ecbb4549e5476039632..62e888c9214166758e1ce2a56fb1c24930de4749 100644 (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()
index 335c95c8fd61bf461888b44af78b097a677b1990..279f140bab3edd05dd834962352a6441ca9797f1 100644 (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>';
                        }
                }
        );
index c7039f7a619b0682e858597851f2329638c7ef3d..e6cbb484cbf5dd2b88a8a894563551955b09c891 100644 (file)
 <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>
                <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">