vicious/mem.lua

52 lines
1.7 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
2010-01-02 21:21:54 +01:00
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
-- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- {{{ Grab environment
local io = { open = io.open }
local setmetatable = setmetatable
local math = { floor = math.floor }
2010-03-11 02:57:07 +01:00
local string = { gmatch = string.gmatch }
-- }}}
-- Mem: provides RAM and Swap usage statistics
module("vicious.mem")
-- {{{ Memory widget type
2009-08-07 17:41:10 +02:00
local function worker(format)
-- Get meminfo
local f = io.open("/proc/meminfo")
local mem = { buf = {}, swp = {} }
for line in f:lines() do
2010-03-11 02:57:07 +01:00
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()
2010-03-11 02:57:07 +01:00
-- Calculate memory percentage
2009-10-05 00:13:05 +02:00
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
2010-03-11 02:57:07 +01:00
mem.swp.inuse = mem.swp.t - mem.swp.f
mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.t * 100)
2010-03-11 02:57:07 +01:00
return {mem.usep, mem.inuse, mem.total, mem.free,
mem.swp.usep, mem.swp.inuse, mem.swp.t, mem.swp.f}
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })