1 -- Authors: Tibor Csögör <tibi@tiborius.net>
2 -- License: Public domain
3 -- Last Changed: 2006-11-14
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
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.
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.
25 -- "MEM: %meminfo_mem_free_adj free, SWAP: %meminfo_swap_used used".
27 -- This software is in the public domain.
29 --------------------------------------------------------------------------------
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
45 local function guess_mem_unit(amount
)
46 amount
= tonumber(amount
)
47 if (amount
< 1024) then
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"
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")
63 while (i
< string.len(s
)) do
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
)
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)
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)
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)
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
)