1 -- Authors: Jakub Ružička <yac@email.cz>
3 -- Last Changed: 2008-08-14
5 -- statusd_mcpu.lua - multi CPU monitor for ion3 statusbar
7 -- version 0.1 by Jakub Ružička <yac@email.cz>, 2008-08-14
10 -- Small and hopefully efficient monitor for multiple CPU using Linux
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.)
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 )
25 -- for example you can put the following in your template for dual core system:
26 -- template = ' cpu: %mcpu [ %mcpu_0, %mcpu_1 ] '
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.
42 important_threshold
= 60,
43 critical_threshold
= 90,
46 local settings
= table.join(statusd
.get_config("mcpu"), defaults
)
49 local f
=io
.popen("cat /proc/stat | grep '^cpu' | wc -l","r")
51 print "Failed to find out number of CPUs."
54 local cpus
= tonumber( f
:read('*a') ) - 1
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
= {}, {}, {}, {}, {}, {}, {}
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
66 function mcpu_refresh()
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*$' )
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
]
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
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
) )
95 local mcpu_timer
= statusd
.create_timer()
97 local function update_mcpu()
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
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 )
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" )
120 statusd
.inform( "mcpu_hint", "normal" )
123 -- each CPU (zero indexed)
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 )
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" )
137 statusd
.inform( hint
, "normal" )
141 mcpu_timer
:set(settings
.interval
, update_mcpu
)