vicious/mem.lua

57 lines
2.0 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 }
2009-10-05 00:13:05 +02:00
local string = { match = string.match }
-- }}}
-- 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
2009-10-05 00:13:05 +02:00
if string.match(line, "^MemTotal.*") then
2009-10-11 16:08:52 +02:00
mem.total = math.floor(string.match(line, "([%d]+)")/1024)
2009-10-05 00:13:05 +02:00
elseif string.match(line, "^MemFree.*") then
2009-10-11 16:08:52 +02:00
mem.buf.f = math.floor(string.match(line, "([%d]+)")/1024)
2009-10-05 00:13:05 +02:00
elseif string.match(line, "^Buffers.*") then
2009-10-11 16:08:52 +02:00
mem.buf.b = math.floor(string.match(line, "([%d]+)")/1024)
2009-10-05 00:13:05 +02:00
elseif string.match(line, "^Cached.*") then
2009-10-11 16:08:52 +02:00
mem.buf.c = math.floor(string.match(line, "([%d]+)")/1024)
2009-10-05 00:13:05 +02:00
-- Get swap stats while we are at it
elseif string.match(line, "^SwapTotal.*") then
2009-10-11 16:08:52 +02:00
mem.swp.total = math.floor(string.match(line, "([%d]+)")/1024)
2009-10-05 00:13:05 +02:00
elseif string.match(line, "^SwapFree.*") then
2009-10-11 16:08:52 +02:00
mem.swp.free = math.floor(string.match(line, "([%d]+)")/1024)
end
end
f:close()
-- Calculate 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
2009-10-05 00:13:05 +02:00
mem.swp.inuse = mem.swp.total - mem.swp.free
mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.total * 100)
2009-10-05 00:13:05 +02:00
return {mem.usep, mem.inuse, mem.total, mem.free,
mem.swp.usep, mem.swp.inuse, mem.swp.total, mem.swp.free}
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })