connections.htm 11 KB

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