vicious/widgets/mem.lua

52 lines
1.8 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 = { lines = io.lines }
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.widgets.mem")
-- {{{ Memory widget type
2009-08-07 17:41:10 +02:00
local function worker(format)
local mem = { buf = {}, swp = {} }
-- Get MEM info
for line in io.lines("/proc/meminfo") 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
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
2011-06-30 01:02:43 +02:00
mem.bcuse = mem.total - mem.buf.f
2009-10-05 00:13:05 +02:00
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,
2011-06-30 01:02:43 +02:00
mem.swp.usep, mem.swp.inuse, mem.swp.t, mem.swp.f,
mem.bcuse }
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })