From 6951da02ce0b5ffa5593d31173cee92d1b33ee3d Mon Sep 17 00:00:00 2001 From: Manuel Munz Date: Wed, 15 Dec 2010 16:35:19 +0000 Subject: [PATCH] modules/freifunk: Make status page update dynamically with javascript and small cosmetic changes. --- libs/sys/luasrc/sys.lua | 4 +- .../luasrc/controller/freifunk/freifunk.lua | 65 +++++- .../luasrc/view/freifunk/public_status.htm | 221 +++++++++++++++--- 3 files changed, 258 insertions(+), 32 deletions(-) diff --git a/libs/sys/luasrc/sys.lua b/libs/sys/luasrc/sys.lua index 61da6f1cb..817217b90 100644 --- a/libs/sys/luasrc/sys.lua +++ b/libs/sys/luasrc/sys.lua @@ -166,6 +166,7 @@ end -- @return String containing the memory used for caching in kB -- @return String containing the memory used for buffering in kB -- @return String containing the free memory amount in kB +-- @return String containing the cpu bogomips (number) function sysinfo() local cpuinfo = fs.readfile("/proc/cpuinfo") local meminfo = fs.readfile("/proc/meminfo") @@ -176,6 +177,7 @@ function sysinfo() local memcached = tonumber(meminfo:match("\nCached:%s*(%d+)")) local memfree = tonumber(meminfo:match("MemFree:%s*(%d+)")) local membuffers = tonumber(meminfo:match("Buffers:%s*(%d+)")) + local bogomips = tonumber(cpuinfo:match("BogoMIPS.-:%s*([^\n]+)")) if not system then system = nixio.uname().machine @@ -187,7 +189,7 @@ function sysinfo() model = cpuinfo:match("cpu model.-:%s*([^\n]+)") end - return system, model, memtotal, memcached, membuffers, memfree + return system, model, memtotal, memcached, membuffers, memfree, bogomips end --- Retrieves the output of the "logread" command. diff --git a/modules/freifunk/luasrc/controller/freifunk/freifunk.lua b/modules/freifunk/luasrc/controller/freifunk/freifunk.lua index 49ce9adef..f4f9b97c0 100644 --- a/modules/freifunk/luasrc/controller/freifunk/freifunk.lua +++ b/modules/freifunk/luasrc/controller/freifunk/freifunk.lua @@ -50,7 +50,8 @@ function index() page.setgroup = false entry({"freifunk", "status.json"}, call("jsonstatus")) - entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload") + entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload") + entry({"freifunk", "status", "public_status_json"}, call("public_status_json")).leaf = true assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, "OLSR", 30) @@ -217,3 +218,65 @@ function jsonstatus() http.prepare_content("application/json") ltn12.pump.all(json.Encoder(root):source(), http.write) end + +function public_status_json() + local twa = require "luci.tools.webadmin" + local sys = require "luci.sys" + local i18n = require "luci.i18n" + local path = luci.dispatcher.context.requestpath + local rv = { } + + local dev + for dev in path[#path]:gmatch("[%w%.%-]+") do + local j = { id = dev } + local iw = luci.sys.wifi.getiwinfo(dev) + if iw then + local f + for _, f in ipairs({ + "channel", "txpower", "bitrate", "signal", "noise", + "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname" + }) do + j[f] = iw[f] + end + end + rv[#rv+1] = j + end + + local load1, load5, load15 = sys.loadavg() + + local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo() + local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)", + tonumber(memtotal) / 1024, + tonumber(memtotal - memfree) / 1024, + tostring(i18n.translate("used")), + memfree / 1024, + tostring(i18n.translate("free")), + memcached / 1024, + tostring(i18n.translate("cached")), + membuffers / 1024, + tostring(i18n.translate("buffered")) + ) + + local dr4 = sys.net.defaultroute() + local dr6 = sys.net.defaultroute6() + + rv[#rv+1] = { + time = os.date("%c"), + uptime = twa.date_format(tonumber(sys.uptime())), + load = string.format("%.2f, %.2f, %.2f", load1, load5, load15), + mem = mem, + defroutev4 = { gateway = dr4.gateway:string(), + dest = dr4.dest:string(), + dev = dr4.device, + metr = dr4.metric }, + defroutev6 = { gateway = dr6.nexthop:string(), + dest = dr6.dest:string(), + dev = dr6.device, + metr = dr6.metric } + } + + luci.http.prepare_content("application/json") + luci.http.write_json(rv) + return +end + diff --git a/modules/freifunk/luasrc/view/freifunk/public_status.htm b/modules/freifunk/luasrc/view/freifunk/public_status.htm index 8948d5e9d..5cfc0c72d 100644 --- a/modules/freifunk/luasrc/view/freifunk/public_status.htm +++ b/modules/freifunk/luasrc/view/freifunk/public_status.htm @@ -2,45 +2,207 @@ local sys = require "luci.sys" local twa = require "luci.tools.webadmin" -- System -local system, model, memtotal, memcached, membuffers, memfree = sys.sysinfo() +local system, model, memtotal, memcached, membuffers, memfree, bogomips = sys.sysinfo() local uptime = twa.date_format(tonumber(sys.uptime())) local_time = os.date("%c") local load1, load5, load15 = sys.loadavg() local load = string.format("%.2f, %.2f, %.2f", load1, load5, load15) -local memory = string.format("%.2f MB (%.0f%% %s, %.0f%% %s, %.0f%% %s)", - tonumber(memtotal) / 1024, - 100 * memcached / memtotal, - tostring(translate("cached")), - 100 * membuffers / memtotal, - tostring(translate("buffered")), - 100 * memfree / memtotal, - tostring(translate("free")) + +local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)", +tonumber(memtotal) / 1024, +tonumber(memtotal - memfree) / 1024, +tostring(i18n.translate("used")), +memfree / 1024, +tostring(i18n.translate("free")), +memcached / 1024, +tostring(i18n.translate("cached")), +membuffers / 1024, +tostring(i18n.translate("buffered")) ) +-- update interval +local bogomips = bogomips or 100 +local interval +if bogomips > 350 then + interval = "5000" +else + interval = "10000" +end + -- wireless local ntm = require "luci.model.network".init() local devices = ntm:get_wifidevs() local netlist = { } local netdevs = { } local dev +for _, dev in ipairs(devices) do + local net + for _, net in ipairs(dev:get_wifinets()) do + netlist[#netlist+1] = net:ifname() + netdevs[net:ifname()] = dev:name() + end +end local has_iwinfo = pcall(require, "iwinfo") -- Routes local defroutev4 = sys.net.defaultroute() local defroutev6 = sys.net.defaultroute6 () - %> + <%+header%> + + + + +

<%:System%>

<%=system%>
<%=model%>
-
<%=load%>
-
<%=memory%>
-
<%=local_time%>
-
<%=uptime%>
+
<%=load%>
+
<%=mem%>
+
<%=local_time%>
+
<%=uptime%>
@@ -114,13 +276,13 @@ local defroutev6 = sys.net.defaultroute6 () local interface = net.iwdata.ifname or "N/A" %> - <%=signal_string%> - <%=bitrate%> - <%=ssid%> - <%=bssid%> - <%=chan%> - <%=mode%> - <%=txpwr%> + <%=signal_string%> + <%=bitrate%> + <%=ssid%> + <%=bssid%> + <%=chan%> + <%=mode%> + <%=txpwr%> <%=interface%> <% end @@ -151,20 +313,20 @@ local defroutev6 = sys.net.defaultroute6 () <% if defroutev4 then %> - <%=defroutev4.dest:string()%> - <%=defroutev4.device%> - <%=defroutev4.gateway:string()%> - <%=defroutev4.metric%> + <%=defroutev4.dest:string()%> + <%=defroutev4.device%> + <%=defroutev4.gateway:string()%> + <%=defroutev4.metric%> <% end if defroutev6 then %> - <%=defroutev6.dest:string()%> - <%=defroutev6.device%> - <%=defroutev6.nexthop:string()%> - <%=defroutev6.metric%> + <%=defroutev6.dest:string()%> + <%=defroutev6.device%> + <%=defroutev6.nexthop:string()%> + <%=defroutev6.metric%> <% end %> @@ -174,5 +336,4 @@ local defroutev6 = sys.net.defaultroute6 () <% end %> - <%+footer%> -- 2.25.1