1 -------------------------------------------------------------------------------------------
4 -- Shows system available memory catching [free] command outputs.
5 -- It is intended to make it simpler than statusd_meminfo, plus user configurable
6 -- measurement units and alarms for "all" available memory metters.
9 -- Just set any of the following labels on cfg_statusbar.lua: %mem_hused, %mem_shared
10 -- %mem_free, %mem_hfree, %mem_swap, %mem_used, %mem_cached. Example: [MF: %mem_free]
13 --> ** "mem_hfree" poses as "htop free memory" or "mem_free +cached +buffers",
14 -- in oposition, "mem_hused" is "mem_used -cached -buffers"; other labels have
15 -- transparent meanings.
17 ------- CONFIG EXAMPLE: ------------------------------------------------------------------
19 -- To modify settings is quite simple and flexible, write (on cfg_statusbar.lua)
20 -- something like this, without comments:
22 -- update_interval = 15*1000, --> Milliseconds
23 -- free_alarm = 25, --> Limits percentaje ...
25 -- units = "m" --> "g" or "k" too
27 -- Write only the settings that do you want to change or leave this section as is...
28 --> ** "update_interval" means "time in milliseconds to update info (default = 15)"
29 -- "xx_alarm" means "do a color advise when memory *percentage* reaches this value".
30 -- (both defaults are 50). "units" means Gb "g", Mb "m" or Kb "k" (default = "m")
31 ------------------------------------------------------------------------------------------
34 -- * Alarms for used memory are inverse to alarms for free memory (think about it...)
35 -- "mem_total" label is useless. If total memory varies, its time to open your
36 -- hardware and check this script from barebone. Seriously, may be your video or wifi
37 -- devices were claiming some free R.A.M. on your machine start-up.
38 -- However, I included "mem_total" just in case.
39 -- ** This script has non blocking I/O.
42 -- GPL2 Copyright(C)2006 Mario Garcia H.
43 -- (Please see http://www.gnu.org/licenses/gpl.html to read complete license)
45 -- T.STAMP: Thu Dec 7 03:28:04 2006
47 -- DEPENDS: "free" command. Probably, all GNU/Linux distros have one.
49 -- INSECTS: Not known.
52 -- G.H. <drosophila (at) nmental (dot) com>
54 ------- DEFAULT SETTINGS :-----------------------------------------------------------------
58 update_interval = 15*1000,
65 local settings = table.join(statusd.get_config("mem"), defaults)
67 ------- MEM MONITOR :----------------------------------------------------------------------
69 local function show_meminfo(status)
71 local ok, _, total, used, free, shared, buffers, cached =
72 string.find(status, "Mem:%s+(%d+)%s+(%d+)%s+" ..
73 "(%d+)%s+(%d+)%s+(%d+)%s+(%d+)")
74 local sw_ok, _, sw_total, sw_used, sw_free =
75 string.find(status, "Swap:%s+(%d+)%s+(%d+)%s+(%d+)")
77 if not ok or not sw_ok
79 statusd.inform("mem_template", "--")
83 local hused = tostring(used - cached - buffers)
84 local hfree = tostring(free + cached + buffers)
85 statusd.inform("mem_total", total)
86 statusd.inform("mem_used", used)
87 statusd.inform("mem_free", free)
88 statusd.inform("mem_shared", shared)
89 statusd.inform("mem_buffers", buffers)
90 statusd.inform("mem_cached", cached)
91 statusd.inform("mem_hused", hused)
92 statusd.inform("mem_hfree", hfree)
93 statusd.inform("mem_sw_total", sw_total)
94 statusd.inform("mem_sw_used", sw_used)
95 statusd.inform("mem_sw_free", sw_free)
97 statusd.inform("mem_used_hint",
98 (used * 100 / total >= settings.used_alarm) and
99 "critical" or "normal")
100 statusd.inform("mem_hused_hint",
101 (hused * 100 / total >= settings.used_alarm) and
102 "critical" or "normal")
103 statusd.inform("mem_free_hint",
104 (free * 100 / total <= settings.free_alarm) and
105 "critical" or "normal")
106 statusd.inform("mem_hfree_hint",
107 (hfree * 100 / total <= settings.free_alarm) and
108 "critical" or "normal")
110 statusd.inform("mem_sw_used_hint",
111 (sw_used * 100 / total >= settings.used_sw_alarm) and
112 "critical" or "normal")
113 statusd.inform("mem_sw_free_hint",
114 (sw_free * 100 / total <= settings.free_sw_alarm) and
115 "critical" or "normal")
117 status = coroutine.yield()
121 local function update_mem()
122 statusd.popen_bgread("free -"..settings.units.."o", coroutine.wrap(show_meminfo))
123 mem_timer:set(settings.update_interval, update_mem)
126 mem_timer = statusd.create_timer()