trunk: changeset 1856
[notion/jeffpc.git] / ext_statusbar / ion-statusd / statusd_load.lua
blob31c9b7a5cd8e4dbedaf1d010ae41e3bba75035e6
1 --
2 -- ion/ext_statusbar/ion-statusd/statusd_load.lua
3 --
4 -- Copyright (c) Tuomo Valkonen 2004.
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.
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.
19 local settings={
20 interval=10*1000,
23 local function get_load_proc()
24 local f=io.open('/proc/loadavg', 'r')
25 if not f then
26 return ""
27 end
28 local s=f:read('*l')
29 f:close()
30 local st, en, load=string.find(s, '^(%d+%.%d+ %d+%.%d+ %d+%.%d+)')
31 return string.gsub((load or ""), " ", ", ")
32 end
34 local function get_load_uptime()
35 local f=io.popen('uptime', 'r')
36 if not f then
37 return "??"
38 end
39 local s=f:read('*l')
40 f:close()
41 local st, en, load=string.find(s, 'load average:%s*(.*)')
42 return (load or "")
43 end
45 local function detect_load_fn()
46 if get_load_proc()~="" then
47 return get_load_proc
48 else
49 return get_load_uptime
50 end
51 end
53 local get_load
55 local function update_load(timer)
56 statusd.inform("load", get_load())
57 timer:set(settings.interval, update_load)
58 end
60 local function init()
61 get_load=detect_load_fn()
62 update_load(statusd.create_timer())
63 end
65 init()