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