modules/admin-full: implement interface status page
[project/luci.git] / modules / admin-full / luasrc / view / admin_status / interfaces.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2008 Steven Barth <steven@midlink.org>
4 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
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 $Id$
13
14 -%>
15
16 <%-
17
18 require "luci.tools.webadmin"
19
20 local wba = luci.tools.webadmin
21 local uci = luci.model.uci.cursor_state()
22
23 local bridge_ifs = { }
24 local single_ifs = { }
25 local wifi_ifs = { }
26 local devinfo = luci.sys.net.deviceinfo()
27
28 uci:foreach("network", "interface",
29 function(s)
30 if s['.name'] ~= "loopback" then
31 if s.type == "bridge" then
32 bridge_ifs[#bridge_ifs+1] = s
33 else
34 single_ifs[#single_ifs+1] = s
35 end
36 end
37 end)
38
39 uci:foreach("wireless", "wifi-iface",
40 function(s)
41 wifi_ifs[s.network or s.device] = true
42 end)
43
44 function is_wifi(i)
45 return wifi_ifs[i]
46 or i:match("^wl%d+")
47 or i:match("^ath%d+")
48 or i:match("^wlan%d+")
49 end
50
51 function get_ifname(s)
52 return s.ifname and s.ifname:match("%S+")
53 end
54
55 function get_ifnames(s)
56 local l = { }
57 if s.ifname then
58 for n in s.ifname:gmatch("%S+") do
59 l[#l+1] = n
60 end
61 end
62 return l
63 end
64
65 function get_vlan(i)
66 return i:match("^%w+%.(%d+)$")
67 end
68
69 function get_vlan_ports(i)
70 local x = get_vlan(i)
71 local d = i:match("(%d+)%.%d+$")
72 local p = { }
73
74 uci:foreach("network", "switch",
75 function(s)
76 local d2 = s['.name']:match("%d+$")
77 if d2 == d and s["vlan"..x] then
78 for pt in s["vlan"..x]:gmatch("%S+") do
79 p[#p+1] = pt
80 end
81 end
82 end)
83
84 return p
85 end
86
87 function get_switch_driver(i)
88 local n, d = i:match("([a-z]+)(%d+)%.%d+$")
89 local hw = luci.fs.readfile("/proc/switch/%s%s/driver" %{ n, d })
90 or luci.fs.readfile("/proc/switch/%s/driver" % d )
91
92 return hw and hw:match("%S+")
93 end
94
95 function get_mac(i)
96 for l in luci.util.execi("ifconfig %q" % i) do
97 if l:find("HWaddr ") then
98 return l:match("HWaddr (%S+)")
99 end
100 end
101 return "00:00:00:00:00:00"
102 end
103
104 function get_aliases(s)
105 local a = { }
106 uci:foreach("network", "alias",
107 function(s2)
108 if s2.interface == s['.name'] then
109 a[#a+1] = s2
110 end
111 end)
112 return a
113 end
114
115 function get_iwinfo(i)
116 local w = { }
117 uci:foreach("wireless", "wifi-iface",
118 function(s)
119 if s.ifname == i then
120 w.type = uci:get("wireless", s.device, "type")
121 w.channel = uci:get("wireless", s.device, "channel")
122 w.mode = ( s.wds == "1" ) and s.mode .. "wds" or s.mode
123 w.ssid = s.ssid
124 w.type = w.type and w.type:gsub("^([a-z])", string.upper)
125 end
126 end)
127 return w
128 end
129
130 function get_iwmode(w)
131 local m = {
132 ap = translate("a_s_if_iwmode_ap", "Master"),
133 sta = translate("a_s_if_iwmode_sta", "Client"),
134 wds = translate("a_s_if_iwmode_wds", "WDS"),
135 stawds = translate("a_s_if_iwmode_stawds", "Client + WDS"),
136 apwds = translate("a_s_if_iwmode_apwds", "Master + WDS"),
137 adhoc = translate("a_s_if_iwmode_adhoc", "Ad-Hoc"),
138 ahdemo = translate("a_s_if_iwmode_ahdemo", "Pseudo Ad-Hoc")
139 }
140
141 return m[w.mode] or w.mode
142 end
143
144 function get_brinfo(s)
145 local b = { }
146 for l in luci.util.execi("brctl show br-%s" % s['.name']) do
147 if not l:match("STP") then
148 local r = luci.util.split(l, "%s+", nil, true)
149 if #r > 2 then
150 b.name = r[1]
151 b.id = r[2]
152 b.stp = r[3] == "yes"
153 b.ifnames = { r[4] }
154 else
155 b.ifnames[#b.ifnames+1] = r[2]
156 end
157 end
158 end
159 return b
160 end
161
162 -%>
163
164 <%+header%>
165
166 <h2><a id="content" name="content"><%:a_s_if_status Interface Status%></a></h2>
167
168 <form method="post" action="<%=REQUEST_URI%>">
169 <div class="cbi-map">
170 <fieldset class="cbi-section">
171 <% for _, i in ipairs(single_ifs) do
172 dev = get_ifname(i)
173 vlan = get_vlan(dev)
174 %>
175 <h3><%:a_s_if_interface Interface%> <%=i['.name']%></h3>
176 <p style="font-size:90%;padding-left:1em">
177
178 <strong><%:a_s_if_device Device%>:</strong>
179 <%=dev%> (<%:a_s_if_mac MAC%> <%=get_mac(dev)%>)<br />
180
181 <strong><%:a_s_if_type Type%>:</strong>
182 <% if is_wifi(dev) then iw = get_iwinfo(dev) -%>
183 <%:a_s_if_wifidev Wireless Adapter%> (<%=iw.type%>)<br />
184 <% if iw then %>
185 &nbsp; &#x2514; <strong><%:a_s_if_iwmode Mode%>:</strong> <%=get_iwmode(iw)%><br />
186 &nbsp; &#x2514; <strong><%:a_s_if_iwssid SSID%>:</strong> <%=iw.ssid%><br />
187 &nbsp; &#x2514; <strong><%:a_s_if_iwchannel Channel%>:</strong> <%=iw.channel%>
188 <% end %>
189 <% else -%>
190 <% if vlan then %>
191 <%:a_s_if_ethswitch Ethernet Switch%> (<%=get_switch_driver(dev)%>)<br />
192 &nbsp; &#x2514; <strong><%:a_s_if_vlan VLAN%>:</strong> <%=get_vlan(dev)%> (<%:a_s_if_vlanports Ports%> <%=table.concat(get_vlan_ports(dev), ", ")%>)
193 <% else %>
194 <%:a_s_if_ethdev Ethernet Adapter%>
195 <% end %>
196 <% end -%><br />
197
198 <strong><%:a_s_if_transfer Transfer%></strong><br />
199 &nbsp; &#x2514; <strong><%:a_s_if_transfer_rx RX%>:</strong> <%=devinfo[dev][2]%> <%:a_s_if_pkts Pkts.%> (<%=wba.byte_format(tonumber(devinfo[dev][1]))%>)<br />
200 &nbsp; &#x2514; <strong><%:a_s_if_transfer_tx TX%>:</strong> <%=devinfo[dev][10]%> <%:a_s_if_pkts Pkts.%> (<%=wba.byte_format(tonumber(devinfo[dev][9]))%>)<br />
201
202 <%- if ( i.ipaddr and #i.ipaddr > 0 ) or ( i.ip6addr and #i.ip6addr > 0 ) then -%>
203 <strong><%:a_s_if_ipconfig IP Configuration%></strong><br />
204 &nbsp; &#x2514; <strong><%:a_s_if_ipconfig_primary Primary%>:</strong>
205 <% if i.ipaddr and #i.ipaddr > 0 then %>
206 <%=i.ipaddr%>/<%=i.netmask%>
207 <% if i.proto == "dhcp" then -%>
208 (<%:a_s_if_ipconfig_dhcp DHCP assigned%>)
209 <%- end %>
210 <% else %>
211 <em><%:a_s_if_ipconfig_none Not configured%></em>
212 <% end %><br />
213
214 <% for i, a in ipairs(get_aliases(i)) do %>
215 &nbsp; &#x2514; <strong><%:a_s_if_ipconfig_alias Alias%> #<%=i%>:</strong>
216 <%=a.ipaddr%>/<%=a.netmask%> (<%:a_s_if_device Device%> <%=dev%>:<%=i%>) <br />
217 <% end %>
218
219 <% if i.ip6addr and #i.ip6addr > 0 then %>
220 &nbsp; &#x2514; <strong><%:a_s_if_ipconfig_ipv6 IPv6%>:</strong> <%=i.ip6addr%><br />
221 <% end %>
222 <%- end -%>
223 <br /></p>
224 <% end %>
225
226
227 <% for _, b in ipairs(bridge_ifs) do
228 br = get_brinfo(b)
229 dev = br.name
230 %>
231 <h3><%:a_s_if_bridge Bridge%> <%=br.name%></h3>
232 <p style="font-size:90%;padding-left:1em">
233
234 <strong><%:a_s_if_device Device%>:</strong>
235 <%=dev%> (<%:a_s_if_mac MAC%> <%=get_mac(dev)%>)<br />
236
237 <strong><%:a_s_if_type Type%>:</strong>
238 <%:a_s_if_ethbridge Ethernet Bridge%><br />
239
240 &nbsp; &#x2514; <strong><%:a_s_if_bridge_id ID%>:</strong> <%=br.id%><br />
241 &nbsp; &#x2514; <strong><%:a_s_if_bridge_stp STP%>:</strong> <%=br.stp and "enabled" or "disabled"%><br />
242
243 <strong><%:a_s_if_transfer Transfer%></strong><br />
244 &nbsp; &#x2514; <strong><%:a_s_if_transfer_rx RX%>:</strong> <%=devinfo[dev][2]%> Pkts. (<%=wba.byte_format(tonumber(devinfo[dev][1]))%>)<br />
245 &nbsp; &#x2514; <strong><%:a_s_if_transfer_tx TX%>:</strong> <%=devinfo[dev][10]%> Pkts. (<%=wba.byte_format(tonumber(devinfo[dev][9]))%>)<br />
246
247 <%- if ( b.ipaddr and #b.ipaddr > 0 ) or ( b.ip6addr and #b.ip6addr > 0 ) then -%>
248 <strong><%:a_s_if_ipconfig IP Configuration%></strong><br />
249 &nbsp; &#x2514; <strong><%:a_s_if_ipconfig_primary Primary%>:</strong>
250 <% if b.ipaddr and #b.ipaddr > 0 then %>
251 <%=b.ipaddr%>/<%=b.netmask%>
252 <% if b.proto == "dhcp" then -%>
253 (<%:a_s_if_ipconfig_dhcp DHCP assigned%>)
254 <%- end %>
255 <% else %>
256 <em><%:a_s_if_ipconfig_none Not configured%></em>
257 <% end %><br />
258
259 <% for i, a in ipairs(get_aliases(b)) do %>
260 &nbsp; &#x2514; <strong><%:a_s_if_ipconfig_alias Alias%> #<%=i%>:</strong>
261 <%=a.ipaddr%>/<%=a.netmask%> (<%:a_s_if_device Device%> <%=dev%>:<%=i%>) <br />
262 <% end %>
263
264 <% if b.ip6addr and #b.ip6addr > 0 then %>
265 &nbsp; &#x2514; <strong><%:a_s_if_ipconfig_ipv6 IPv6%>:</strong> <%=b.ip6addr%><br />
266 <% end %>
267 <%- end -%>
268
269 <% for n, i in ipairs(br.ifnames) do
270 dev = i
271 vlan = get_vlan(dev)
272 %>
273 <strong><%:a_s_if_bridge_port Bridge Port%> <%=n%></strong><br />
274
275 &nbsp; &#x2514; <strong><%:a_s_if_device Device%>:</strong>
276 <%=dev%> (<%:a_s_if_mac MAC%> <%=get_mac(dev)%>)<br />
277
278 &nbsp; &#x2514; <strong><%:a_s_if_type Type%>:</strong>
279 <% if is_wifi(dev) then iw = get_iwinfo(dev) -%>
280 <%:a_s_if_wifidev Wireless Adapter%> (<%=iw.type%>)<br />
281 <% if iw then %>
282 &nbsp; &nbsp; &nbsp; &#x2514; <strong><%:a_s_if_iwmode Mode%>:</strong> <%=get_iwmode(iw)%><br />
283 &nbsp; &nbsp; &nbsp; &#x2514; <strong><%:a_s_if_iwssid SSID%>:</strong> <%=iw.ssid%><br />
284 &nbsp; &nbsp; &nbsp; &#x2514; <strong><%:a_s_if_iwchannel Channel%>:</strong> <%=iw.channel%>
285 <% end %>
286 <% else -%>
287 <% if vlan then %>
288 <%:a_s_if_ethswitch Ethernet Switch%> (<%=get_switch_driver(dev)%>)<br />
289 &nbsp; &nbsp; &nbsp; &#x2514; <strong><%:a_s_if_vlan VLAN%>:</strong>
290 <%=get_vlan(dev)%> (<%:a_s_if_vlan_ports Ports%> <%=table.concat(get_vlan_ports(dev), ", ")%>)
291 <% else %>
292 <%:a_s_if_ethdev Ethernet Adapter%>
293 <% end %>
294 <% end -%><br />
295 <% end %>
296 <br /></p>
297 <% end %>
298 </fieldset>
299 </div>
300 </form>
301
302 <%+footer%>