Released version 3-2014010502
[notion.git] / contrib / statusd / legacy / statusd_mcpu.lua
blob02ca722b42edd989387c5b2c8791340b750ae528
1 -- Authors: Jakub Ružička <yac@email.cz>
2 -- License: Unknown
3 -- Last Changed: 2008-08-14
4 --
5 -- statusd_mcpu.lua - multi CPU monitor for ion3 statusbar
6 --
7 -- version 0.1 by Jakub Ružička <yac@email.cz>, 2008-08-14
8 --
9 --
10 -- Small and hopefully efficient monitor for multiple CPU using Linux
11 -- /proc/stat.
13 -- in cfg_statusbar.lua you can
15 -- use following meters:
16 -- %mcpu - avearge load of all CPUs
17 -- %mcpu_n - average load of n-th CPU (zero-indexed - %mcpu_0, %mcpu_1 etc.)
19 -- set options:
20 -- interval - update interval
21 -- prc_char - percent character (may be '')
22 -- important_threshold - important hint above this % ( >100 to disable )
23 -- critical_threshold - critical hint above this % ( >100 to disable )
24 --
25 -- for example you can put the following in your template for dual core system:
26 -- template = ' cpu: %mcpu [ %mcpu_0, %mcpu_1 ] '
28 -- NOTES:
30 -- * It's very easy to add avg and per-cpu user, nice, system, idle, iowait, irq
31 -- and softirg meters using a_* values as described in update_mcpu() but I
32 -- don't want/need these so you have to add them yourself.
34 -- * Script will (or should) work for one CPU but will produce mcpu and
35 -- redundant mcpu_0. If you want it to be efficient on one CPU, just edit it or
36 -- if you think the script should take care of it(extra ifs... pfff), mail me.
39 local defaults = {
40 interval = 1000,
41 prc_char = '%',
42 important_threshold = 60,
43 critical_threshold = 90,
46 local settings = table.join(statusd.get_config("mcpu"), defaults)
48 -- how many CPUs?
49 local f=io.popen("cat /proc/stat | grep '^cpu' | wc -l","r")
50 if not f then
51 print "Failed to find out number of CPUs."
52 return 0
53 end
54 local cpus = tonumber( f:read('*a') ) - 1
55 f:close()
57 -- ugly global init :o]
58 user, nice, system, idle, iowait, irq, softirq = {}, {}, {}, {}, {}, {}, {}
59 a_user, a_nice, a_system, a_idle, a_iowait, a_irq, a_softirq = {}, {}, {}, {}, {}, {}, {}
60 for i = 0,cpus do
61 user[i], nice[i], system[i], idle[i], iowait[i], irq[i], softirq[i] = 0, 0, 0, 0, 0, 0, 0
62 a_user[i], a_nice[i], a_system[i], a_idle[i], a_iowait[i], a_irq[i], a_softirq[i] = 0, 0, 0, 0, 0, 0, 0
63 end
65 -- refresh values
66 function mcpu_refresh()
67 local i=0
68 -- not sure if breaking this cycle closes file automaticaly as it should...
69 for line in io.lines( '/proc/stat' ) do
71 suc, _, n_user, n_nice, n_system, n_idle, n_iowait, n_irq, n_softirq = string.find( line,
72 '^cpu%d?%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+%d+%s+%d+%s*$' )
74 if suc then
75 -- current values --
76 a_user[i], a_nice[i], a_system[i], a_idle[i], a_iowait[i], a_irq[i], a_softirq[i] =
77 n_user - user[i], n_nice - nice[i], n_system - system[i], n_idle - idle[i], n_iowait - iowait[i], n_irq - irq[i], n_softirq - softirq[i]
79 -- last --
80 user[i], nice[i], system[i], idle[i], iowait[i], irq[i], softirq[i] = n_user, n_nice, n_system, n_idle, n_iowait, n_irq, n_softirq
81 else
82 if i < cpus then
83 -- this will probably suck on single CPU systems
84 print( string.format( "Getting CPU info for all CPUs failed. (i=%d,cpus=%d)", i, cpus) )
85 break
86 end
87 end
89 i = i + 1
90 end
91 end
93 --- main ---
95 local mcpu_timer = statusd.create_timer()
97 local function update_mcpu()
98 local all
99 local prc = {}, lprc
101 mcpu_refresh()
103 for i = 0,cpus do
104 all = a_user[i] + a_nice[i] + a_system[i] + a_idle[i] + a_iowait[i] + a_irq[i] + a_softirq[i]
105 prc[i] = math.floor( (1.0 - a_idle[i]/all ) * 100 + 0.5 )
108 -- summary for all CPUs
109 lprc = prc[0]
110 statusd.inform('mcpu', lprc..settings.prc_char )
111 -- you can put here something like:
112 -- statusd.inform('mcpu_user', a_user[0]..settings.prc_char )
114 -- hint
115 if lprc >= settings.critical_threshold then
116 statusd.inform( "mcpu_hint", "critical" )
117 elseif lprc >= settings.important_threshold then
118 statusd.inform( "mcpu_hint", "important" )
119 else
120 statusd.inform( "mcpu_hint", "normal" )
123 -- each CPU (zero indexed)
124 for i = 1,cpus do
125 lprc = prc[i]
126 statusd.inform('mcpu_'..(i-1), lprc..settings.prc_char )
127 -- again, this is possible:
128 -- statusd.inform('mcpu_'..(i-1)..'_user', a_user[i]..settings.prc_char )
130 -- hint
131 local hint='mcpu_'..(i-1)..'_hint'
132 if lprc >= settings.critical_threshold then
133 statusd.inform( hint, "critical" )
134 elseif lprc >= settings.important_threshold then
135 statusd.inform( hint, "important" )
136 else
137 statusd.inform( hint, "normal" )
141 mcpu_timer:set(settings.interval, update_mcpu)
144 update_mcpu()