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 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
33 expires = (expire ~= 0) and os.difftime(expire, os.time()),
36 hostname = (name ~= "*") and name
38 elseif family == 6 and ip:match(":") then
40 expires = (expire ~= 0) and os.difftime(expire, os.time()),
42 duid = (duid ~= "*") and duid,
43 hostname = (name ~= "*") and name
52 local lease6file = "/tmp/hosts/odhcpd"
53 uci:foreach("dhcp", "odhcpd",
55 if t.leasefile and nfs.access(t.leasefile) then
56 lease6file = t.leasefile
60 local fd = io.open(lease6file, "r")
63 local ln = fd:read("*l")
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
71 expires = (expire >= 0) and os.difftime(expire, os.time()),
74 hostname = (name ~= "-") and name
76 elseif ip and iaid == "ipv4" and family == 4 then
77 local mac, mac1, mac2, mac3, mac4, mac5, mac6
78 if duid and type(duid) == "string" then
79 mac1, mac2, mac3, mac4, mac5, mac6 = duid:match("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
81 if not (mac1 and mac2 and mac3 and mac4 and mac5 and mac6) then
82 mac = "FF:FF:FF:FF:FF:FF"
84 mac = mac1..":"..mac2..":"..mac3..":"..mac4..":"..mac5..":"..mac6
87 expires = (expire >= 0) and os.difftime(expire, os.time()),
89 macaddr = mac:lower(),
91 hostname = (name ~= "-") and name
102 function dhcp_leases()
103 return dhcp_leases_common(4)
106 function dhcp6_leases()
107 return dhcp_leases_common(6)
110 function wifi_networks()
112 local ntm = require "luci.model.network".init()
115 for _, dev in ipairs(ntm:get_wifidevs()) do
119 name = dev:get_i18n(),
124 for _, net in ipairs(dev:get_wifinets()) do
125 rd.networks[#rd.networks+1] = {
126 name = net:shortname(),
127 link = net:adminlink(),
129 mode = net:active_mode(),
130 ssid = net:active_ssid(),
131 bssid = net:active_bssid(),
132 encryption = net:active_encryption(),
133 frequency = net:frequency(),
134 channel = net:channel(),
135 signal = net:signal(),
136 quality = net:signal_percent(),
138 bitrate = net:bitrate(),
139 ifname = net:ifname(),
140 assoclist = net:assoclist(),
141 country = net:country(),
142 txpower = net:txpower(),
143 txpoweroff = net:txpower_offset(),
144 disabled = (dev:get("disabled") == "1" or
145 net:get("disabled") == "1")
155 function wifi_network(id)
156 local ntm = require "luci.model.network".init()
157 local net = ntm:get_wifinet(id)
159 local dev = net:get_device()
163 name = net:shortname(),
164 link = net:adminlink(),
166 mode = net:active_mode(),
167 ssid = net:active_ssid(),
168 bssid = net:active_bssid(),
169 encryption = net:active_encryption(),
170 frequency = net:frequency(),
171 channel = net:channel(),
172 signal = net:signal(),
173 quality = net:signal_percent(),
175 bitrate = net:bitrate(),
176 ifname = net:ifname(),
177 assoclist = net:assoclist(),
178 country = net:country(),
179 txpower = net:txpower(),
180 txpoweroff = net:txpower_offset(),
181 disabled = (dev:get("disabled") == "1" or
182 net:get("disabled") == "1"),
186 name = dev:get_i18n()
194 function switch_status(devs)
197 for dev in devs:gmatch("[^%s,]+") do
199 local swc = io.popen("swconfig dev %q show" % dev, "r")
205 local port, up = l:match("port:(%d+) link:(%w+)")
207 local speed = l:match(" speed:(%d+)")
208 local duplex = l:match(" (%w+)-duplex")
209 local txflow = l:match(" (txflow)")
210 local rxflow = l:match(" (rxflow)")
211 local auto = l:match(" (auto)")
214 port = tonumber(port) or 0,
215 speed = tonumber(speed) or 0,
217 duplex = (duplex == "full"),
218 rxflow = (not not rxflow),
219 txflow = (not not txflow),
220 auto = (not not auto)
227 switches[dev] = ports