From 0b0d068ccb91f25f48099ff4d346eac2c387f86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Wed, 2 Mar 2011 13:28:33 +0200 Subject: health: fix memory info Parse /proc/meminfo instead of 'free' output which changed in busybox and caused breakage. Display also Buffers. --- health-model.lua | 28 ++++++++++++++++++++++------ health-system-html.lsp | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/health-model.lua b/health-model.lua index 2749e2a..1bc8376 100644 --- a/health-model.lua +++ b/health-model.lua @@ -32,17 +32,31 @@ local function diskfree ( media ) return cmd_result end -local function getpercentage ( data,grep,field1,field2 ) - local debugoutput = "" - local grepline = format.string_to_table(string.match(data,grep .. ".*"),"%s+") - local val1percentage = math.floor(((grepline[field1] / (grepline[field1]+grepline[field2]))*100)+0.5) - return val1percentage +local function memusage () + local mult = { kB=1024, MB=1048576, GB=1073741824 } + local fd = io.open("/proc/meminfo") + local res = {} + local field, value, unit + + for line in fd:lines() do + field,value, unit = string.match(line, "([^: ]*):%s*(%d+)%s(%a+)") + if field ~= nil and value ~= nil then + res[field] = tonumber(value) + if unit ~= nil then + res[field] = res[field] * mult[unit] + end + end + end + fd:close() + + return res end -- ############################################################### -- Public functions get_system = function (self) local system = {} + local meminfo = memusage() system.uptime = cfe({ value=querycmd("uptime"), label="Uptime" }) system.date = cfe({ value=querycmd("date"), label="Date" }) local indexver = indexversion() @@ -50,7 +64,9 @@ get_system = function (self) system.timezone = cfe({ value=date.what_tz(), label="Time Zone" }) system.uname = cfe({ value=querycmd("uname -a"), label="UName" }) system.memory = cfe({ value=querycmd("free"), label="Memory usage" }) - system.memory.used = getpercentage(querycmd("free"), "Total:", 3, 4) + system.memory.free = math.floor(100 * meminfo["MemFree"] / meminfo["MemTotal"]) + system.memory.buffers = math.floor(100 * (meminfo["Buffers"] + meminfo["Cached"]) / meminfo["MemTotal"]) + system.memory.used = 100 - math.floor(100 * (meminfo["MemFree"] + meminfo["Buffers"] + meminfo["Cached"]) / meminfo["MemTotal"]) return cfe({ type="group", value=system }) end diff --git a/health-system-html.lsp b/health-system-html.lsp index f2e4d6c..f25bf8b 100644 --- a/health-system-html.lsp +++ b/health-system-html.lsp @@ -23,14 +23,42 @@
<%= html.html_escape(view.value.memory.value) %>
+<% +local function print_percent(val) + if (tonumber(val) > 10) then + io.write(html.html_escape(val) .. "%") + end +end +%> + - + + + + +
0%
<% if ( tonumber(view.value.memory.used) > 10) then io.write(html.html_escape(view.value.memory.used) .. "%") end %>
<% if ( 90 > tonumber(view.value.memory.used)) then io.write((100-tonumber(view.value.memory.used)) .. "%") end %>
100%0% +
<% print_percent(view.value.memory.used) %>
+
+
<% print_percent(view.value.memory.buffers) %>
+
+
<% print_percent(view.value.memory.free) %>
+
100%
+ - + + + + + + +
Total:=Used=FreeLegend:=Used=Buffers=Free
-- cgit v1.2.3