9a77f89106937b0819c07e6c3bd1d37cb16a320a
[project/luci.git] / modules / luci-mod-admin-full / luasrc / view / admin_network / iface_overview.htm
1 <%#
2  Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
3  Licensed to the public under the Apache License 2.0.
4 -%>
5
6 <%-
7         local ntm = require "luci.model.network".init()
8         local fwm = require "luci.model.firewall".init()
9
10         local net
11         local ifaces = { }
12         local netlist = { }
13         for _, net in ipairs(ntm:get_networks()) do
14                 if net:name() ~= "loopback" then
15                         local z = fwm:get_zone_by_network(net:name())
16                         ifaces[#ifaces+1] = net:name()
17                         netlist[#netlist+1] = {
18                                 net:name(), z and z:name() or "-", z
19                         }
20                 end
21         end
22
23         table.sort(netlist,
24                 function(a, b)
25                         if a[2] ~= b[2] then
26                                 return a[2] < b[2]
27                         else
28                                 return a[1] < b[1]
29                         end
30                 end)
31 -%>
32
33 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
34 <script type="text/javascript">//<![CDATA[
35         function iface_shutdown(id, reconnect) {
36                 if (!reconnect && !confirm(String.format('<%_Really shutdown interface "%s" ?\nYou might lose access to this device if you are connected via this interface.%>', id)))
37                         return;
38
39                 var d = document.getElementById(id + '-ifc-description');
40                 if (d)
41                         d.innerHTML = reconnect
42                                 ? '<em><%:Interface is reconnecting...%></em>'
43                                 : '<em><%:Interface is shutting down...%></em>';
44
45                 var s = document.getElementById('ifc-rc-status');
46                 if (s)
47                 {
48                         s.parentNode.style.display = 'block';
49                         s.innerHTML = '<%:Waiting for changes to be applied...%>';
50                 }
51
52                 (new XHR()).post('<%=url('admin/network')%>/iface_' + (reconnect ? 'reconnect' : 'shutdown') + '/' + id, { token: '<%=token%>' },
53                         function(x)
54                         {
55                                 if (s)
56                                 {
57                                         s.innerHTML = reconnect
58                                                 ? '<%:Interface reconnected%>'
59                                                 : '<%:Interface shut down%>';
60
61                                         window.setTimeout(function() {
62                                                 s.parentNode.style.display = 'none';
63                                         }, 1000);
64                                 }
65                         }
66                 );
67         }
68
69         function iface_delete(id) {
70                 if (!confirm('<%:Really delete this interface? The deletion cannot be undone!\nYou might lose access to this device if you are connected via this interface.%>'))
71                         return;
72
73                 (new XHR()).post('<%=url('admin/network/iface_delete')%>/' + id, { token: '<%=token%>' },
74                         function(x) {
75                                 location.href = '<%=url('admin/network/network')%>';
76                         }
77                 );
78         }
79
80         var iwxhr = new XHR();
81         var wifidevs = <%=luci.http.write_json(netdevs)%>;
82         var arptable = <%=luci.http.write_json(arpcache)%>;
83
84         XHR.poll(5, '<%=url('admin/network/iface_status', table.concat(ifaces, ","))%>', null,
85                 function(x, ifcs)
86                 {
87                         if (ifcs)
88                         {
89                                 for (var idx = 0; idx < ifcs.length; idx++)
90                                 {
91                                         var ifc = ifcs[idx];
92                                         var html = '';
93
94                                         var s = document.getElementById(ifc.id + '-ifc-devices');
95                                         if (s)
96                                         {
97                                                 var stat = String.format(
98                                                         '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" />',
99                                                                 ifc.type,
100                                                                 ifc.is_up ? '' : '_disabled'
101                                                 );
102
103                                                 if (ifc.subdevices && ifc.subdevices.length)
104                                                 {
105                                                         stat += ' <strong>(';
106
107                                                         for (var j = 0; j < ifc.subdevices.length; j++)
108                                                         {
109                                                                 var sif = ifc.subdevices[j];
110
111                                                                 stat += String.format(
112                                                                         '<img src="<%=resource%>/icons/%s%s.png" style="width:16px; height:16px; vertical-align:middle" title="%h" />',
113                                                                                 sif.type,
114                                                                                 sif.is_up ? '' : '_disabled',
115                                                                                 sif.name
116                                                                 );
117                                                         }
118
119                                                         stat += ')</strong>';
120                                                 }
121
122                                                 stat += String.format(
123                                                         '<br /><small>%h</small>',
124                                                                 ifc.name
125                                                 );
126
127                                                 s.innerHTML = stat;
128                                         }
129
130                                         var d = document.getElementById(ifc.id + '-ifc-description');
131                                         if (d && ifc.proto && ifc.ifname)
132                                         {
133                                                 if (ifc.is_up)
134                                                 {
135                                                         html += String.format('<strong><%:Uptime%>:</strong> %t<br />', ifc.uptime);
136                                                 }
137
138                                                 if (ifc.type != 'tunnel')
139                                                 {
140                                                         html += String.format('<strong><%:MAC-Address%>:</strong> %s<br />', ifc.macaddr);
141                                                 }
142
143                                                 html += String.format(
144                                                         '<strong><%:RX%></strong>: %.2mB (%d <%:Pkts.%>)<br />' +
145                                                         '<strong><%:TX%></strong>: %.2mB (%d <%:Pkts.%>)<br />',
146                                                                 ifc.rx_bytes, ifc.rx_packets,
147                                                                 ifc.tx_bytes, ifc.tx_packets
148                                                 );
149
150                                                 if (ifc.ipaddrs && ifc.ipaddrs.length)
151                                                 {
152                                                         html += '<strong><%:IPv4%>: </strong>';
153
154                                                         for (var i = 0; i < ifc.ipaddrs.length; i++)
155                                                                 html += String.format(
156                                                                         '%s%s/%d',
157                                                                         i ? ', ' : '',
158                                                                         ifc.ipaddrs[i].addr,
159                                                                         ifc.ipaddrs[i].prefix
160                                                                 );
161
162                                                         html += '<br />';
163                                                 }
164
165                                                 if (ifc.ip6addrs && ifc.ip6addrs.length)
166                                                 {
167                                                         html += '<strong><%:IPv6%>: </strong>';
168
169                                                         for (var i = 0; i < ifc.ip6addrs.length; i++)
170                                                                 html += String.format(
171                                                                         '%s%s/%d',
172                                                                         i ? ', ' : '',
173                                                                         ifc.ip6addrs[i].addr.toUpperCase(),
174                                                                         ifc.ip6addrs[i].prefix
175                                                                 );
176
177                                                         html += '<br />';
178                                                 }
179
180                                                 d.innerHTML = html;
181                                         }
182                                         else if (d && !ifc.proto)
183                                         {
184                                                 var e = document.getElementById(ifc.id + '-ifc-edit');
185                                                 if (e)
186                                                         e.disabled = true;
187
188                                                 d.innerHTML = String.format(
189                                                         '<em><%:Unsupported protocol type.%></em><br />' +
190                                                         '<a href="%h"><%:Install protocol extensions...%></a>',
191                                                                 '<%=url("admin/system/packages")%>?query=luci-proto&display=available'
192                                                 );
193                                         }
194                                         else if (d && !ifc.ifname)
195                                         {
196                                                 d.innerHTML = String.format(
197                                                         '<em><%:Network without interfaces.%></em><br />' +
198                                                         '<a href="<%=url("admin/network/network/%s")%>?tab.network.%s=physical"><%:Assign interfaces...%></a>',
199                                                                 ifc.name, ifc.name
200                                                 );
201                                         }
202                                         else if (d)
203                                         {
204                                                 d.innerHTML = '<em><%:Interface not present or not connected yet.%></em>';
205                                         }
206                                 }
207                         }
208                 }
209         );
210 //]]></script>
211
212 <fieldset class="cbi-section" style="display:none">
213         <legend><%:Reconnecting interface%></legend>
214         <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" />
215         <span id="ifc-rc-status"><%:Waiting for changes to be applied...%></span>
216 </fieldset>
217
218 <div class="cbi-map">
219         <fieldset class="cbi-section">
220                 <legend><%:Interface Overview%></legend>
221
222                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
223                         <tr class="cbi-section-table-titles">
224                                 <th class="cbi-section-table-cell"><%:Network%></th>
225                                 <th class="cbi-section-table-cell" style="text-align:left"><%:Status%></th>
226                                 <th class="cbi-section-table-cell"><%:Actions%></th>
227                         </tr>
228                         <%
229                                 for i, net in ipairs(netlist) do
230                                         local z = net[3]
231                                         local c = z and z:get_color() or "#EEEEEE"
232                                         local t = z and translate("Part of zone %q" % z:name()) or translate("No zone assigned")
233                         %>
234                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=i % 2 + 1%>">
235                                         <td class="cbi-value-field" style="padding:3px">
236                                                 <div class="ifacebox">
237                                                         <div class="ifacebox-head" style="background-color:<%=c%>" title="<%=pcdata(t)%>">
238                                                                 <strong><%=net[1]:upper()%></strong>
239                                                         </div>
240                                                         <div class="ifacebox-body" id="<%=net[1]%>-ifc-devices">
241                                                                 <img src="<%=resource%>/icons/ethernet_disabled.png" style="width:16px; height:16px" /><br />
242                                                                 <small>?</small>
243                                                         </div>
244                                                 </div>
245                                         </td>
246                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px" id="<%=net[1]%>-ifc-description">
247                                                 <em><%:Collecting data...%></em>
248                                         </td>
249                                         <td style="width:420px">
250                                                 <input type="button" class="cbi-button cbi-button-reload" style="width:100px" onclick="iface_shutdown('<%=net[1]%>', true)" title="<%:Reconnect this interface%>" value="<%:Connect%>" />
251                                                 <input type="button" class="cbi-button cbi-button-reset" style="width:100px" onclick="iface_shutdown('<%=net[1]%>', false)" title="<%:Shutdown this interface%>" value="<%:Stop%>" />
252                                                 <input type="button" class="cbi-button cbi-button-edit" style="width:100px" onclick="location.href='<%=url("admin/network/network", net[1])%>'" title="<%:Edit this interface%>" value="<%:Edit%>" id="<%=net[1]%>-ifc-edit" />
253                                                 <input type="submit" class="cbi-button cbi-button-remove" style="width:100px" onclick="iface_delete('<%=net[1]%>')" value="<%:Delete%>" />
254                                         </td>
255                                 </tr>
256                         <% end %>
257                 </table>
258
259                 <input type="button" class="cbi-button cbi-button-add" value="<%:Add new interface...%>" onclick="location.href='<%=url("admin/network/iface_add")%>'" />
260         </fieldset>
261 </div>