9c9eb01c966408682693eee45b5ed1caa9a4f992
[project/luci.git] / modules / admin-full / luasrc / view / admin_status / connections.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 <%+header%>
16
17 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
18 <script type="text/javascript">//<![CDATA[
19         var bwxhr = new XHR();
20
21         var G;
22         var TIME  = 0;
23         var UDP   = 1;
24         var TCP   = 2;
25         var OTHER = 3;
26
27         var width  = 760;
28         var height = 300;
29         var step   = 5;
30
31         var data_wanted = Math.floor(width / step);
32         var data_fill   = 0;
33         var data_stamp  = 0;
34
35         var data_udp = [ ];
36         var data_tcp = [ ];
37         var data_otr = [ ];
38
39         var line_udp;
40         var line_tcp;
41
42         var label_25;
43         var label_50;
44         var label_75;
45
46         var label_udp_cur;
47         var label_udp_avg;
48         var label_udp_peak;
49
50         var label_tcp_cur;
51         var label_tcp_avg;
52         var label_tcp_peak;
53
54         var label_otr_cur;
55         var label_otr_avg;
56         var label_otr_peak;
57
58         var label_scale;
59
60         var conn_table;
61
62         var dns_cache = { };
63
64
65         /* wait for SVG */
66         window.setTimeout(
67                 function() {
68                         var svg = document.getElementById('bwsvg');
69
70                         try {
71                                 G = svg.getSVGDocument
72                                         ? svg.getSVGDocument() : svg.contentDocument;
73                         }
74                         catch(e) {
75                                 G = document.embeds['bwsvg'].getSVGDocument();
76                         }
77
78                         if (!G)
79                         {
80                                 window.setTimeout(arguments.callee, 1000);
81                         }
82                         else
83                         {
84                                 /* find sizes */
85                                 width       = svg.offsetWidth  - 2;
86                                 height      = svg.offsetHeight - 2;
87                                 data_wanted = Math.ceil(width / step);
88
89                                 /* prefill datasets */
90                                 for (var i = 0; i < data_wanted; i++)
91                                 {
92                                         data_udp[i] = 0;
93                                         data_tcp[i] = 0;
94                                         data_otr[i] = 0;
95                                 }
96
97                                 /* find svg elements */
98                                 line_udp = G.getElementById('udp');
99                                 line_tcp = G.getElementById('tcp');
100                                 line_otr = G.getElementById('other');
101
102                                 label_25 = G.getElementById('label_25');
103                                 label_50 = G.getElementById('label_50');
104                                 label_75 = G.getElementById('label_75');
105
106                                 label_udp_cur  = document.getElementById('lb_udp_cur');
107                                 label_udp_avg  = document.getElementById('lb_udp_avg');
108                                 label_udp_peak = document.getElementById('lb_udp_peak');
109
110                                 label_tcp_cur  = document.getElementById('lb_tcp_cur');
111                                 label_tcp_avg  = document.getElementById('lb_tcp_avg');
112                                 label_tcp_peak = document.getElementById('lb_tcp_peak');
113
114                                 label_otr_cur  = document.getElementById('lb_otr_cur');
115                                 label_otr_avg  = document.getElementById('lb_otr_avg');
116                                 label_otr_peak = document.getElementById('lb_otr_peak');
117
118                                 label_scale    = document.getElementById('scale');
119
120                                 conn_table     = document.getElementById('connections');
121
122
123                                 /* plot horizontal time interval lines */
124                                 for (var i = width % (step * 60); i < width; i += step * 60)
125                                 {
126                                         var line = G.createElementNS('http://www.w3.org/2000/svg', 'line');
127                                                 line.setAttribute('x1', i);
128                                                 line.setAttribute('y1', 0);
129                                                 line.setAttribute('x2', i);
130                                                 line.setAttribute('y2', '100%');
131                                                 line.setAttribute('style', 'stroke:black;stroke-width:0.1');
132
133                                         var text = G.createElementNS('http://www.w3.org/2000/svg', 'text');
134                                                 text.setAttribute('x', i + 5);
135                                                 text.setAttribute('y', 15);
136                                                 text.setAttribute('style', 'fill:#999999; font-size:9pt');
137                                                 text.appendChild(G.createTextNode(Math.round((width - i) / step / 60) + 'm'));
138
139                                         label_25.parentNode.appendChild(line);
140                                         label_25.parentNode.appendChild(text);
141                                 }
142
143                                 label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
144
145                                 /* render datasets, start update interval */
146                                 XHR.poll(3, '<%=build_url("admin/status/realtime/connections_status")%>', null,
147                                         function(x, json)
148                                         {
149                                                 var conn = json.connections;
150
151                                                 while (conn_table.rows.length > 1)
152                                                         conn_table.rows[0].parentNode.deleteRow(-1);
153
154
155                                                 var lookup_queue = [ ];
156
157                                                 conn.sort(function(a, b) {
158                                                         return b.bytes - a.bytes;
159                                                 });
160
161                                                 for (var i = 0; i < conn.length; i++)
162                                                 {
163                                                         var c  = conn[i];
164                                                         var tr = conn_table.rows[0].parentNode.insertRow(-1);
165                                                             tr.className = 'cbi-section-table-row cbi-rowstyle-' + (1 + (i % 2));
166
167                                                         if (!dns_cache[c.src])
168                                                                 lookup_queue.push(c.src);
169
170                                                         if (!dns_cache[c.dst])
171                                                                 lookup_queue.push(c.dst);
172
173                                                         var src = dns_cache[c.src] || (c.layer3 == 'ipv6' ? '[' + c.src + ']' : c.src);
174                                                         var dst = dns_cache[c.dst] || (c.layer3 == 'ipv6' ? '[' + c.dst + ']' : c.dst);
175
176                                                         tr.insertCell(-1).innerHTML = c.layer3.toUpperCase();
177                                                         tr.insertCell(-1).innerHTML = c.layer4.toUpperCase();
178                                                         tr.insertCell(-1).innerHTML = String.format('%s:%d', src, c.sport);
179                                                         tr.insertCell(-1).innerHTML = String.format('%s:%d', dst, c.dport);
180
181                                                         var traf = tr.insertCell(-1);
182                                                             traf.style.whiteSpace = 'nowrap';
183                                                             traf.innerHTML = String.format('%1024.2mB (%d <%:Pkts.%>)', c.bytes, c.packets);
184                                                 }
185
186                                                 if (lookup_queue.length > 0)
187                                                         XHR.get('<%=build_url("admin/status/nameinfo")%>/' + lookup_queue.slice(0, 100).join('/'), null,
188                                                                 function(x, json)
189                                                                 {
190                                                                         for (var addr in json)
191                                                                                 dns_cache[addr] = json[addr];
192                                                                 }
193                                                         );
194
195
196                                                 var data = json.statistics;
197
198                                                 var data_max   = 0;
199                                                 var data_scale = 0;
200
201                                                 var data_udp_avg = 0;
202                                                 var data_tcp_avg = 0;
203                                                 var data_otr_avg = 0;
204
205                                                 var data_udp_peak = 0;
206                                                 var data_tcp_peak = 0;
207                                                 var data_otr_peak = 0;
208
209                                                 for (var i = data_stamp ? 0 : 1; i < data.length; i++)
210                                                 {
211                                                         /* skip overlapping entries */
212                                                         if (data[i][TIME] <= data_stamp)
213                                                                 continue;
214
215                                                         data_udp.push(data[i][UDP]);
216                                                         data_tcp.push(data[i][TCP]);
217                                                         data_otr.push(data[i][OTHER]);
218                                                 }
219
220                                                 /* cut off outdated entries */
221                                                 data_udp = data_udp.slice(data_udp.length - data_wanted, data_udp.length);
222                                                 data_tcp = data_tcp.slice(data_tcp.length - data_wanted, data_tcp.length);
223                                                 data_otr = data_otr.slice(data_otr.length - data_wanted, data_otr.length);
224
225                                                 /* find peak */
226                                                 for (var i = 0; i < data_udp.length; i++)
227                                                 {
228                                                         data_max = Math.max(data_max, data_udp[i]);
229                                                         data_max = Math.max(data_max, data_tcp[i]);
230                                                         data_max = Math.max(data_max, data_otr[i]);
231
232                                                         data_udp_peak = Math.max(data_udp_peak, data_udp[i]);
233                                                         data_tcp_peak = Math.max(data_tcp_peak, data_tcp[i]);
234                                                         data_otr_peak = Math.max(data_otr_peak, data_otr[i]);
235
236                                                         if (i > 0)
237                                                         {
238                                                                 data_udp_avg = (data_udp_avg + data_udp[i]) / 2;
239                                                                 data_tcp_avg = (data_tcp_avg + data_tcp[i]) / 2;
240                                                                 data_otr_avg = (data_otr_avg + data_otr[i]) / 2;
241                                                         }
242                                                         else
243                                                         {
244                                                                 data_udp_avg = data_udp[i];
245                                                                 data_tcp_avg = data_tcp[i];
246                                                                 data_otr_avg = data_otr[i];
247                                                         }
248                                                 }
249
250                                                 /* remember current timestamp, calculate horizontal scale */
251                                                 data_stamp = data[data.length-1][TIME];
252                                                 data_scale = height / (data_max * 1.1);
253
254
255                                                 /* plot data */
256                                                 var pt_udp = '0,' + height;
257                                                 var pt_tcp = '0,' + height;
258                                                 var pt_otr = '0,' + height;
259
260                                                 var y_udp = 0;
261                                                 var y_tcp = 0;
262                                                 var y_otr = 0;
263
264                                                 for (var i = 0; i < data_udp.length; i++)
265                                                 {
266                                                         var x = i * step;
267
268                                                         y_udp = height - Math.floor(data_udp[i] * data_scale);
269                                                         y_tcp = height - Math.floor(data_tcp[i] * data_scale);
270                                                         y_otr = height - Math.floor(data_otr[i] * data_scale);
271
272                                                         pt_udp += ' ' + x + ',' + y_udp;
273                                                         pt_tcp += ' ' + x + ',' + y_tcp;
274                                                         pt_otr += ' ' + x + ',' + y_otr;
275                                                 }
276
277                                                 pt_udp += ' ' + width + ',' + y_udp + ' ' + width + ',' + height;
278                                                 pt_tcp += ' ' + width + ',' + y_tcp + ' ' + width + ',' + height;
279                                                 pt_otr += ' ' + width + ',' + y_otr + ' ' + width + ',' + height;
280
281
282                                                 line_udp.setAttribute('points', pt_udp);
283                                                 line_tcp.setAttribute('points', pt_tcp);
284                                                 line_otr.setAttribute('points', pt_otr);
285
286                                                 label_25.firstChild.data = Math.floor(1.1 * 0.25 * data_max);
287                                                 label_50.firstChild.data = Math.floor(1.1 * 0.50 * data_max);
288                                                 label_75.firstChild.data = Math.floor(1.1 * 0.75 * data_max);
289
290                                                 label_udp_cur.innerHTML = Math.floor(data_udp[data_udp.length-1]);
291                                                 label_tcp_cur.innerHTML = Math.floor(data_tcp[data_tcp.length-1]);
292                                                 label_otr_cur.innerHTML = Math.floor(data_otr[data_otr.length-1]);
293
294                                                 label_udp_avg.innerHTML = Math.floor(data_udp_avg);
295                                                 label_tcp_avg.innerHTML = Math.floor(data_tcp_avg);
296                                                 label_otr_avg.innerHTML = Math.floor(data_otr_avg);
297
298                                                 label_udp_peak.innerHTML = Math.floor(data_udp_peak);
299                                                 label_tcp_peak.innerHTML = Math.floor(data_tcp_peak);
300                                                 label_otr_peak.innerHTML = Math.floor(data_otr_peak);
301                                         }
302                                 );
303                         }
304                 }, 1000
305         );
306 //]]></script>
307
308 <h2><a id="content" name="content"><%:Realtime Connections%></a></h2>
309
310 <div class="cbi-map-descr"><%:This page gives an overview over currently active network connections.%></div>
311
312 <fieldset class="cbi-section" id="cbi-table-table">
313         <legend><%:Active Connections%></legend>
314
315         <embed id="bwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/connections.svg" />
316         <div style="text-align:right"><small id="scale">-</small></div>
317         <br />
318
319         <table style="width:100%; table-layout:fixed" cellspacing="5">
320                 <tr>
321                         <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid blue"><%:UDP:%></strong></td>
322                         <td id="lb_udp_cur">0</td>
323
324                         <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
325                         <td id="lb_udp_avg">0</td>
326
327                         <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
328                         <td id="lb_udp_peak">0</td>
329                 </tr>
330                 <tr>
331                         <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:TCP:%></strong></td>
332                         <td id="lb_tcp_cur">0</td>
333
334                         <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
335                         <td id="lb_tcp_avg">0</td>
336
337                         <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
338                         <td id="lb_tcp_peak">0</td>
339                 </tr>
340                 <tr>
341                         <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid red"><%:Other:%></strong></td>
342                         <td id="lb_otr_cur">0</td>
343
344                         <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
345                         <td id="lb_otr_avg">0</td>
346
347                         <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
348                         <td id="lb_otr_peak">0</td>
349                 </tr>
350         </table>
351         <br />
352
353         <div class="cbi-section-node">
354                 <table class="cbi-section-table" id="connections">
355                         <tr class="cbi-section-table-titles">
356                                 <th class="cbi-section-table-cell"><%:Network%></th>
357                                 <th class="cbi-section-table-cell"><%:Protocol%></th>
358                                 <th class="cbi-section-table-cell"><%:Source%></th>
359                                 <th class="cbi-section-table-cell"><%:Destination%></th>
360                                 <th class="cbi-section-table-cell"><%:Transfer%></th>
361                         </tr>
362
363                         <tr><td colspan="5"><em><%:Collecting data...%></em></td></tr>
364                 </table>
365         </div>
366 </fieldset>
367
368 <%+footer%>