Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_mem.lua
blob17773fab76eb3a452264020bb9e991beebdf2b1c
1 -- Authors: Mario Garcia H. <drosophila@nmental.com>
2 -- License: GPL, version 2
3 -- Last Changed: 2006-12-07
4 --
5 -------------------------------------------------------------------------------------------
6 --
7 -- PURPOSE:
8 -- Shows system available memory catching [free] command outputs.
9 -- It is intended to make it simpler than statusd_meminfo, plus user configurable
10 -- measurement units and alarms for "all" available memory metters.
12 -- USAGE:
13 -- Just set any of the following labels on cfg_statusbar.lua: %mem_hused, %mem_shared
14 -- %mem_free, %mem_hfree, %mem_swap, %mem_used, %mem_cached. Example: [MF: %mem_free]
15 --
16 -- MEANINGS:
17 --> ** "mem_hfree" poses as "htop free memory" or "mem_free +cached +buffers",
18 -- in oposition, "mem_hused" is "mem_used -cached -buffers"; other labels have
19 -- transparent meanings.
21 ------- CONFIG EXAMPLE: ------------------------------------------------------------------
23 -- To modify settings is quite simple and flexible, write (on cfg_statusbar.lua)
24 -- something like this, without comments:
25 -- mem = {
26 -- update_interval = 15*1000, --> Milliseconds
27 -- free_alarm = 25, --> Limits percentaje ...
28 -- used_alarm = 65,
29 -- units = "m" --> "g" or "k" too
30 -- }
31 -- Write only the settings that do you want to change or leave this section as is...
32 --> ** "update_interval" means "time in milliseconds to update info (default = 15)"
33 -- "xx_alarm" means "do a color advise when memory *percentage* reaches this value".
34 -- (both defaults are 50). "units" means Gb "g", Mb "m" or Kb "k" (default = "m")
35 ------------------------------------------------------------------------------------------
37 -- NOTES:
38 -- * Alarms for used memory are inverse to alarms for free memory (think about it...)
39 -- "mem_total" label is useless. If total memory varies, its time to open your
40 -- hardware and check this script from barebone. Seriously, may be your video or wifi
41 -- devices were claiming some free R.A.M. on your machine start-up.
42 -- However, I included "mem_total" just in case.
43 -- ** This script has non blocking I/O.
45 -- LICENSE:
46 -- GPL2 Copyright(C)2006 Mario Garcia H.
47 -- (Please see http://www.gnu.org/licenses/gpl.html to read complete license)
49 -- T.STAMP: Thu Dec 7 03:28:04 2006
50 --
51 -- DEPENDS: "free" command. Probably, all GNU/Linux distros have one.
52 --
53 -- INSECTS: Not known.
54 --
55 -- CONTACT:
56 -- G.H. <drosophila (at) nmental (dot) com>
58 ------- DEFAULT SETTINGS :-----------------------------------------------------------------
60 local mem_timer
61 local defaults = { update_interval = 15*1000, free_alarm = 50, used_alarm = 50, units = "m" }
62 local settings = table.join(statusd.get_config("mem"), defaults)
64 ------- MEM MONITOR :----------------------------------------------------------------------
66 local function show_meminfo(status)
67 while status do
68 local ok, _, total, used, free, shared, buffers, cached =--
69 string.find(status, "Mem:%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)")
71 if not ok then statusd.inform("mem_template", "--") return end
73 statusd.inform("mem_total", total)
74 statusd.inform("mem_used", used)
75 statusd.inform("mem_free", free)
76 statusd.inform("mem_shared", shared)
77 statusd.inform("mem_buffers", buffers)
78 statusd.inform("mem_cached", cached)
79 statusd.inform("mem_hused", tostring(used - cached - buffers))
80 statusd.inform("mem_hfree", tostring(free + cached + buffers))
82 statusd.inform("mem_used_hint",
83 used*100/total >= settings.used_alarm and "critical" or "important")
84 statusd.inform("mem_hused_hint",
85 (used - cached - buffers)*100/total >= settings.used_alarm and "critical" or "important")
86 statusd.inform("mem_free_hint",
87 free*100/total <= settings.free_alarm and "critical" or "important")
88 statusd.inform("mem_hfree_hint",
89 (free + cached + buffers)*100/total <= settings.free_alarm and "critical" or "important")
91 status = coroutine.yield()
92 end
93 end
95 local function update_mem()
96 statusd.popen_bgread("free -"..settings.units.."o", coroutine.wrap(show_meminfo))
97 mem_timer:set(settings.update_interval, update_mem)
98 end
100 mem_timer = statusd.create_timer()
101 update_mem()