1 -- Authors: Relu Patrascu <ikoflexer@gmail.com>
2 -- License: LGPL, version 2.1 or later
3 -- Last Changed: 2004-11-05
5 -- statusd_inetaddr.lua
7 -- Copyright (c) Relu Patrascu 2004.
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.
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 }
26 local function get_iface_proc()
28 local f
=io
.open('/proc/net/wireless', 'r')
32 -- first 2 lines -- headers
35 -- the third line has wifi info
38 f
=io
.open('/proc/net/dev', 'r')
42 -- first 2 lines -- headers, next line check for lo
46 st
, en
, iface
= string.find(s
, '(%w+:)')
47 if iface
== 'lo:' then
53 st
, en
, iface
= string.find(s
, '(%w+:)')
55 return tostring(iface
)
57 st
, en
, iface
= string.find(s
, '(%w+:)')
59 return tostring(iface
)
62 local function get_inetaddr_ifcfg()
64 local st
,en
,iface
= string.find(tostring(get_iface_proc()), '(%w+):')
65 local f
= io
.popen("/sbin/ifconfig " .. iface
)
69 local ifconfig_info
= f
:read('*a')
71 st
,en
,inetaddr
= string.find(ifconfig_info
, 'inet addr:(%d+%.%d+%.%d+%.%d+)')
76 return tostring(" " .. inetaddr
)
79 local function get_inetaddr_fn()
80 return get_inetaddr_ifcfg
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
)
91 get_inetaddr
=get_inetaddr_fn()
92 inetaddr_timer
=statusd
.create_timer()