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 leasefile = "/tmp/dhcp.leases"
14 uci:foreach("dhcp", "dnsmasq",
16 if s.leasefile and nfs.access(s.leasefile) then
17 leasefile = s.leasefile
22 local fd = io.open(leasefile, "r")
25 local ln = fd:read("*l")
29 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
30 local expire = tonumber(ts) or 0
31 if ts and mac and ip and name and duid then
32 if family == 4 and not ip:match(":") then
34 expires = (expire ~= 0) and os.difftime(expire, os.time()),
35 macaddr = ipc.checkmac(mac) or "00:00:00:00:00:00",
37 hostname = (name ~= "*") and name
39 elseif family == 6 and ip:match(":") then
41 expires = (expire ~= 0) and os.difftime(expire, os.time()),
43 duid = (duid ~= "*") and duid,
44 hostname = (name ~= "*") and name
53 local lease6file = "/tmp/hosts/odhcpd"
54 uci:foreach("dhcp", "odhcpd",
56 if t.leasefile and nfs.access(t.leasefile) then
57 lease6file = t.leasefile
61 local fd = io.open(lease6file, "r")
64 local ln = fd:read("*l")
68 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
69 local expire = tonumber(ts) or 0
70 if ip and iaid ~= "ipv4" and family == 6 then
72 expires = (expire >= 0) and os.difftime(expire, os.time()),
75 hostname = (name ~= "-") and name
77 elseif ip and iaid == "ipv4" and family == 4 then
79 expires = (expire >= 0) and os.difftime(expire, os.time()),
80 macaddr = ipc.checkmac(duid:gsub("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$", "%1:%2:%3:%4:%5:%6")) or "00:00:00:00:00:00",
82 hostname = (name ~= "-") and name
93 function dhcp_leases()
94 return dhcp_leases_common(4)
97 function dhcp6_leases()
98 return dhcp_leases_common(6)
101 function wifi_networks()
103 local ntm = require "luci.model.network".init()
106 for _, dev in ipairs(ntm:get_wifidevs()) do
110 name = dev:get_i18n(),
115 for _, net in ipairs(dev:get_wifinets()) do
117 for _, a in pairs(net:assoclist() or {}) do
121 rd.networks[#rd.networks+1] = {
122 name = net:shortname(),
123 link = net:adminlink(),
125 mode = net:active_mode(),
126 ssid = net:active_ssid(),
127 bssid = net:active_bssid(),
128 encryption = net:active_encryption(),
129 frequency = net:frequency(),
130 channel = net:channel(),
131 signal = net:signal(),
132 quality = net:signal_percent(),
134 bitrate = net:bitrate(),
135 ifname = net:ifname(),
136 country = net:country(),
137 txpower = net:txpower(),
138 txpoweroff = net:txpower_offset(),
140 disabled = (dev:get("disabled") == "1" or
141 net:get("disabled") == "1")
151 function wifi_network(id)
152 local ntm = require "luci.model.network".init()
153 local net = ntm:get_wifinet(id)
155 local dev = net:get_device()
159 name = net:shortname(),
160 link = net:adminlink(),
162 mode = net:active_mode(),
163 ssid = net:active_ssid(),
164 bssid = net:active_bssid(),
165 encryption = net:active_encryption(),
166 frequency = net:frequency(),
167 channel = net:channel(),
168 signal = net:signal(),
169 quality = net:signal_percent(),
171 bitrate = net:bitrate(),
172 ifname = net:ifname(),
173 country = net:country(),
174 txpower = net:txpower(),
175 txpoweroff = net:txpower_offset(),
176 disabled = (dev:get("disabled") == "1" or
177 net:get("disabled") == "1"),
181 name = dev:get_i18n()
189 function wifi_assoclist()
190 local sys = require "luci.sys"
191 local ntm = require "luci.model.network".init()
192 local hosts = sys.net.host_hints()
195 local _, dev, net, bss
197 for _, dev in ipairs(ntm:get_wifidevs()) do
198 local radioname = dev:get_i18n()
200 for _, net in ipairs(dev:get_wifinets()) do
201 local netname = net:shortname()
202 local netlink = net:adminlink()
203 local ifname = net:ifname()
205 for _, bss in pairs(net:assoclist() or {}) do
206 local host = hosts[_]
210 bss.radio = radioname
214 bss.host_name = (host) and (host.name or host.ipv4 or host.ipv6)
215 bss.host_hint = (host and host.name and (host.ipv4 or host.ipv6)) and (host.ipv4 or host.ipv6)
217 assoc[#assoc+1] = bss
222 table.sort(assoc, function(a, b)
223 if a.radio ~= b.radio then
224 return a.radio < b.radio
225 elseif a.ifname ~= b.ifname then
226 return a.ifname < b.ifname
228 return a.bssid < b.bssid
235 function switch_status(devs)
238 for dev in devs:gmatch("[^%s,]+") do
240 local swc = io.popen("swconfig dev %s show"
241 % luci.util.shellquote(dev), "r")
248 local port, up = l:match("port:(%d+) link:(%w+)")
250 local speed = l:match(" speed:(%d+)")
251 local duplex = l:match(" (%w+)-duplex")
252 local txflow = l:match(" (txflow)")
253 local rxflow = l:match(" (rxflow)")
254 local auto = l:match(" (auto)")
257 port = tonumber(port) or 0,
258 speed = tonumber(speed) or 0,
260 duplex = (duplex == "full"),
261 rxflow = (not not rxflow),
262 txflow = (not not txflow),
263 auto = (not not auto)
270 switches[dev] = ports