2 -- statusd_mcpu.lua - multi CPU monitor for ion3 statusbar
4 -- version 0.1 by Jakub Ružička <yac@email.cz>, 2008-08-14
7 -- Small and hopefully efficient monitor for multiple CPU using Linux
10 -- in cfg_statusbar.lua you can
12 -- use following meters:
13 -- %mcpu - avearge load of all CPUs
14 -- %mcpu_n - average load of n-th CPU (zero-indexed - %mcpu_0, %mcpu_1 etc.)
17 -- interval - update interval
18 -- prc_char - percent character (may be '')
19 -- important_threshold - important hint above this % ( >100 to disable )
20 -- critical_threshold - critical hint above this % ( >100 to disable )
22 -- for example you can put the following in your template for dual core system:
23 -- template = ' cpu: %mcpu [ %mcpu_0, %mcpu_1 ] '
27 -- * It's very easy to add avg and per-cpu user, nice, system, idle, iowait, irq
28 -- and softirg meters using a_* values as described in update_mcpu() but I
29 -- don't want/need these so you have to add them yourself.
31 -- * Script will (or should) work for one CPU but will produce mcpu and
32 -- redundant mcpu_0. If you want it to be efficient on one CPU, just edit it or
33 -- if you think the script should take care of it(extra ifs... pfff), mail me.
39 important_threshold
= 60,
40 critical_threshold
= 90,
43 local settings
= table.join(statusd
.get_config("mcpu"), defaults
)
46 local f
=io
.popen("cat /proc/stat | grep '^cpu' | wc -l","r")
48 print "Failed to find out number of CPUs."
51 local cpus
= tonumber( f
:read('*a') ) - 1
54 -- ugly global init :o]
55 user
, nice
, system
, idle
, iowait
, irq
, softirq
= {}, {}, {}, {}, {}, {}, {}
56 a_user
, a_nice
, a_system
, a_idle
, a_iowait
, a_irq
, a_softirq
= {}, {}, {}, {}, {}, {}, {}
58 user
[i
], nice
[i
], system
[i
], idle
[i
], iowait
[i
], irq
[i
], softirq
[i
] = 0, 0, 0, 0, 0, 0, 0
59 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 function mcpu_refresh()
65 -- not sure if breaking this cycle closes file automaticaly as it should...
66 for line
in io
.lines( '/proc/stat' ) do
68 suc
, _
, n_user
, n_nice
, n_system
, n_idle
, n_iowait
, n_irq
, n_softirq
= string.find( line
,
69 '^cpu%d?%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)%s+%d+%s+%d+%s*$' )
73 a_user
[i
], a_nice
[i
], a_system
[i
], a_idle
[i
], a_iowait
[i
], a_irq
[i
], a_softirq
[i
] =
74 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
]
77 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
80 -- this will probably suck on single CPU systems
81 print( string.format( "Getting CPU info for all CPUs failed. (i=%d,cpus=%d)", i
, cpus
) )
92 local mcpu_timer
= statusd
.create_timer()
94 local function update_mcpu()
101 all
= a_user
[i
] + a_nice
[i
] + a_system
[i
] + a_idle
[i
] + a_iowait
[i
] + a_irq
[i
] + a_softirq
[i
]
102 prc
[i
] = math
.floor( (1.0 - a_idle
[i
]/all
) * 100 + 0.5 )
105 -- summary for all CPUs
107 --statusd.inform('mcpu', lprc..settings.prc_char )
108 statusd
.inform('mcpu', string.format("%02d", tonumber(lprc
..settings
.prc_char
) ))
109 -- you can put here something like:
110 -- statusd.inform('mcpu_user', a_user[0]..settings.prc_char )
111 -- statusd.inform('mcpu_user', a_user[0]..settings.prc_char )
112 statusd
.inform('mcpu_user', string.format("%02d", tonumber(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), string.format("%02d", tonumber(lprc
..settings
.prc_char
)))
127 -- again, this is possible:
128 -- statusd.inform('mcpu_'..(i-1)..'_user', a_user[i]..settings.prc_char )
129 statusd
.inform('mcpu_'..(i
-1)..'_user', string.format("%02d", tonumber(a_user
[i
]..settings
.prc_char
) ))
132 local hint
='mcpu_'..(i
-1)..'_hint'
133 if lprc
>= settings
.critical_threshold
then
134 statusd
.inform( hint
, "critical" )
135 elseif lprc
>= settings
.important_threshold
then
136 statusd
.inform( hint
, "important" )
138 statusd
.inform( hint
, "normal" )
142 mcpu_timer
:set(settings
.interval
, update_mcpu
)