Add support for stateful DHCPv6
[project/luci.git] / modules / admin-core / luasrc / tools / status.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 ]]--
13
14 module("luci.tools.status", package.seeall)
15
16 local uci = require "luci.model.uci".cursor()
17
18 local function dhcp_leases_common(family)
19 local rv = { }
20 local nfs = require "nixio.fs"
21 local leasefile = "/var/dhcp.leases"
22
23 uci:foreach("dhcp", "dnsmasq",
24 function(s)
25 if s.leasefile and nfs.access(s.leasefile) then
26 leasefile = s.leasefile
27 return false
28 end
29 end)
30
31 local fd = io.open(leasefile, "r")
32 if fd then
33 while true do
34 local ln = fd:read("*l")
35 if not ln then
36 break
37 else
38 local ts, mac, ip, name, duid = ln:match("^(%d+) (%S+) (%S+) (%S+) (%S+)")
39 if ts and mac and ip and name and duid then
40 if family == 4 and not ip:match(":") then
41 rv[#rv+1] = {
42 expires = os.difftime(tonumber(ts) or 0, os.time()),
43 macaddr = mac,
44 ipaddr = ip,
45 hostname = (name ~= "*") and name
46 }
47 elseif family == 6 and ip:match(":") then
48 rv[#rv+1] = {
49 expires = os.difftime(tonumber(ts) or 0, os.time()),
50 ip6addr = ip,
51 duid = (duid ~= "*") and duid,
52 hostname = (name ~= "*") and name
53 }
54 end
55 end
56 end
57 end
58 fd:close()
59 end
60
61 return rv
62 end
63
64 function dhcp_leases()
65 return dhcp_leases_common(4)
66 end
67
68 function dhcp6_leases()
69 local nfs = require "nixio.fs"
70 local leasefile = "/tmp/hosts/6relayd"
71 local rv = {}
72
73 if nfs.access(leasefile, "r") then
74 local fd = io.open(leasefile, "r")
75 if fd then
76 while true do
77 local ln = fd:read("*l")
78 if not ln then
79 break
80 else
81 local iface, duid, iaid, name, ts, id, length, ip = ln:match("^# (%S+) (%S+) (%S+) (%S+) (%d+) (%S+) (%S+) (.*)")
82 if ip then
83 rv[#rv+1] = {
84 expires = os.difftime(tonumber(ts) or 0, os.time()),
85 duid = duid,
86 ip6addr = ip,
87 hostname = (name ~= "-") and name
88 }
89 end
90 end
91 end
92 fd:close()
93 end
94 return rv
95 elseif luci.sys.call("dnsmasq --version 2>/dev/null | grep -q ' DHCPv6 '") == 0 then
96 return dhcp_leases_common(6)
97 end
98 end
99
100 function wifi_networks()
101 local rv = { }
102 local ntm = require "luci.model.network".init()
103
104 local dev
105 for _, dev in ipairs(ntm:get_wifidevs()) do
106 local rd = {
107 up = dev:is_up(),
108 device = dev:name(),
109 name = dev:get_i18n(),
110 networks = { }
111 }
112
113 local net
114 for _, net in ipairs(dev:get_wifinets()) do
115 rd.networks[#rd.networks+1] = {
116 name = net:shortname(),
117 link = net:adminlink(),
118 up = net:is_up(),
119 mode = net:active_mode(),
120 ssid = net:active_ssid(),
121 bssid = net:active_bssid(),
122 encryption = net:active_encryption(),
123 frequency = net:frequency(),
124 channel = net:channel(),
125 signal = net:signal(),
126 quality = net:signal_percent(),
127 noise = net:noise(),
128 bitrate = net:bitrate(),
129 ifname = net:ifname(),
130 assoclist = net:assoclist(),
131 country = net:country(),
132 txpower = net:txpower(),
133 txpoweroff = net:txpower_offset()
134 }
135 end
136
137 rv[#rv+1] = rd
138 end
139
140 return rv
141 end
142
143 function wifi_network(id)
144 local ntm = require "luci.model.network".init()
145 local net = ntm:get_wifinet(id)
146 if net then
147 local dev = net:get_device()
148 if dev then
149 return {
150 id = id,
151 name = net:shortname(),
152 link = net:adminlink(),
153 up = net:is_up(),
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(),
162 noise = net:noise(),
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 device = {
170 up = dev:is_up(),
171 device = dev:name(),
172 name = dev:get_i18n()
173 }
174 }
175 end
176 end
177 return { }
178 end
179
180 function switch_status(devs)
181 local dev
182 local switches = { }
183 for dev in devs:gmatch("[^%s,]+") do
184 local ports = { }
185 local swc = io.popen("swconfig dev %q show" % dev, "r")
186 if swc then
187 local l
188 repeat
189 l = swc:read("*l")
190 if l then
191 local port, up = l:match("port:(%d+) link:(%w+)")
192 if port then
193 local speed = l:match(" speed:(%d+)")
194 local duplex = l:match(" (%w+)-duplex")
195 local txflow = l:match(" (txflow)")
196 local rxflow = l:match(" (rxflow)")
197 local auto = l:match(" (auto)")
198
199 ports[#ports+1] = {
200 port = tonumber(port) or 0,
201 speed = tonumber(speed) or 0,
202 link = (up == "up"),
203 duplex = (duplex == "full"),
204 rxflow = (not not rxflow),
205 txflow = (not not txflow),
206 auto = (not not auto)
207 }
208 end
209 end
210 until not l
211 swc:close()
212 end
213 switches[dev] = ports
214 end
215 return switches
216 end