aboutsummaryrefslogtreecommitdiff
path: root/widgets/mem.lua
blob: 67ef42aec4a204060f7367a9324baf71449c36f6 (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
---------------------------------------------------
-- 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 = { lines = io.lines }
local setmetatable = setmetatable
local math = { floor = math.floor }
local string = { gmatch = string.gmatch }
-- }}}


-- Mem: provides RAM and Swap usage statistics
-- vicious.widgets.mem
local mem = {}


-- {{{ Memory widget type
local function worker(format)
    local _mem = { buf = {}, swp = {} }

    -- Get MEM info
    for line in io.lines("/proc/meminfo") do
        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

    -- Calculate memory percentage
    _mem.free  = _mem.buf.f + _mem.buf.b + _mem.buf.c
    _mem.inuse = _mem.total - _mem.free
    _mem.bcuse = _mem.total - _mem.buf.f
    _mem.usep  = math.floor(_mem.inuse / _mem.total * 100)
    -- Calculate swap percentage
    _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.t, _mem.swp.f,
            _mem.bcuse }
end
-- }}}

return setmetatable(mem, { __call = function(_, ...) return worker(...) end })