]> git.decadent.org.uk Git - ion3.git/blob - mod_statusbar/ion-statusd/statusd_load.lua
[svn-upgrade] Integrating new upstream version, ion3 (20070608)
[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="1min",
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 load_timer
29
30 local function get_hint(v)
31     local i="normal"
32     if v then
33         if v>settings.critical_threshold then
34             i="critical"
35         elseif v>settings.important_threshold then
36             i="important"
37         end
38     end
39     return i
40 end
41
42 local function fmt(l)
43     if not l then
44         return "?"
45     else
46         return string.format("%0.2f", l)
47     end
48 end
49
50 local function update_load()
51     local lds = statusd.getloadavg()
52     f1, f5, f15 = fmt(lds["1min"]), fmt(lds["5min"]), fmt(lds["15min"])
53     statusd.inform("load", f1..", "..f5..", "..f15)
54     statusd.inform("load_hint", get_hint(lds[settings.load_hint]))
55     statusd.inform("load_1min", f1)
56     statusd.inform("load_1min_hint", get_hint(lds["1min"]))
57     statusd.inform("load_5min", f5)
58     statusd.inform("load_5min_hint", get_hint(lds["5min"]))
59     statusd.inform("load_15min", f15)
60     statusd.inform("load_15min_hint", get_hint(lds["15min"]))
61     load_timer:set(settings.update_interval, update_load)
62 end
63
64 -- Init
65 --statusd.inform("load_template", "0.00, 0.00, 0.00");
66
67 load_timer=statusd.create_timer()
68 update_load()
69
70