Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_meminfo.lua
blob6ea221c66f823eb15c5677374db944e5d905393f
1 -- Authors: Tibor Csögör <tibi@tiborius.net>
2 -- License: Public domain
3 -- Last Changed: 2006-11-14
4 --
5 -- $Id: statusd_meminfo.lua 59 2006-11-14 11:17:02Z tibi $
7 -- statusd_meminfo.lua -- memory and swap usage monitor for Ion3's statusbar
9 -- version : 0.1
10 -- date : 2006-11-14
11 -- author : Tibor Csögör <tibi@tiborius.net>
13 -- Shows the memory and swap usage of the system.
14 -- This script depends on the /proc filesystem and thus only works on Linux.
15 -- Tested with kernel 2.6.16.
17 -- Configuration:
18 -- The placeholders `mem_total', `mem_used', `mem_free', `mem_buffers',
19 -- `mem_cached', `swap_total', `swap_used' and `swap_free' hold the same
20 -- information as the corresponding fieds in top(1). `mem_used_adj' and
21 -- `mem_free_adj' are adjusted values, here buffers and cached memory count as
22 -- free. Placeholders suffixed with `_p' yield percentage values.
24 -- Example usage:
25 -- "MEM: %meminfo_mem_free_adj free, SWAP: %meminfo_swap_used used".
27 -- This software is in the public domain.
29 --------------------------------------------------------------------------------
32 local defaults = {
33 update_interval = 1000, -- 1 second
36 local settings = table.join(statusd.get_config("meminfo"), defaults)
38 local meminfo_timer = statusd.create_timer()
40 function math.round(num, idp)
41 local mult = 10^(idp or 0)
42 return math.floor(num * mult + 0.5) / mult
43 end
45 local function guess_mem_unit(amount)
46 amount = tonumber(amount)
47 if (amount < 1024) then
48 return amount .. "k"
49 elseif (amount >= 1024) and (amount < 1048576) then
50 return math.round((amount / 1024)) .. "M"
51 elseif (amount > 1048576) then
52 return math.round((amount / 1048576), 1) .. "G"
53 end
54 end
56 local function get_meminfo()
57 local meminfo_table = {}
58 local f = io.open('/proc/meminfo', 'r')
59 if (f == nil) then return nil end
60 local s = f:read("*a")
61 f:close()
62 local i = 0
63 while (i < string.len(s)) do
64 local j, k, v
65 i, j, k, v = string.find(s, "([%w_]+):%s+(%d+) kB\n", i)
66 if (i == nil) then return nil end
67 meminfo_table[k] = tonumber(v)
68 i = j+1
69 end
70 return meminfo_table
71 end
73 local function update_meminfo()
74 local t = get_meminfo()
75 if (t == nil) then return nil end
77 statusd.inform("meminfo_mem_total", guess_mem_unit(t.MemTotal))
78 statusd.inform("meminfo_mem_used", guess_mem_unit(t.MemTotal - t.MemFree))
79 statusd.inform("meminfo_mem_used_p",
80 math.round(((t.MemTotal-t.MemFree)/t.MemTotal)*100) .. "%")
81 statusd.inform("meminfo_mem_used_adj",
82 guess_mem_unit(t.MemTotal-(t.MemFree+t.Buffers+t.Cached)))
83 statusd.inform("meminfo_mem_used_adj_p", math.round(
84 ((t.MemTotal-(t.MemFree+t.Buffers+t.Cached))/t.MemTotal)*100)
85 .. "%")
86 statusd.inform("meminfo_mem_free", guess_mem_unit(t.MemFree))
87 statusd.inform("meminfo_mem_free_p", math.round((t.MemFree/t.MemTotal)*100)
88 .. "%")
89 statusd.inform("meminfo_mem_free_adj",
90 guess_mem_unit(t.MemFree+t.Buffers+t.Cached))
91 statusd.inform("meminfo_mem_free_adj_p",
92 math.round(((t.MemFree+t.Buffers+t.Cached)/t.MemTotal)*100)
93 .. "%")
94 statusd.inform("meminfo_mem_buffers", guess_mem_unit(t.Buffers))
95 statusd.inform("meminfo_mem_cached", guess_mem_unit(t.Cached))
96 statusd.inform("meminfo_swap_total", guess_mem_unit(t.SwapTotal))
97 statusd.inform("meminfo_swap_free", guess_mem_unit(t.SwapFree))
98 statusd.inform("meminfo_swap_free_p",
99 math.round((t.SwapFree/t.SwapTotal)*100) .. "%")
100 statusd.inform("meminfo_swap_used", guess_mem_unit(t.SwapTotal-t.SwapFree))
101 statusd.inform("meminfo_swap_used_p",
102 math.round(((t.SwapTotal-t.SwapFree)/t.SwapTotal)*100) .. "%")
104 meminfo_timer:set(settings.update_interval, update_meminfo)
107 update_meminfo()
109 -- EOF