]> git.decadent.org.uk Git - ion3.git/blob - mod_statusbar/ion-statusd/statusd_load.lua
[svn-inject] Installing original source of ion3
[ion3.git] / mod_statusbar / ion-statusd / statusd_load.lua
1 --
2 -- ion/mod_statusbar/ion-statusd/statusd_load.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2006.
5 --
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
10 --
11
12 --
13 -- We should really use getloadavg(3) instead and move the meter to
14 -- Ion side to get properly up-to-date loads. But until such an export
15 -- is made, and we use potentially blocking files and external programs, 
16 -- this meter must be in ion-statusd.
17 --
18
19 local defaults={
20     update_interval=10*1000,
21     load_hint=1,
22     important_threshold=1.5,
23     critical_threshold=4.0
24 }
25
26 local settings=table.join(statusd.get_config("load"), defaults)
27
28 local loadpat='^(%d+%.%d+).*(%d+%.%d+).*(%d+%.%d+)'
29
30 local function get_load_proc()
31     local f=io.open('/proc/loadavg', 'r')
32     if not f then
33         return ""
34     end
35     local s=f:read('*l')
36     f:close()
37     local st, en, load=string.find(s, '^(%d+%.%d+ %d+%.%d+ %d+%.%d+)')
38     
39     return string.gsub((load or ""), " ", ", ")
40 end
41
42 local function get_load_uptime()
43     local f=io.popen('uptime', 'r')
44     if not f then
45         return "??"
46     end
47     local s=f:read('*l')
48     f:close()
49     local st, en, load=string.find(s, 'load averages?:%s*(.*)')
50     return (load or "")
51 end
52
53 local function detect_load_fn()
54     if get_load_proc()~="" then
55         return get_load_proc
56     else
57         return get_load_uptime
58     end
59 end
60
61 local get_load, load_timer
62
63 local function get_hint(l)
64     local v=tonumber(l)
65     local i="normal"
66     if v then
67         if v>settings.critical_threshold then
68             i="critical"
69         elseif v>settings.important_threshold then
70             i="important"
71         end
72     end
73     return i
74 end
75
76 local l1min, l5min, l15min=2+1, 2+2, 2+3
77
78 local function update_load()
79     local l = get_load()    
80     local lds={string.find(l, loadpat)}
81     statusd.inform("load", l)
82     statusd.inform("load_hint", get_hint(lds[settings.load_hint+2]))
83     statusd.inform("load_1min", lds[l1min])
84     statusd.inform("load_1min_hint", get_hint(lds[l1min]))
85     statusd.inform("load_5min", lds[l5min])
86     statusd.inform("load_5min_hint", get_hint(lds[l5min]))
87     statusd.inform("load_15min", lds[l15min])
88     statusd.inform("load_15min_hint", get_hint(lds[l15min]))
89     load_timer:set(settings.update_interval, update_load)
90 end
91
92 -- Init
93 --statusd.inform("load_template", "0.00, 0.00, 0.00");
94
95 get_load=detect_load_fn()
96 load_timer=statusd.create_timer()
97 update_load()
98
99