2 LuCI - Lua Configuration Interface
3 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
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
9 http://www.apache.org/licenses/LICENSE-2.0
15 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
16 <script type="text/javascript">//<![CDATA[
17 var bwxhr = new XHR();
29 var data_wanted = Math.floor(width / step);
66 var svg = document.getElementById('bwsvg');
69 G = svg.getSVGDocument
70 ? svg.getSVGDocument() : svg.contentDocument;
73 G = document.embeds['bwsvg'].getSVGDocument();
78 window.setTimeout(arguments.callee, 1000);
83 width = svg.offsetWidth - 2;
84 height = svg.offsetHeight - 2;
85 data_wanted = Math.ceil(width / step);
87 /* prefill datasets */
88 for (var i = 0; i < data_wanted; i++)
95 /* find svg elements */
96 line_udp = G.getElementById('udp');
97 line_tcp = G.getElementById('tcp');
98 line_otr = G.getElementById('other');
100 label_25 = G.getElementById('label_25');
101 label_50 = G.getElementById('label_50');
102 label_75 = G.getElementById('label_75');
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');
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');
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');
116 label_scale = document.getElementById('scale');
118 conn_table = document.getElementById('connections');
121 /* plot horizontal time interval lines */
122 for (var i = width % (step * 60); i < width; i += step * 60)
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');
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'));
137 label_25.parentNode.appendChild(line);
138 label_25.parentNode.appendChild(text);
141 label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
143 /* render datasets, start update interval */
144 XHR.poll(3, '<%=build_url("admin/status/realtime/connections_status")%>', null,
147 var conn = json.connections;
149 while (conn_table.rows.length > 1)
150 conn_table.rows[0].parentNode.deleteRow(-1);
153 var lookup_queue = [ ];
155 conn.sort(function(a, b) {
156 return b.bytes - a.bytes;
159 for (var i = 0; i < conn.length; i++)
163 if (c.src == '127.0.0.1' && c.dst == '127.0.0.1')
166 var tr = conn_table.rows[0].parentNode.insertRow(-1);
167 tr.className = 'cbi-section-table-row cbi-rowstyle-' + (1 + (i % 2));
169 if (!dns_cache[c.src])
170 lookup_queue.push(c.src);
172 if (!dns_cache[c.dst])
173 lookup_queue.push(c.dst);
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);
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);
183 var traf = tr.insertCell(-1);
184 traf.style.whiteSpace = 'nowrap';
185 traf.innerHTML = String.format('%1024.2mB (%d <%:Pkts.%>)', c.bytes, c.packets);
188 if (lookup_queue.length > 0)
189 XHR.get('<%=build_url("admin/status/nameinfo")%>/' + lookup_queue.slice(0, 100).join('/'), null,
192 for (var addr in json)
193 dns_cache[addr] = json[addr];
198 var data = json.statistics;
203 var data_udp_avg = 0;
204 var data_tcp_avg = 0;
205 var data_otr_avg = 0;
207 var data_udp_peak = 0;
208 var data_tcp_peak = 0;
209 var data_otr_peak = 0;
211 for (var i = data_stamp ? 0 : 1; i < data.length; i++)
213 /* skip overlapping entries */
214 if (data[i][TIME] <= data_stamp)
217 data_udp.push(data[i][UDP]);
218 data_tcp.push(data[i][TCP]);
219 data_otr.push(data[i][OTHER]);
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);
228 for (var i = 0; i < data_udp.length; i++)
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]);
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]);
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;
246 data_udp_avg = data_udp[i];
247 data_tcp_avg = data_tcp[i];
248 data_otr_avg = data_otr[i];
252 /* remember current timestamp, calculate horizontal scale */
253 data_stamp = data[data.length-1][TIME];
254 data_scale = height / (data_max * 1.1);
258 var pt_udp = '0,' + height;
259 var pt_tcp = '0,' + height;
260 var pt_otr = '0,' + height;
266 for (var i = 0; i < data_udp.length; i++)
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);
274 pt_udp += ' ' + x + ',' + y_udp;
275 pt_tcp += ' ' + x + ',' + y_tcp;
276 pt_otr += ' ' + x + ',' + y_otr;
279 pt_udp += ' ' + width + ',' + y_udp + ' ' + width + ',' + height;
280 pt_tcp += ' ' + width + ',' + y_tcp + ' ' + width + ',' + height;
281 pt_otr += ' ' + width + ',' + y_otr + ' ' + width + ',' + height;
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] ]
290 order.sort(function(a, b) { return b[1] - a[1] });
292 for (var i = 0; i < order.length; i++)
293 order[i][0].parentNode.appendChild(order[i][0]);
296 line_udp.setAttribute('points', pt_udp);
297 line_tcp.setAttribute('points', pt_tcp);
298 line_otr.setAttribute('points', pt_otr);
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);
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]);
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);
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);
322 <h2><a id="content" name="content"><%:Realtime Connections%></a></h2>
324 <div class="cbi-map-descr"><%:This page gives an overview over currently active network connections.%></div>
326 <fieldset class="cbi-section" id="cbi-table-table">
327 <legend><%:Active Connections%></legend>
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>
333 <table style="width:100%; table-layout:fixed" cellspacing="5">
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>
338 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
339 <td id="lb_udp_avg">0</td>
341 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
342 <td id="lb_udp_peak">0</td>
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>
348 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
349 <td id="lb_tcp_avg">0</td>
351 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
352 <td id="lb_tcp_peak">0</td>
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>
358 <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
359 <td id="lb_otr_avg">0</td>
361 <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
362 <td id="lb_otr_peak">0</td>
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>
377 <tr><td colspan="5"><em><%:Collecting data...%></em></td></tr>