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(),
124 disabled = (dev:get("disabled") == "1" or
125 net:get("disabled") == "1")
135 function wifi_network(id)
136 local ntm = require "luci.model.network".init()
137 local net = ntm:get_wifinet(id)
139 local dev = net:get_device()
143 name = net:shortname(),
144 link = net:adminlink(),
146 mode = net:active_mode(),
147 ssid = net:active_ssid(),
148 bssid = net:active_bssid(),
149 encryption = net:active_encryption(),
150 frequency = net:frequency(),
151 channel = net:channel(),
152 signal = net:signal(),
153 quality = net:signal_percent(),
155 bitrate = net:bitrate(),
156 ifname = net:ifname(),
157 assoclist = net:assoclist(),
158 country = net:country(),
159 txpower = net:txpower(),
160 txpoweroff = net:txpower_offset(),
161 disabled = (dev:get("disabled") == "1" or
162 net:get("disabled") == "1"),
166 name = dev:get_i18n()
174 function switch_status(devs)
177 for dev in devs:gmatch("[^%s,]+") do
179 local swc = io.popen("swconfig dev %q show" % dev, "r")
185 local port, up = l:match("port:(%d+) link:(%w+)")
187 local speed = l:match(" speed:(%d+)")
188 local duplex = l:match(" (%w+)-duplex")
189 local txflow = l:match(" (txflow)")
190 local rxflow = l:match(" (rxflow)")
191 local auto = l:match(" (auto)")
194 port = tonumber(port) or 0,
195 speed = tonumber(speed) or 0,
197 duplex = (duplex == "full"),
198 rxflow = (not not rxflow),
199 txflow = (not not txflow),
200 auto = (not not auto)
207 switches[dev] = ports