connections.htm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. continue;
  120. var tr = conn_table.rows[0].parentNode.insertRow(-1);
  121. tr.className = 'cbi-section-table-row cbi-rowstyle-' + (1 + (i % 2));
  122. if (!dns_cache[c.src])
  123. lookup_queue.push(c.src);
  124. if (!dns_cache[c.dst])
  125. lookup_queue.push(c.dst);
  126. var src = dns_cache[c.src] || (c.layer3 == 'ipv6' ? '[' + c.src + ']' : c.src);
  127. var dst = dns_cache[c.dst] || (c.layer3 == 'ipv6' ? '[' + c.dst + ']' : c.dst);
  128. tr.insertCell(-1).innerHTML = c.layer3.toUpperCase();
  129. tr.insertCell(-1).innerHTML = c.layer4.toUpperCase();
  130. tr.insertCell(-1).innerHTML = String.format('%s:%d', src, c.sport);
  131. tr.insertCell(-1).innerHTML = String.format('%s:%d', dst, c.dport);
  132. var traf = tr.insertCell(-1);
  133. traf.style.whiteSpace = 'nowrap';
  134. traf.innerHTML = String.format('%1024.2mB (%d <%:Pkts.%>)', c.bytes, c.packets);
  135. }
  136. if (lookup_queue.length > 0)
  137. XHR.get('<%=build_url("admin/status/nameinfo")%>/' + lookup_queue.slice(0, 100).join('/'), null,
  138. function(x, json)
  139. {
  140. for (var addr in json)
  141. dns_cache[addr] = json[addr];
  142. }
  143. );
  144. var data = json.statistics;
  145. var data_max = 0;
  146. var data_scale = 0;
  147. var data_udp_avg = 0;
  148. var data_tcp_avg = 0;
  149. var data_otr_avg = 0;
  150. var data_udp_peak = 0;
  151. var data_tcp_peak = 0;
  152. var data_otr_peak = 0;
  153. for (var i = data_stamp ? 0 : 1; i < data.length; i++)
  154. {
  155. /* skip overlapping entries */
  156. if (data[i][TIME] <= data_stamp)
  157. continue;
  158. data_udp.push(data[i][UDP]);
  159. data_tcp.push(data[i][TCP]);
  160. data_otr.push(data[i][OTHER]);
  161. }
  162. /* cut off outdated entries */
  163. data_udp = data_udp.slice(data_udp.length - data_wanted, data_udp.length);
  164. data_tcp = data_tcp.slice(data_tcp.length - data_wanted, data_tcp.length);
  165. data_otr = data_otr.slice(data_otr.length - data_wanted, data_otr.length);
  166. /* find peak */
  167. for (var i = 0; i < data_udp.length; i++)
  168. {
  169. data_max = Math.max(data_max, data_udp[i]);
  170. data_max = Math.max(data_max, data_tcp[i]);
  171. data_max = Math.max(data_max, data_otr[i]);
  172. data_udp_peak = Math.max(data_udp_peak, data_udp[i]);
  173. data_tcp_peak = Math.max(data_tcp_peak, data_tcp[i]);
  174. data_otr_peak = Math.max(data_otr_peak, data_otr[i]);
  175. if (i > 0)
  176. {
  177. data_udp_avg = (data_udp_avg + data_udp[i]) / 2;
  178. data_tcp_avg = (data_tcp_avg + data_tcp[i]) / 2;
  179. data_otr_avg = (data_otr_avg + data_otr[i]) / 2;
  180. }
  181. else
  182. {
  183. data_udp_avg = data_udp[i];
  184. data_tcp_avg = data_tcp[i];
  185. data_otr_avg = data_otr[i];
  186. }
  187. }
  188. /* remember current timestamp, calculate horizontal scale */
  189. data_stamp = data[data.length-1][TIME];
  190. data_scale = height / (data_max * 1.1);
  191. /* plot data */
  192. var pt_udp = '0,' + height;
  193. var pt_tcp = '0,' + height;
  194. var pt_otr = '0,' + height;
  195. var y_udp = 0;
  196. var y_tcp = 0;
  197. var y_otr = 0;
  198. for (var i = 0; i < data_udp.length; i++)
  199. {
  200. var x = i * step;
  201. y_udp = height - Math.floor(data_udp[i] * data_scale);
  202. y_tcp = height - Math.floor(data_tcp[i] * data_scale);
  203. y_otr = height - Math.floor(data_otr[i] * data_scale);
  204. pt_udp += ' ' + x + ',' + y_udp;
  205. pt_tcp += ' ' + x + ',' + y_tcp;
  206. pt_otr += ' ' + x + ',' + y_otr;
  207. }
  208. pt_udp += ' ' + width + ',' + y_udp + ' ' + width + ',' + height;
  209. pt_tcp += ' ' + width + ',' + y_tcp + ' ' + width + ',' + height;
  210. pt_otr += ' ' + width + ',' + y_otr + ' ' + width + ',' + height;
  211. var order = [
  212. [ line_udp, data_udp[data_udp.length-1] ],
  213. [ line_tcp, data_tcp[data_tcp.length-1] ],
  214. [ line_otr, data_otr[data_otr.length-1] ]
  215. ];
  216. order.sort(function(a, b) { return b[1] - a[1] });
  217. for (var i = 0; i < order.length; i++)
  218. order[i][0].parentNode.appendChild(order[i][0]);
  219. line_udp.setAttribute('points', pt_udp);
  220. line_tcp.setAttribute('points', pt_tcp);
  221. line_otr.setAttribute('points', pt_otr);
  222. label_25.firstChild.data = Math.floor(1.1 * 0.25 * data_max);
  223. label_50.firstChild.data = Math.floor(1.1 * 0.50 * data_max);
  224. label_75.firstChild.data = Math.floor(1.1 * 0.75 * data_max);
  225. label_udp_cur.innerHTML = Math.floor(data_udp[data_udp.length-1]);
  226. label_tcp_cur.innerHTML = Math.floor(data_tcp[data_tcp.length-1]);
  227. label_otr_cur.innerHTML = Math.floor(data_otr[data_otr.length-1]);
  228. label_udp_avg.innerHTML = Math.floor(data_udp_avg);
  229. label_tcp_avg.innerHTML = Math.floor(data_tcp_avg);
  230. label_otr_avg.innerHTML = Math.floor(data_otr_avg);
  231. label_udp_peak.innerHTML = Math.floor(data_udp_peak);
  232. label_tcp_peak.innerHTML = Math.floor(data_tcp_peak);
  233. label_otr_peak.innerHTML = Math.floor(data_otr_peak);
  234. }
  235. );
  236. }
  237. }, 1000
  238. );
  239. //]]></script>
  240. <h2><a id="content" name="content"><%:Realtime Connections%></a></h2>
  241. <div class="cbi-map-descr"><%:This page gives an overview over currently active network connections.%></div>
  242. <fieldset class="cbi-section" id="cbi-table-table">
  243. <legend><%:Active Connections%></legend>
  244. <embed id="bwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/connections.svg" />
  245. <div style="text-align:right"><small id="scale">-</small></div>
  246. <br />
  247. <table style="width:100%; table-layout:fixed" cellspacing="5">
  248. <tr>
  249. <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid blue"><%:UDP:%></strong></td>
  250. <td id="lb_udp_cur">0</td>
  251. <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
  252. <td id="lb_udp_avg">0</td>
  253. <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
  254. <td id="lb_udp_peak">0</td>
  255. </tr>
  256. <tr>
  257. <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:TCP:%></strong></td>
  258. <td id="lb_tcp_cur">0</td>
  259. <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
  260. <td id="lb_tcp_avg">0</td>
  261. <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
  262. <td id="lb_tcp_peak">0</td>
  263. </tr>
  264. <tr>
  265. <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid red"><%:Other:%></strong></td>
  266. <td id="lb_otr_cur">0</td>
  267. <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
  268. <td id="lb_otr_avg">0</td>
  269. <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
  270. <td id="lb_otr_peak">0</td>
  271. </tr>
  272. </table>
  273. <br />
  274. <div class="cbi-section-node">
  275. <table class="cbi-section-table" id="connections">
  276. <tr class="cbi-section-table-titles">
  277. <th class="cbi-section-table-cell"><%:Network%></th>
  278. <th class="cbi-section-table-cell"><%:Protocol%></th>
  279. <th class="cbi-section-table-cell"><%:Source%></th>
  280. <th class="cbi-section-table-cell"><%:Destination%></th>
  281. <th class="cbi-section-table-cell"><%:Transfer%></th>
  282. </tr>
  283. <tr><td colspan="5"><em><%:Collecting data...%></em></td></tr>
  284. </table>
  285. </div>
  286. </fieldset>
  287. <%+footer%>