applications/luci-multiwan: add status indicators, patch by Etienne Champetier <etien...
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 2 Oct 2011 10:23:32 +0000 (10:23 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 2 Oct 2011 10:23:32 +0000 (10:23 +0000)
applications/luci-multiwan/luasrc/controller/multiwan.lua
applications/luci-multiwan/luasrc/view/admin_status/index/multiwan.htm [new file with mode: 0644]
applications/luci-multiwan/luasrc/view/multiwan_status.htm [new file with mode: 0644]

index 2a45c5f7bcbd3ea457bee26e8cd8f07faf66675f..6aa4cf454a1f49a2fc58243bd1d8225774bf5443 100644 (file)
@@ -11,8 +11,52 @@ function index()
        page = entry({"admin", "network", "multiwan"}, cbi("multiwan/multiwan"), _("Multi-WAN"))
        page.i18n = "multiwan"
        page.dependent = true
+       
+       entry({"admin", "network", "multiwan", "status"}, call("multiwan_status"))
 
        page = entry({"mini", "network", "multiwan"}, cbi("multiwan/multiwanmini", {autoapply=true}), _("Multi-WAN"))
        page.i18n = "multiwan"
        page.dependent = true
 end
+function multiwan_status()
+       local nfs = require "nixio.fs"
+       local cachefile = "/tmp/.mwan/cache"
+
+       local rv = {    }
+
+       cachefile = nfs.readfile(cachefile)
+       if cachefile then
+               local ntm = require "luci.model.network".init()
+               _, _, wan_if_map = string.find(cachefile, "wan_if_map=\"([^\"]*)\"")
+               _, _, wan_fail_map = string.find(cachefile, "wan_fail_map=\"([^\"]*)\"")
+               _, _, wan_recovery_map = string.find(cachefile, "wan_recovery_map=\"([^\"]*)\"")
+               
+               rv.wans = { }
+               wansid = {}
+
+               for wanname, wanifname in string.gfind(wan_if_map, "([^%[]+)%[([^%]]+)%]") do
+                       local wanlink = ntm:get_interface(wanifname)
+                             wanlink = wanlink and wanlink:get_network()
+                             wanlink = wanlink and wanlink:adminlink() or "#"
+                       wansid[wanname] = #rv.wans + 1
+                       rv.wans[wansid[wanname]] = { name = wanname, link = wanlink, ifname = wanifname, status = "ok", count = 0 }
+               end
+
+               for wanname, failcount in string.gfind(wan_fail_map, "([^%[]+)%[([^%]]+)%]") do
+                       if failcount == "x" then
+                               rv.wans[wansid[wanname]].status = "ko"
+                       else
+                               rv.wans[wansid[wanname]].status = "failing"
+                               rv.wans[wansid[wanname]].count = failcount
+                       end
+               end
+
+               for wanname, recoverycount in string.gfind(wan_recovery_map, "([^%[]+)%[([^%]]+)%]") do
+                       rv.wans[wansid[wanname]].status = "recovering"
+                       rv.wans[wansid[wanname]].count = recoverycount
+               end
+       end
+
+       luci.http.prepare_content("application/json")
+       luci.http.write_json(rv)
+end
diff --git a/applications/luci-multiwan/luasrc/view/admin_status/index/multiwan.htm b/applications/luci-multiwan/luasrc/view/admin_status/index/multiwan.htm
new file mode 100644 (file)
index 0000000..79c0453
--- /dev/null
@@ -0,0 +1 @@
+<%+multiwan_status%>
diff --git a/applications/luci-multiwan/luasrc/view/multiwan_status.htm b/applications/luci-multiwan/luasrc/view/multiwan_status.htm
new file mode 100644 (file)
index 0000000..8c4127b
--- /dev/null
@@ -0,0 +1,71 @@
+<script type="text/javascript">//<![CDATA[
+       XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "network", "multiwan", "status")%>', null,
+               function(x, st)
+               {
+                       var tx = document.getElementById('multiwan_status_text');
+                       if (tx)
+                       {
+                               if (st && st.wans)
+                               {
+                                       var temp = '';
+
+                                       for( var i = 0; i < st.wans.length; i++ )
+                                       {
+                                               var stat = '<%:Unknown%>';
+                                               var cssc = '';
+
+                                               switch (st.wans[i].status)
+                                               {
+                                                       case 'ok':
+                                                               stat = '<%:OK%>';
+                                                               cssc = 'wanok';
+                                                               break;
+
+                                                       case 'ko':
+                                                               stat = '<%:KO%>';
+                                                               cssc = 'wanko';
+                                                               break;
+
+                                                       case 'recovering':
+                                                               stat = String.format('<%:Recovering%>(%d)', st.wans[i].count);
+                                                               cssc = 'wanrecov';
+                                                               break;
+
+                                                       case 'failing':
+                                                               stat = String.format('<%:Failing%>(%d)', st.wans[i].count);
+                                                               cssc = 'wanfail';
+                                                               break;
+                                               }
+
+                                               temp += String.format(
+                                                       '<span class="%s"><strong>%s (<a href="%q">%s</a>) :</strong> %s</span>',
+                                                               cssc, st.wans[i].name, st.wans[i].link, st.wans[i].ifname, stat
+                                               );
+                                       }
+                                       
+                                       tx.innerHTML = temp;
+                               }
+                               else
+                               {
+                                       tx.innerHTML = '';
+                               }
+                       }
+               }
+       );
+//]]></script>
+
+<style type="text/css">
+  .wanok { background-color: rgb(144, 240, 144); }
+  .wanko { background-color: rgb(240, 144, 144); }
+  .wanrecov { background-color: rgb(240, 240, 0); }
+  .wanfail { background-color: rgb(255, 165, 0); }
+  .wanko, .wanok, .wanrecov, .wanfail {
+       padding: 0.4em;
+       margin: 0.4em;
+  }
+</style>
+
+<fieldset class="cbi-section">
+       <legend><%:Multi-WAN Status%></legend>
+       <div id="multiwan_status_text" style="text-align:center;"><em><%:Collecting data...%></em></div>
+</fieldset>