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
77 expires = (expire >= 0) and os.difftime(expire, os.time()),
80 hostname = (name ~= "-") and name
91 function dhcp_leases()
92 return dhcp_leases_common(4)
95 function dhcp6_leases()
96 return dhcp_leases_common(6)
99 function wifi_networks()
101 local ntm = require "luci.model.network".init()
104 for _, dev in ipairs(ntm:get_wifidevs()) do
108 name = dev:get_i18n(),
113 for _, net in ipairs(dev:get_wifinets()) do
114 rd.networks[#rd.networks+1] = {
115 name = net:shortname(),
116 link = net:adminlink(),
118 mode = net:active_mode(),
119 ssid = net:active_ssid(),
120 bssid = net:active_bssid(),
121 encryption = net:active_encryption(),
122 frequency = net:frequency(),
123 channel = net:channel(),
124 signal = net:signal(),
125 quality = net:signal_percent(),
127 bitrate = net:bitrate(),
128 ifname = net:ifname(),
129 assoclist = net:assoclist(),
130 country = net:country(),
131 txpower = net:txpower(),
132 txpoweroff = net:txpower_offset(),
133 disabled = (dev:get("disabled") == "1" or
134 net:get("disabled") == "1")
144 function wifi_network(id)
145 local ntm = require "luci.model.network".init()
146 local net = ntm:get_wifinet(id)
148 local dev = net:get_device()
152 name = net:shortname(),
153 link = net:adminlink(),
155 mode = net:active_mode(),
156 ssid = net:active_ssid(),
157 bssid = net:active_bssid(),
158 encryption = net:active_encryption(),
159 frequency = net:frequency(),
160 channel = net:channel(),
161 signal = net:signal(),
162 quality = net:signal_percent(),
164 bitrate = net:bitrate(),
165 ifname = net:ifname(),
166 assoclist = net:assoclist(),
167 country = net:country(),
168 txpower = net:txpower(),
169 txpoweroff = net:txpower_offset(),
170 disabled = (dev:get("disabled") == "1" or
171 net:get("disabled") == "1"),
175 name = dev:get_i18n()
183 function switch_status(devs)
186 for dev in devs:gmatch("[^%s,]+") do
188 local swc = io.popen("swconfig dev %q show" % dev, "r")
194 local port, up = l:match("port:(%d+) link:(%w+)")
196 local speed = l:match(" speed:(%d+)")
197 local duplex = l:match(" (%w+)-duplex")
198 local txflow = l:match(" (txflow)")
199 local rxflow = l:match(" (rxflow)")
200 local auto = l:match(" (auto)")
203 port = tonumber(port) or 0,
204 speed = tonumber(speed) or 0,
206 duplex = (duplex == "full"),
207 rxflow = (not not rxflow),
208 txflow = (not not txflow),
209 auto = (not not auto)
216 switches[dev] = ports