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