Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_cpustat.lua
blob6af605bdbd4f7b69745fc75d416fd268f0c97b8d
1 -- Authors: Tibor Csögör <tibi@tiborius.net>
2 -- License: Public domain
3 -- Last Changed: 2007-03-10
4 --
5 -- $Id: statusd_cpustat.lua 80 2007-03-10 00:16:09Z tibi $
7 -- statusd_cpustat.lua -- CPU monitor for Ion3's statusbar
9 -- version : 0.3
10 -- date : 2007-03-10
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.
17 -- Configuration:
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 --------------------------------------------------------------------------------
28 local defaults = {
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
41 end
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()
46 local f1, f2, s, s2
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
50 s = f1:read("*l")
51 s2 = f2:read("*l")
52 f1:close()
53 f2:close()
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
61 local t = {}
62 for tmp in string.gfind(s, "%s+(%d+)") do
63 table.insert(t, tonumber(tmp))
64 end
66 last_stat = current_stat
67 current_stat = t
68 if (first_run) then
69 last_stat = t
70 first_run = false
71 end
73 local c = {}
74 for i = 1, #(t) do
75 table.insert(c, math.round(((current_stat[i] - last_stat[i])
76 / uptime_interv) * 100))
77 end
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))
86 return c
87 end
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",
104 t[1], t[9], t[2]))
105 cpustat_timer:set(settings.update_interval, update_cpustat)
108 update_cpustat()
110 -- EOF