Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_inetaddr.lua
blob77080702cbaa05849ddd5f6658027adde9cf4f6e
1 -- Authors: Relu Patrascu <ikoflexer@gmail.com>
2 -- License: LGPL, version 2.1 or later
3 -- Last Changed: 2004-11-05
4 --
5 -- statusd_inetaddr.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_inetaddr then
23 statusd_inetaddr={ interval=10*1000 }
24 end
26 local function get_iface_proc()
27 local st, en, iface
28 local f=io.open('/proc/net/wireless', 'r')
29 if not f then
30 return ""
31 end
32 -- first 2 lines -- headers
33 local s=f:read('*l')
34 s=f:read('*l')
35 -- the third line has wifi info
36 s=f:read('*l')
37 if not s then
38 f=io.open('/proc/net/dev', 'r')
39 if not f then
40 return "net off"
41 end
42 -- first 2 lines -- headers, next line check for lo
43 s=f:read('*l')
44 s=f:read('*l')
45 s=f:read('*l')
46 st, en, iface = string.find(s, '(%w+:)')
47 if iface == 'lo:' then
48 s=f:read('*l')
49 end
50 if not s then
51 return "net off"
52 end
53 st, en, iface = string.find(s, '(%w+:)')
54 f:close()
55 return tostring(iface)
56 end
57 st, en, iface = string.find(s, '(%w+:)')
58 f:close()
59 return tostring(iface)
60 end
62 local function get_inetaddr_ifcfg()
63 local inetaddr
64 local st,en,iface = string.find(tostring(get_iface_proc()), '(%w+):')
65 local f = io.popen("/sbin/ifconfig " .. iface)
66 if not f then
67 inetaddr=""
68 else
69 local ifconfig_info = f:read('*a')
70 f:close()
71 st,en,inetaddr = string.find(ifconfig_info, 'inet addr:(%d+%.%d+%.%d+%.%d+)')
72 if not inetaddr then
73 inetaddr=""
74 end
75 end
76 return tostring(" " .. inetaddr)
77 end
79 local function get_inetaddr_fn()
80 return get_inetaddr_ifcfg
81 end
83 local get_inetaddr, inetaddr_timer
85 local function update_inetaddr()
86 statusd.inform("inetaddr", get_inetaddr())
87 inetaddr_timer:set(statusd_inetaddr.interval, update_inetaddr)
88 end
90 -- Init
91 get_inetaddr=get_inetaddr_fn()
92 inetaddr_timer=statusd.create_timer()
93 update_inetaddr()