File: statusd_inet_addr.lua

package info (click to toggle)
ion3-scripts 20050418-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 300 kB
  • ctags: 153
  • sloc: makefile: 18
file content (89 lines) | stat: -rw-r--r-- 2,258 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- statusd_inet_addr.lua
-- 
-- Copyright (c) Relu Patrascu 2004.
--
-- Ion is free software; you can redistribute it and/or modify it under
-- the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- Nov. 5, 2004
-- Disclaimer
-- Neither am I a lua expert nor do I have the time to invest in writing
-- better code here.  I simply needed this utility and it works OK for me.
-- I give no guarantees of any kind for this code.  Suggestions for
-- improvement are welcome.
-- ikoflexer at gmail dot com

local settings={
    interval=10*1000,
}

local function get_iface_proc()
    local st, en, iface
    local f=io.open('/proc/net/wireless', 'r')
    if not f then
        return ""
    end
    -- first 2 lines -- headers
    local s=f:read('*l')
    s=f:read('*l')
    -- the third line has wifi info
    s=f:read('*l')
    if not s then
		f=io.open('/proc/net/dev', 'r')
		if not f then
	   		return "net off"
		end
		-- first 2 lines -- headers, next line check for lo
    	s=f:read('*l')
    	s=f:read('*l')
    	s=f:read('*l')
    	st, en, iface = string.find(s, '(%w+:)')
		if iface == 'lo:' then
    		s=f:read('*l')
		end
		if not s then
			return "net off"
		end
    	st, en, iface = string.find(s, '(%w+:)')
    	f:close()
        return tostring(iface) 
    end
    st, en, iface = string.find(s, '(%w+:)')
    f:close()
    return tostring(iface)
end

local function get_inet_addr_ifcfg()
	local inet_addr
	local st,en,iface = string.find(tostring(get_iface_proc()), '(%w+):')
	local f = io.popen("ifconfig " .. iface)
	if not f then
		inet_addr=""
	else
		local ifconfig_info = f:read('*a')
		st,en,inet_addr = string.find(ifconfig_info, 'inet addr:(%d+\.%d+\.%d+\.%d+)')
		if not inet_addr then
			inet_addr=""
		end
	end
	return tostring(" " .. inet_addr)
end

local function get_inet_addr_fn()
	return get_inet_addr_ifcfg
end

local get_inet_addr, inet_addr_timer

local function update_inet_addr()
    statusd.inform("inet_addr", get_inet_addr())
    inet_addr_timer:set(settings.interval, update_inet_addr)
end

-- Init
get_inet_addr=get_inet_addr_fn()
inet_addr_timer=statusd.create_timer()
update_inet_addr()