luci-base: luci.tools.status: properly parse infinite dnsmasq leases
[project/luci.git] / modules / luci-base / luasrc / tools / status.lua
1 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.tools.status", package.seeall)
5
6 local uci = require "luci.model.uci".cursor()
7
8 local function dhcp_leases_common(family)
9 local rv = { }
10 local nfs = require "nixio.fs"
11 local leasefile = "/tmp/dhcp.leases"
12
13 uci:foreach("dhcp", "dnsmasq",
14 function(s)
15 if s.leasefile and nfs.access(s.leasefile) then
16 leasefile = s.leasefile
17 return false
18 end
19 end)
20
21 local fd = io.open(leasefile, "r")
22 if fd then
23 while true do
24 local ln = fd:read("*l")
25 if not ln then
26 break
27 else
28 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
29 local expire = tonumber(ts) or 0
30 if ts and mac and ip and name and duid then
31 if family == 4 and not ip:match(":") then
32 rv[#rv+1] = {
33 expires = (expire ~= 0) and os.difftime(expire, os.time()),
34 macaddr = mac,
35 ipaddr = ip,
36 hostname = (name ~= "*") and name
37 }
38 elseif family == 6 and ip:match(":") then
39 rv[#rv+1] = {
40 expires = (expire ~= 0) and os.difftime(expire, os.time()),
41 ip6addr = ip,
42 duid = (duid ~= "*") and duid,
43 hostname = (name ~= "*") and name
44 }
45 end
46 end
47 end
48 end
49 fd:close()
50 end
51
52 local lease6file = "/tmp/hosts/odhcpd"
53 uci:foreach("dhcp", "odhcpd",
54 function(t)
55 if t.leasefile and nfs.access(t.leasefile) then
56 lease6file = t.leasefile
57 return false
58 end
59 end)
60 local fd = io.open(lease6file, "r")
61 if fd then
62 while true do
63 local ln = fd:read("*l")
64 if not ln then
65 break
66 else
67 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
68 local expire = tonumber(ts) or 0
69 if ip and iaid ~= "ipv4" and family == 6 then
70 rv[#rv+1] = {
71 expires = (expire >= 0) and os.difftime(expire, os.time()),
72 duid = duid,
73 ip6addr = ip,
74 hostname = (name ~= "-") and name
75 }
76 elseif ip and iaid == "ipv4" and family == 4 then
77 rv[#rv+1] = {
78 expires = (expire >= 0) and os.difftime(expire, os.time()),
79 macaddr = duid,
80 ipaddr = ip,
81 hostname = (name ~= "-") and name
82 }
83 end
84 end
85 end
86 fd:close()
87 end
88
89 return rv
90 end
91
92 function dhcp_leases()
93 return dhcp_leases_common(4)
94 end
95
96 function dhcp6_leases()
97 return dhcp_leases_common(6)
98 end
99
100 function wifi_networks()
101 local rv = { }
102 local ntm = require "luci.model.network".init()
103
104 local dev
105 for _, dev in ipairs(ntm:get_wifidevs()) do
106 local rd = {
107 up = dev:is_up(),
108 device = dev:name(),
109 name = dev:get_i18n(),
110 networks = { }
111 }
112
113 local net
114 for _, net in ipairs(dev:get_wifinets()) do
115 rd.networks[#rd.networks+1] = {
116 name = net:shortname(),
117 link = net:adminlink(),
118 up = net:is_up(),
119 mode = net:active_mode(),
120 ssid = net:active_ssid(),
121 bssid = net:active_bssid(),
122 encryption = net:active_encryption(),
123 frequency = net:frequency(),
124 channel = net:channel(),
125 signal = net:signal(),
126 quality = net:signal_percent(),
127 noise = net:noise(),
128 bitrate = net:bitrate(),
129 ifname = net:ifname(),
130 assoclist = net:assoclist(),
131 country = net:country(),
132 txpower = net:txpower(),
133 txpoweroff = net:txpower_offset(),
134 disabled = (dev:get("disabled") == "1" or
135 net:get("disabled") == "1")
136 }
137 end
138
139 rv[#rv+1] = rd
140 end
141
142 return rv
143 end
144
145 function wifi_network(id)
146 local ntm = require "luci.model.network".init()
147 local net = ntm:get_wifinet(id)
148 if net then
149 local dev = net:get_device()
150 if dev then
151 return {
152 id = id,
153 name = net:shortname(),
154 link = net:adminlink(),
155 up = net:is_up(),
156 mode = net:active_mode(),
157 ssid = net:active_ssid(),
158 bssid = net:active_bssid(),
159 encryption = net:active_encryption(),
160 frequency = net:frequency(),
161 channel = net:channel(),
162 signal = net:signal(),
163 quality = net:signal_percent(),
164 noise = net:noise(),
165 bitrate = net:bitrate(),
166 ifname = net:ifname(),
167 assoclist = net:assoclist(),
168 country = net:country(),
169 txpower = net:txpower(),
170 txpoweroff = net:txpower_offset(),
171 disabled = (dev:get("disabled") == "1" or
172 net:get("disabled") == "1"),
173 device = {
174 up = dev:is_up(),
175 device = dev:name(),
176 name = dev:get_i18n()
177 }
178 }
179 end
180 end
181 return { }
182 end
183
184 function switch_status(devs)
185 local dev
186 local switches = { }
187 for dev in devs:gmatch("[^%s,]+") do
188 local ports = { }
189 local swc = io.popen("swconfig dev %q show" % dev, "r")
190 if swc then
191 local l
192 repeat
193 l = swc:read("*l")
194 if l then
195 local port, up = l:match("port:(%d+) link:(%w+)")
196 if port then
197 local speed = l:match(" speed:(%d+)")
198 local duplex = l:match(" (%w+)-duplex")
199 local txflow = l:match(" (txflow)")
200 local rxflow = l:match(" (rxflow)")
201 local auto = l:match(" (auto)")
202
203 ports[#ports+1] = {
204 port = tonumber(port) or 0,
205 speed = tonumber(speed) or 0,
206 link = (up == "up"),
207 duplex = (duplex == "full"),
208 rxflow = (not not rxflow),
209 txflow = (not not txflow),
210 auto = (not not auto)
211 }
212 end
213 end
214 until not l
215 swc:close()
216 end
217 switches[dev] = ports
218 end
219 return switches
220 end