[UP] add many more ion3 cfg -_-, powerfull, and add thinkpad xorg.conf/.Xmodmap,...
[arrow.git] / archlinux_conf / etc / ion3 / statusd_mcpu.lua
blob5c5bfd165e9aecc310773dc8fa1072cc248e4e4b
1 --
2 -- statusd_mcpu.lua - multi CPU monitor for ion3 statusbar
3 --
4 -- version 0.1 by Jakub Ružička <yac@email.cz>, 2008-08-14
5 --
6 --
7 -- Small and hopefully efficient monitor for multiple CPU using Linux
8 -- /proc/stat.
9 --
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.)
16 -- set options:
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 )
21 --
22 -- for example you can put the following in your template for dual core system:
23 -- template = ' cpu: %mcpu [ %mcpu_0, %mcpu_1 ] '
25 -- NOTES:
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.
36 local defaults = {
37 interval = 2000,
38 prc_char = '',
39 important_threshold = 60,
40 critical_threshold = 90,
43 local settings = table.join(statusd.get_config("mcpu"), defaults)
45 -- how many CPUs?
46 local f=io.popen("cat /proc/stat | grep '^cpu' | wc -l","r")
47 if not f then
48 print "Failed to find out number of CPUs."
49 return 0
50 end
51 local cpus = tonumber( f:read('*a') ) - 1
52 f:close()
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 = {}, {}, {}, {}, {}, {}, {}
57 for i = 0,cpus do
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
60 end
62 -- refresh values
63 function mcpu_refresh()
64 local i=0
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*$' )
71 if suc then
72 -- current values --
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]
76 -- last --
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
78 else
79 if i < cpus then
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) )
82 break
83 end
84 end
86 i = i + 1
87 end
88 end
90 --- main ---
92 local mcpu_timer = statusd.create_timer()
94 local function update_mcpu()
95 local all
96 local prc = {}, lprc
98 mcpu_refresh()
100 for i = 0,cpus do
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
106 lprc = prc[0]
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) ))
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), 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) ))
131 -- hint
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" )
137 else
138 statusd.inform( hint, "normal" )
142 mcpu_timer:set(settings.interval, update_mcpu)
145 update_mcpu()