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()
8 local function dhcp_leases_common(family)
10 local nfs = require "nixio.fs"
11 local leasefile = "/tmp/dhcp.leases"
13 uci:foreach("dhcp", "dnsmasq",
15 if s.leasefile and nfs.access(s.leasefile) then
16 leasefile = s.leasefile
21 local fd = io.open(leasefile, "r")
24 local ln = fd:read("*l")
28 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
29 if ts and mac and ip and name and duid then
30 if family == 4 and not ip:match(":") then
32 expires = os.difftime(tonumber(ts) or 0, os.time()),
35 hostname = (name ~= "*") and name
37 elseif family == 6 and ip:match(":") then
39 expires = os.difftime(tonumber(ts) or 0, os.time()),
41 duid = (duid ~= "*") and duid,
42 hostname = (name ~= "*") and name
51 local lease6file = "/tmp/hosts/odhcpd"
52 uci:foreach("dhcp", "odhcpd",
54 if t.leasefile and nfs.access(t.leasefile) then
55 lease6file = t.leasefile
59 local fd = io.open(lease6file, "r")
62 local ln = fd:read("*l")
66 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (-?%d+) (%S+) (%S+) (.*)")
67 local expire = tonumber(ts) or 0
68 if ip and iaid ~= "ipv4" and family == 6 then
70 expires = (expire >= 0) and os.difftime(expire, os.time()),
73 hostname = (name ~= "-") and name
75 elseif ip and iaid == "ipv4" and family == 4 then
76 local mac, mac1, mac2, mac3, mac4, mac5, mac6
77 if duid and type(duid) == "string" then
78 mac1, mac2, mac3, mac4, mac5, mac6 = duid:match("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
80 if not (mac1 and mac2 and mac3 and mac4 and mac5 and mac6) then
81 mac = "FF:FF:FF:FF:FF:FF"
83 mac = mac1..":"..mac2..":"..mac3..":"..mac4..":"..mac5..":"..mac6
86 expires = (expire >= 0) and os.difftime(expire, os.time()),
88 macaddr = mac:lower(),
90 hostname = (name ~= "-") and name
101 function dhcp_leases()
102 return dhcp_leases_common(4)
105 function dhcp6_leases()
106 return dhcp_leases_common(6)
109 function wifi_networks()
111 local ntm = require "luci.model.network".init()
114 for _, dev in ipairs(ntm:get_wifidevs()) do
118 name = dev:get_i18n(),
123 for _, net in ipairs(dev:get_wifinets()) do
124 rd.networks[#rd.networks+1] = {
125 name = net:shortname(),
126 link = net:adminlink(),
128 mode = net:active_mode(),
129 ssid = net:active_ssid(),
130 bssid = net:active_bssid(),
131 encryption = net:active_encryption(),
132 frequency = net:frequency(),
133 channel = net:channel(),
134 signal = net:signal(),
135 quality = net:signal_percent(),
137 bitrate = net:bitrate(),
138 ifname = net:ifname(),
139 assoclist = net:assoclist(),
140 country = net:country(),
141 txpower = net:txpower(),
142 txpoweroff = net:txpower_offset(),
143 disabled = (dev:get("disabled") == "1" or
144 net:get("disabled") == "1")
154 function wifi_network(id)
155 local ntm = require "luci.model.network".init()
156 local net = ntm:get_wifinet(id)
158 local dev = net:get_device()
162 name = net:shortname(),
163 link = net:adminlink(),
165 mode = net:active_mode(),
166 ssid = net:active_ssid(),
167 bssid = net:active_bssid(),
168 encryption = net:active_encryption(),
169 frequency = net:frequency(),
170 channel = net:channel(),
171 signal = net:signal(),
172 quality = net:signal_percent(),
174 bitrate = net:bitrate(),
175 ifname = net:ifname(),
176 assoclist = net:assoclist(),
177 country = net:country(),
178 txpower = net:txpower(),
179 txpoweroff = net:txpower_offset(),
180 disabled = (dev:get("disabled") == "1" or
181 net:get("disabled") == "1"),
185 name = dev:get_i18n()
193 function switch_status(devs)
196 for dev in devs:gmatch("[^%s,]+") do
198 local swc = io.popen("swconfig dev %q show" % dev, "r")
204 local port, up = l:match("port:(%d+) link:(%w+)")
206 local speed = l:match(" speed:(%d+)")
207 local duplex = l:match(" (%w+)-duplex")
208 local txflow = l:match(" (txflow)")
209 local rxflow = l:match(" (rxflow)")
210 local auto = l:match(" (auto)")
213 port = tonumber(port) or 0,
214 speed = tonumber(speed) or 0,
216 duplex = (duplex == "full"),
217 rxflow = (not not rxflow),
218 txflow = (not not txflow),
219 auto = (not not auto)
226 switches[dev] = ports