1 -- Copyright 2011 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.tools.status", package.seeall)
6 local uci = require "luci.model.uci".cursor()
7 local ipc = require "luci.ip"
9 local function dhcp_leases_common(family)
11 local nfs = require "nixio.fs"
12 local sys = require "luci.sys"
13 local leasefile = "/tmp/dhcp.leases"
15 uci:foreach("dhcp", "dnsmasq",
17 if s.leasefile and nfs.access(s.leasefile) then
18 leasefile = s.leasefile
23 local fd = io.open(leasefile, "r")
26 local ln = fd:read("*l")
30 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
31 local expire = tonumber(ts) or 0
32 if ts and mac and ip and name and duid then
33 if family == 4 and not ip:match(":") then
35 expires = (expire ~= 0) and os.difftime(expire, os.time()),
36 macaddr = ipc.checkmac(mac) or "00:00:00:00:00:00",
38 hostname = (name ~= "*") and name
40 elseif family == 6 and ip:match(":") then
42 expires = (expire ~= 0) and os.difftime(expire, os.time()),
44 duid = (duid ~= "*") and duid,
45 hostname = (name ~= "*") and name
54 local lease6file = "/tmp/hosts/odhcpd"
55 uci:foreach("dhcp", "odhcpd",
57 if t.leasefile and nfs.access(t.leasefile) then
58 lease6file = t.leasefile
62 local fd = io.open(lease6file, "r")
65 local ln = fd:read("*l")
69 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
70 local expire = tonumber(ts) or 0
71 if ip and iaid ~= "ipv4" and family == 6 then
73 expires = (expire >= 0) and os.difftime(expire, os.time()),
76 hostname = (name ~= "-") and name
78 elseif ip and iaid == "ipv4" and family == 4 then
80 expires = (expire >= 0) and os.difftime(expire, os.time()),
81 macaddr = sys.net.duid_to_mac(duid) or "00:00:00:00:00:00",
83 hostname = (name ~= "-") and name
93 local hosts = sys.net.host_hints()
94 for _, lease in ipairs(rv) do
95 local mac = sys.net.duid_to_mac(lease.duid)
96 local host = mac and hosts[mac]
98 if not lease.name then
99 lease.host_hint = host.name or host.ipv4 or host.ipv6
100 elseif host.name and lease.hostname ~= host.name then
101 lease.host_hint = host.name
110 function dhcp_leases()
111 return dhcp_leases_common(4)
114 function dhcp6_leases()
115 return dhcp_leases_common(6)
118 function wifi_networks()
120 local ntm = require "luci.model.network".init()
123 for _, dev in ipairs(ntm:get_wifidevs()) do
127 name = dev:get_i18n(),
132 for _, net in ipairs(dev:get_wifinets()) do
134 for _, a in pairs(net:assoclist() or {}) do
138 rd.networks[#rd.networks+1] = {
139 name = net:shortname(),
140 link = net:adminlink(),
142 mode = net:active_mode(),
143 ssid = net:active_ssid(),
144 bssid = net:active_bssid(),
145 encryption = net:active_encryption(),
146 frequency = net:frequency(),
147 channel = net:channel(),
148 signal = net:signal(),
149 quality = net:signal_percent(),
151 bitrate = net:bitrate(),
152 ifname = net:ifname(),
153 country = net:country(),
154 txpower = net:txpower(),
155 txpoweroff = net:txpower_offset(),
157 disabled = (dev:get("disabled") == "1" or
158 net:get("disabled") == "1")
168 function wifi_network(id)
169 local ntm = require "luci.model.network".init()
170 local net = ntm:get_wifinet(id)
172 local dev = net:get_device()
176 name = net:shortname(),
177 link = net:adminlink(),
179 mode = net:active_mode(),
180 ssid = net:active_ssid(),
181 bssid = net:active_bssid(),
182 encryption = net:active_encryption(),
183 frequency = net:frequency(),
184 channel = net:channel(),
185 signal = net:signal(),
186 quality = net:signal_percent(),
188 bitrate = net:bitrate(),
189 ifname = net:ifname(),
190 country = net:country(),
191 txpower = net:txpower(),
192 txpoweroff = net:txpower_offset(),
193 disabled = (dev:get("disabled") == "1" or
194 net:get("disabled") == "1"),
198 name = dev:get_i18n()
206 function wifi_assoclist()
207 local sys = require "luci.sys"
208 local ntm = require "luci.model.network".init()
209 local hosts = sys.net.host_hints()
212 local _, dev, net, bss
214 for _, dev in ipairs(ntm:get_wifidevs()) do
215 local radioname = dev:get_i18n()
217 for _, net in ipairs(dev:get_wifinets()) do
218 local netname = net:shortname()
219 local netlink = net:adminlink()
220 local ifname = net:ifname()
222 for _, bss in pairs(net:assoclist() or {}) do
223 local host = hosts[_]
227 bss.radio = radioname
231 bss.host_name = (host) and (host.name or host.ipv4 or host.ipv6)
232 bss.host_hint = (host and host.name and (host.ipv4 or host.ipv6)) and (host.ipv4 or host.ipv6)
234 assoc[#assoc+1] = bss
239 table.sort(assoc, function(a, b)
240 if a.radio ~= b.radio then
241 return a.radio < b.radio
242 elseif a.ifname ~= b.ifname then
243 return a.ifname < b.ifname
245 return a.bssid < b.bssid
252 function switch_status(devs)
255 for dev in devs:gmatch("[^%s,]+") do
257 local swc = io.popen("swconfig dev %s show"
258 % luci.util.shellquote(dev), "r")
265 local port, up = l:match("port:(%d+) link:(%w+)")
267 local speed = l:match(" speed:(%d+)")
268 local duplex = l:match(" (%w+)-duplex")
269 local txflow = l:match(" (txflow)")
270 local rxflow = l:match(" (rxflow)")
271 local auto = l:match(" (auto)")
274 port = tonumber(port) or 0,
275 speed = tonumber(speed) or 0,
277 duplex = (duplex == "full"),
278 rxflow = (not not rxflow),
279 txflow = (not not txflow),
280 auto = (not not auto)
287 switches[dev] = ports