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 fd = io.open("/tmp/hosts/odhcpd", "r")
54 local ln = fd:read("*l")
58 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (%d+) (%S+) (%S+) (.*)")
59 if ip and iaid ~= "ipv4" and family == 6 then
61 expires = os.difftime(tonumber(ts) or 0, os.time()),
64 hostname = (name ~= "-") and name
66 elseif ip and iaid == "ipv4" and family == 4 then
68 expires = os.difftime(tonumber(ts) or 0, os.time()),
71 hostname = (name ~= "-") and name
82 function dhcp_leases()
83 return dhcp_leases_common(4)
86 function dhcp6_leases()
87 return dhcp_leases_common(6)
90 function wifi_networks()
92 local ntm = require "luci.model.network".init()
95 for _, dev in ipairs(ntm:get_wifidevs()) do
99 name = dev:get_i18n(),
104 for _, net in ipairs(dev:get_wifinets()) do
105 rd.networks[#rd.networks+1] = {
106 name = net:shortname(),
107 link = net:adminlink(),
109 mode = net:active_mode(),
110 ssid = net:active_ssid(),
111 bssid = net:active_bssid(),
112 encryption = net:active_encryption(),
113 frequency = net:frequency(),
114 channel = net:channel(),
115 signal = net:signal(),
116 quality = net:signal_percent(),
118 bitrate = net:bitrate(),
119 ifname = net:ifname(),
120 assoclist = net:assoclist(),
121 country = net:country(),
122 txpower = net:txpower(),
123 txpoweroff = net:txpower_offset()
133 function wifi_network(id)
134 local ntm = require "luci.model.network".init()
135 local net = ntm:get_wifinet(id)
137 local dev = net:get_device()
141 name = net:shortname(),
142 link = net:adminlink(),
144 mode = net:active_mode(),
145 ssid = net:active_ssid(),
146 bssid = net:active_bssid(),
147 encryption = net:active_encryption(),
148 frequency = net:frequency(),
149 channel = net:channel(),
150 signal = net:signal(),
151 quality = net:signal_percent(),
153 bitrate = net:bitrate(),
154 ifname = net:ifname(),
155 assoclist = net:assoclist(),
156 country = net:country(),
157 txpower = net:txpower(),
158 txpoweroff = net:txpower_offset(),
159 disabled = (dev:get("disabled") == "1" or
160 net:get("disabled") == "1"),
164 name = dev:get_i18n()
172 function switch_status(devs)
175 for dev in devs:gmatch("[^%s,]+") do
177 local swc = io.popen("swconfig dev %q show" % dev, "r")
183 local port, up = l:match("port:(%d+) link:(%w+)")
185 local speed = l:match(" speed:(%d+)")
186 local duplex = l:match(" (%w+)-duplex")
187 local txflow = l:match(" (txflow)")
188 local rxflow = l:match(" (rxflow)")
189 local auto = l:match(" (auto)")
192 port = tonumber(port) or 0,
193 speed = tonumber(speed) or 0,
195 duplex = (duplex == "full"),
196 rxflow = (not not rxflow),
197 txflow = (not not txflow),
198 auto = (not not auto)
205 switches[dev] = ports