Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / legacy / statusd_iface.lua
blob9706c609d662c7db6de2076e52ab1b2e3eaea9a0
1 -- Authors: Relu Patrascu <ikoflexer@gmail.com>
2 -- License: LGPL, version 2.1 or later
3 -- Last Changed: 2004-11-05
4 --
5 -- statusd_iface.lua
6 --
7 -- Copyright (c) Relu Patrascu 2004.
8 --
9 -- Ion is free software; you can redistribute it and/or modify it under
10 -- the terms of the GNU Lesser General Public License as published by
11 -- the Free Software Foundation; either version 2.1 of the License, or
12 -- (at your option) any later version.
14 -- Nov. 5, 2004
15 -- Disclaimer
16 -- Neither am I a lua expert nor do I have the time to invest in writing
17 -- better code here. I simply needed this utility and it works OK for me.
18 -- I give no guarantees of any kind for this code. Suggestions for
19 -- improvement are welcome.
20 -- ikoflexer at gmail dot com
22 if not statusd_iface then
23 statusd_iface={ interval=10*1000 }
24 end
26 local iface
27 local function get_iface_proc()
28 local st, en
29 local f=io.open('/proc/net/wireless', 'r')
30 if not f then
31 return ""
32 end
33 -- first 2 lines -- headers
34 local s=f:read('*l')
35 s=f:read('*l')
36 -- the third line has wifi info
37 s=f:read('*l')
38 if not s then
39 f=io.open('/proc/net/dev', 'r')
40 if not f then
41 return "net off"
42 end
43 -- first 2 lines -- headers, next line check for lo
44 s=f:read('*l')
45 s=f:read('*l')
46 s=f:read('*l')
47 st, en, iface = string.find(s, '(%w+:)')
48 if iface == 'lo:' then
49 s=f:read('*l')
50 end
51 if not s then
52 return "net off"
53 end
54 st, en, iface = string.find(s, '(%w+:)')
55 f:close()
56 return tostring(iface)
57 end
58 st, en, iface = string.find(s, '(%w+:)')
59 f:close()
60 return tostring(iface)
61 end
63 local function get_iface_fn()
64 return get_iface_proc
65 end
67 local get_iface, iface_timer
69 local function update_iface()
70 statusd.inform("iface", get_iface())
71 iface_timer:set(statusd_iface.interval, update_iface)
72 end
74 -- Init
75 get_iface=get_iface_fn()
76 iface_timer=statusd.create_timer()
77 update_iface()