aboutsummaryrefslogtreecommitdiff
path: root/mem.lua
blob: f3352b6041dc3915f3a8a7dac037635ef1fa371c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
---------------------------------------------------
-- Licensed under the GNU General Public License v2
--  * (c) 2010, Adrian C. <anrxc@sysphere.org>
--  * (c) 2009, Lucas de Vries <lucas@glacicle.com>
---------------------------------------------------

-- {{{ Grab environment
local io = { open = io.open }
local setmetatable = setmetatable
local math = { floor = math.floor }
local string = { match = string.match }
-- }}}


-- Mem: provides RAM and Swap usage statistics
module("vicious.mem")


-- {{{ Memory widget type
local function worker(format)
    -- Get meminfo
    local f = io.open("/proc/meminfo")
    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)
        end
    end
    f:close()

    -- Calculate 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)

    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 })