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 = "/var/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 if ip and iaid ~= "ipv4" and family == 6 then
69 expires = os.difftime(tonumber(ts) or 0, os.time()),
72 hostname = (name ~= "-") and name
74 elseif ip and iaid == "ipv4" and family == 4 then
76 expires = os.difftime(tonumber(ts) or 0, os.time()),
79 hostname = (name ~= "-") and name
90 function dhcp_leases()
91 return dhcp_leases_common(4)
94 function dhcp6_leases()
95 return dhcp_leases_common(6)
98 function wifi_networks()
100 local ntm = require "luci.model.network".init()
103 for _, dev in ipairs(ntm:get_wifidevs()) do
107 name = dev:get_i18n(),
112 for _, net in ipairs(dev:get_wifinets()) do
113 rd.networks[#rd.networks+1] = {
114 name = net:shortname(),
115 link = net:adminlink(),
117 mode = net:active_mode(),
118 ssid = net:active_ssid(),
119 bssid = net:active_bssid(),
120 encryption = net:active_encryption(),
121 frequency = net:frequency(),
122 channel = net:channel(),
123 signal = net:signal(),
124 quality = net:signal_percent(),
126 bitrate = net:bitrate(),
127 ifname = net:ifname(),
128 assoclist = net:assoclist(),
129 country = net:country(),
130 txpower = net:txpower(),
131 txpoweroff = net:txpower_offset(),
132 disabled = (dev:get("disabled") == "1" or
133 net:get("disabled") == "1")
143 function wifi_network(id)
144 local ntm = require "luci.model.network".init()
145 local net = ntm:get_wifinet(id)
147 local dev = net:get_device()
151 name = net:shortname(),
152 link = net:adminlink(),
154 mode = net:active_mode(),
155 ssid = net:active_ssid(),
156 bssid = net:active_bssid(),
157 encryption = net:active_encryption(),
158 frequency = net:frequency(),
159 channel = net:channel(),
160 signal = net:signal(),
161 quality = net:signal_percent(),
163 bitrate = net:bitrate(),
164 ifname = net:ifname(),
165 assoclist = net:assoclist(),
166 country = net:country(),
167 txpower = net:txpower(),
168 txpoweroff = net:txpower_offset(),
169 disabled = (dev:get("disabled") == "1" or
170 net:get("disabled") == "1"),
174 name = dev:get_i18n()
182 function switch_status(devs)
185 for dev in devs:gmatch("[^%s,]+") do
187 local swc = io.popen("swconfig dev %q show" % dev, "r")
193 local port, up = l:match("port:(%d+) link:(%w+)")
195 local speed = l:match(" speed:(%d+)")
196 local duplex = l:match(" (%w+)-duplex")
197 local txflow = l:match(" (txflow)")
198 local rxflow = l:match(" (rxflow)")
199 local auto = l:match(" (auto)")
202 port = tonumber(port) or 0,
203 speed = tonumber(speed) or 0,
205 duplex = (duplex == "full"),
206 rxflow = (not not rxflow),
207 txflow = (not not txflow),
208 auto = (not not auto)
215 switches[dev] = ports