]> git.llucax.com Git - personal/ion3-config.git/blob - statusd_mem.lua
Add lm-sensors statusd monitor.
[personal/ion3-config.git] / statusd_mem.lua
1 -------------------------------------------------------------------------------------------
2 --
3 --      PURPOSE:
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.
7 --
8 --      USAGE:
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]
11 --
12 --      MEANINGS:
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.
16 --
17 ------- CONFIG EXAMPLE: ------------------------------------------------------------------
18 --
19 --      To modify settings is quite simple and flexible, write (on cfg_statusbar.lua)
20 --      something like this, without comments:
21 --                                      mem = {
22 --                                         update_interval = 15*1000, --> Milliseconds
23 --                                         free_alarm = 25,   --> Limits percentaje ...
24 --                                         used_alarm = 65,
25 --                                         units = "m"        --> "g" or "k" too
26 --                                         }
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 ------------------------------------------------------------------------------------------
32 --
33 --      NOTES:
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.
40 --
41 --      LICENSE:
42 --      GPL2 Copyright(C)2006 Mario Garcia H.
43 --      (Please see http://www.gnu.org/licenses/gpl.html to read complete license)
44 --
45 --      T.STAMP: Thu Dec  7 03:28:04 2006
46 --
47 --      DEPENDS: "free" command. Probably, all GNU/Linux distros have one.
48 --
49 --      INSECTS: Not known.
50 --
51 --      CONTACT:
52 --      G.H. <drosophila (at) nmental (dot) com>
53 --
54 ------- DEFAULT SETTINGS :-----------------------------------------------------------------
55
56 local mem_timer
57 local defaults = {
58         update_interval = 15*1000,
59         free_alarm = 50,
60         used_alarm = 50,
61         used_sw_alarm = 50,
62         free_sw_alarm = 50,
63         units = "m"
64 }
65 local settings = table.join(statusd.get_config("mem"), defaults)
66
67 ------- MEM MONITOR :----------------------------------------------------------------------
68
69 local function show_meminfo(status)
70         while status do
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+)")
76                 --
77                 if not ok or not sw_ok
78                 then
79                         statusd.inform("mem_template", "--")
80                         return
81                 end
82                 --
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)
96                 --
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")
109                 --
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")
116                 --
117                 status = coroutine.yield()
118         end
119 end
120
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)
124 end
125
126 mem_timer = statusd.create_timer()
127 update_mem()
128