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
116 rd.networks[#rd.networks+1] = {
117 name = net:shortname(),
118 link = net:adminlink(),
120 mode = net:active_mode(),
121 ssid = net:active_ssid(),
122 bssid = net:active_bssid(),
123 encryption = net:active_encryption(),
124 frequency = net:frequency(),
125 channel = net:channel(),
126 signal = net:signal(),
127 quality = net:signal_percent(),
129 bitrate = net:bitrate(),
130 ifname = net:ifname(),
131 assoclist = net:assoclist(),
132 country = net:country(),
133 txpower = net:txpower(),
134 txpoweroff = net:txpower_offset(),
135 disabled = (dev:get("disabled") == "1" or
136 net:get("disabled") == "1")
146 function wifi_network(id)
147 local ntm = require "luci.model.network".init()
148 local net = ntm:get_wifinet(id)
150 local dev = net:get_device()
154 name = net:shortname(),
155 link = net:adminlink(),
157 mode = net:active_mode(),
158 ssid = net:active_ssid(),
159 bssid = net:active_bssid(),
160 encryption = net:active_encryption(),
161 frequency = net:frequency(),
162 channel = net:channel(),
163 signal = net:signal(),
164 quality = net:signal_percent(),
166 bitrate = net:bitrate(),
167 ifname = net:ifname(),
168 assoclist = net:assoclist(),
169 country = net:country(),
170 txpower = net:txpower(),
171 txpoweroff = net:txpower_offset(),
172 disabled = (dev:get("disabled") == "1" or
173 net:get("disabled") == "1"),
177 name = dev:get_i18n()
185 function switch_status(devs)
188 for dev in devs:gmatch("[^%s,]+") do
190 local swc = io.popen("swconfig dev %s show"
191 % luci.util.shellquote(dev), "r")
198 local port, up = l:match("port:(%d+) link:(%w+)")
200 local speed = l:match(" speed:(%d+)")
201 local duplex = l:match(" (%w+)-duplex")
202 local txflow = l:match(" (txflow)")
203 local rxflow = l:match(" (rxflow)")
204 local auto = l:match(" (auto)")
207 port = tonumber(port) or 0,
208 speed = tonumber(speed) or 0,
210 duplex = (duplex == "full"),
211 rxflow = (not not rxflow),
212 txflow = (not not txflow),
213 auto = (not not auto)
220 switches[dev] = ports