1 -- Authors: Tibor Csögör <tibi@tiborius.net>
2 -- License: Public domain
3 -- Last Changed: 2007-03-10
5 -- $Id: statusd_cpustat.lua 80 2007-03-10 00:16:09Z tibi $
7 -- statusd_cpustat.lua -- CPU monitor for Ion3's statusbar
11 -- author : Tibor Csögör <tibi@tiborius.net>
13 -- Shows the CPU utilization of the system similiar to top(1).
14 -- This script depends on the /proc filesystem and thus only works on Linux.
15 -- Tested with kernel 2.6.16.
18 -- The following placeholders (with a "cpustat_" prefix) can be used in the
19 -- statusbar template: `user', `nice', `idle', `system', `iowait', `irq',
20 -- `softirq' and `steal'. In addition, `system_adj' sums up the last 5 fields.
21 -- For a compact output simply use the `cpustat' placeholder.
23 -- This software is in the public domain.
25 --------------------------------------------------------------------------------
29 update_interval
= 1000, -- 1 second
32 local settings
= table.join(statusd
.get_config("cpustat"), defaults
)
34 local last_stat
, current_stat
35 local last_uptime
, current_uptime
= 0, 0
36 local first_run
= true
38 function math
.round(number, precision
)
39 local m
= 10^
(precision
or 0)
40 return math
.floor((number*m
)+(1/2)) / m
43 -- this function reads stats from /proc/stat and /proc/uptime and calculates the
44 -- CPU time used in the measurement interval
45 local function get_cpustat()
47 f1
= io
.open('/proc/stat', 'r')
48 f2
= io
.open('/proc/uptime', 'r')
49 if ((f1
== nil ) or (f2
== nil)) then return nil end
55 last_uptime
= current_uptime
56 local tmp
, tmp
, up1
, up2
57 tmp
, tmp
, up1
, up2
= string.find(s2
, "(%d+)\.(%d+)%s%d")
58 current_uptime
= tonumber(up1
.. up2
)
59 local uptime_interv
= current_uptime
- last_uptime
62 for tmp
in string.gfind(s
, "%s+(%d+)") do
63 table.insert(t
, tonumber(tmp
))
66 last_stat
= current_stat
75 table.insert(c
, math
.round(((current_stat
[i
] - last_stat
[i
])
76 / uptime_interv
) * 100))
79 -- adjusted system CPU time (= system + iowait + irq + softirq + steal)
80 table.insert(c
, math
.round((((current_stat
[3] - last_stat
[3]) +
81 (current_stat
[5] - last_stat
[5]) +
82 (current_stat
[6] - last_stat
[6]) +
83 (current_stat
[7] - last_stat
[7]) +
84 (current_stat
[8] - last_stat
[8]))
85 / uptime_interv
) * 100))
89 local cpustat_timer
= statusd
.create_timer()
91 local function update_cpustat()
92 local t
= get_cpustat()
93 if (t
== nil) then return nil end
94 statusd
.inform("cpustat_user", t
[1] .. "%")
95 statusd
.inform("cpustat_nice", t
[2] .. "%")
96 statusd
.inform("cpustat_system", t
[3] .. "%")
97 statusd
.inform("cpustat_system_adj", t
[9] .. "%")
98 statusd
.inform("cpustat_idle", t
[4] .. "%")
99 statusd
.inform("cpustat_iowait", t
[5] .. "%")
100 statusd
.inform("cpustat_irq", t
[6] .. "%")
101 statusd
.inform("cpustat_softirq", t
[7] .. "%")
102 statusd
.inform("cpustat_steal", t
[8] .. "%")
103 statusd
.inform("cpustat", string.format("%3d%% us,%3d%% sy,%3d%% ni",
105 cpustat_timer
:set(settings
.update_interval
, update_cpustat
)