From 3178068b962bca4f97d5e69a96375b865feb8d01 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Thu, 11 Mar 2010 02:57:07 +0100 Subject: mem: rewrite and simplify --- mem.lua | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'mem.lua') diff --git a/mem.lua b/mem.lua index f3352b6..f331ae4 100644 --- a/mem.lua +++ b/mem.lua @@ -8,7 +8,7 @@ local io = { open = io.open } local setmetatable = setmetatable local math = { floor = math.floor } -local string = { match = string.match } +local string = { gmatch = string.gmatch } -- }}} @@ -23,33 +23,28 @@ local function worker(format) local mem = { buf = {}, swp = {} } for line in f:lines() do - if string.match(line, "^MemTotal.*") then - mem.total = math.floor(string.match(line, "([%d]+)")/1024) - elseif string.match(line, "^MemFree.*") then - mem.buf.f = math.floor(string.match(line, "([%d]+)")/1024) - elseif string.match(line, "^Buffers.*") then - mem.buf.b = math.floor(string.match(line, "([%d]+)")/1024) - elseif string.match(line, "^Cached.*") then - mem.buf.c = math.floor(string.match(line, "([%d]+)")/1024) - -- Get swap stats while we are at it - elseif string.match(line, "^SwapTotal.*") then - mem.swp.total = math.floor(string.match(line, "([%d]+)")/1024) - elseif string.match(line, "^SwapFree.*") then - mem.swp.free = math.floor(string.match(line, "([%d]+)")/1024) + for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+") do + if k == "MemTotal" then mem.total = math.floor(v/1024) + elseif k == "MemFree" then mem.buf.f = math.floor(v/1024) + elseif k == "Buffers" then mem.buf.b = math.floor(v/1024) + elseif k == "Cached" then mem.buf.c = math.floor(v/1024) + elseif k == "SwapTotal" then mem.swp.t = math.floor(v/1024) + elseif k == "SwapFree" then mem.swp.f = math.floor(v/1024) + end end end f:close() - -- Calculate percentage + -- Calculate memory percentage mem.free = mem.buf.f + mem.buf.b + mem.buf.c mem.inuse = mem.total - mem.free mem.usep = math.floor(mem.inuse / mem.total * 100) -- Calculate swap percentage - mem.swp.inuse = mem.swp.total - mem.swp.free - mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.total * 100) + mem.swp.inuse = mem.swp.t - mem.swp.f + mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.t * 100) - return {mem.usep, mem.inuse, mem.total, mem.free, - mem.swp.usep, mem.swp.inuse, mem.swp.total, mem.swp.free} + return {mem.usep, mem.inuse, mem.total, mem.free, + mem.swp.usep, mem.swp.inuse, mem.swp.t, mem.swp.f} end -- }}} -- cgit v1.2.3