2
0

bandwidth.htm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <%#
  2. Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
  3. Licensed to the public under the Apache License 2.0.
  4. -%>
  5. <%-
  6. local ntm = require "luci.model.network".init()
  7. local dev
  8. local devices = { }
  9. for _, dev in luci.util.vspairs(luci.sys.net.devices()) do
  10. if dev ~= "lo" and not ntm:ignore_interface(dev) then
  11. devices[#devices+1] = dev
  12. end
  13. end
  14. local curdev = luci.http.formvalue("dev") or devices[1]
  15. -%>
  16. <%+header%>
  17. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  18. <script type="text/javascript">//<![CDATA[
  19. var bwxhr = new XHR();
  20. var G;
  21. var TIME = 0;
  22. var RXB = 1;
  23. var RXP = 2;
  24. var TXB = 3;
  25. var TXP = 4;
  26. var width = 760;
  27. var height = 300;
  28. var step = 5;
  29. var data_wanted = Math.floor(width / step);
  30. var data_fill = 0;
  31. var data_stamp = 0;
  32. var data_rx = [ ];
  33. var data_tx = [ ];
  34. var line_rx;
  35. var line_tx;
  36. var label_25;
  37. var label_50;
  38. var label_75;
  39. var label_rx_cur;
  40. var label_rx_avg;
  41. var label_rx_peak;
  42. var label_tx_cur;
  43. var label_tx_avg;
  44. var label_tx_peak;
  45. var label_scale;
  46. function bandwidth_label(bytes, br)
  47. {
  48. var uby = '<%:kB/s%>';
  49. var kby = (bytes / 1024);
  50. if (kby >= 1024)
  51. {
  52. uby = '<%:MB/s%>';
  53. kby = kby / 1024;
  54. }
  55. var ubi = '<%:kbit/s%>';
  56. var kbi = (bytes * 8 / 1024);
  57. if (kbi >= 1024)
  58. {
  59. ubi = '<%:Mbit/s%>';
  60. kbi = kbi / 1024;
  61. }
  62. return String.format("%f %s%s(%f %s)",
  63. kbi.toFixed(2), ubi,
  64. br ? '<br />' : ' ',
  65. kby.toFixed(2), uby
  66. );
  67. }
  68. /* wait for SVG */
  69. window.setTimeout(
  70. function() {
  71. var svg = document.getElementById('bwsvg');
  72. try {
  73. G = svg.getSVGDocument
  74. ? svg.getSVGDocument() : svg.contentDocument;
  75. }
  76. catch(e) {
  77. G = document.embeds['bwsvg'].getSVGDocument();
  78. }
  79. if (!G)
  80. {
  81. window.setTimeout(arguments.callee, 1000);
  82. }
  83. else
  84. {
  85. /* find sizes */
  86. width = svg.offsetWidth - 2;
  87. height = svg.offsetHeight - 2;
  88. data_wanted = Math.ceil(width / step);
  89. /* prefill datasets */
  90. for (var i = 0; i < data_wanted; i++)
  91. {
  92. data_rx[i] = 0;
  93. data_tx[i] = 0;
  94. }
  95. /* find svg elements */
  96. line_rx = G.getElementById('rx');
  97. line_tx = G.getElementById('tx');
  98. label_25 = G.getElementById('label_25');
  99. label_50 = G.getElementById('label_50');
  100. label_75 = G.getElementById('label_75');
  101. label_rx_cur = document.getElementById('rx_bw_cur');
  102. label_rx_avg = document.getElementById('rx_bw_avg');
  103. label_rx_peak = document.getElementById('rx_bw_peak');
  104. label_tx_cur = document.getElementById('tx_bw_cur');
  105. label_tx_avg = document.getElementById('tx_bw_avg');
  106. label_tx_peak = document.getElementById('tx_bw_peak');
  107. label_scale = document.getElementById('scale');
  108. /* plot horizontal time interval lines */
  109. for (var i = width % (step * 60); i < width; i += step * 60)
  110. {
  111. var line = G.createElementNS('http://www.w3.org/2000/svg', 'line');
  112. line.setAttribute('x1', i);
  113. line.setAttribute('y1', 0);
  114. line.setAttribute('x2', i);
  115. line.setAttribute('y2', '100%');
  116. line.setAttribute('style', 'stroke:black;stroke-width:0.1');
  117. var text = G.createElementNS('http://www.w3.org/2000/svg', 'text');
  118. text.setAttribute('x', i + 5);
  119. text.setAttribute('y', 15);
  120. text.setAttribute('style', 'fill:#999999; font-size:9pt');
  121. text.appendChild(G.createTextNode(Math.round((width - i) / step / 60) + 'm'));
  122. label_25.parentNode.appendChild(line);
  123. label_25.parentNode.appendChild(text);
  124. }
  125. label_scale.innerHTML = String.format('<%:(%d minute window, %d second interval)%>', data_wanted / 60, 3);
  126. /* render datasets, start update interval */
  127. XHR.poll(3, '<%=build_url("admin/status/realtime/bandwidth_status", curdev)%>', null,
  128. function(x, data)
  129. {
  130. var data_max = 0;
  131. var data_scale = 0;
  132. var data_rx_avg = 0;
  133. var data_tx_avg = 0;
  134. var data_rx_peak = 0;
  135. var data_tx_peak = 0;
  136. for (var i = data_stamp ? 0 : 1; i < data.length; i++)
  137. {
  138. /* skip overlapping entries */
  139. if (data[i][TIME] <= data_stamp)
  140. continue;
  141. /* normalize difference against time interval */
  142. if (i > 0)
  143. {
  144. var time_delta = data[i][TIME] - data[i-1][TIME];
  145. if (time_delta)
  146. {
  147. data_rx.push((data[i][RXB] - data[i-1][RXB]) / time_delta);
  148. data_tx.push((data[i][TXB] - data[i-1][TXB]) / time_delta);
  149. }
  150. }
  151. }
  152. /* cut off outdated entries */
  153. data_rx = data_rx.slice(data_rx.length - data_wanted, data_rx.length);
  154. data_tx = data_tx.slice(data_tx.length - data_wanted, data_tx.length);
  155. /* find peak */
  156. for (var i = 0; i < data_rx.length; i++)
  157. {
  158. data_max = Math.max(data_max, data_rx[i]);
  159. data_max = Math.max(data_max, data_tx[i]);
  160. data_rx_peak = Math.max(data_rx_peak, data_rx[i]);
  161. data_tx_peak = Math.max(data_tx_peak, data_tx[i]);
  162. if (i > 0)
  163. {
  164. data_rx_avg = (data_rx_avg + data_rx[i]) / 2;
  165. data_tx_avg = (data_tx_avg + data_tx[i]) / 2;
  166. }
  167. else
  168. {
  169. data_rx_avg = data_rx[i];
  170. data_tx_avg = data_tx[i];
  171. }
  172. }
  173. /* remember current timestamp, calculate horizontal scale */
  174. data_stamp = data[data.length-1][TIME];
  175. data_scale = height / (data_max * 1.1);
  176. /* plot data */
  177. var pt_rx = '0,' + height;
  178. var pt_tx = '0,' + height;
  179. var y_rx = 0;
  180. var y_tx = 0;
  181. for (var i = 0; i < data_rx.length; i++)
  182. {
  183. var x = i * step;
  184. y_rx = height - Math.floor(data_rx[i] * data_scale);
  185. y_tx = height - Math.floor(data_tx[i] * data_scale);
  186. pt_rx += ' ' + x + ',' + y_rx;
  187. pt_tx += ' ' + x + ',' + y_tx;
  188. }
  189. pt_rx += ' ' + width + ',' + y_rx + ' ' + width + ',' + height;
  190. pt_tx += ' ' + width + ',' + y_tx + ' ' + width + ',' + height;
  191. line_rx.setAttribute('points', pt_rx);
  192. line_tx.setAttribute('points', pt_tx);
  193. label_25.firstChild.data = bandwidth_label(1.1 * 0.25 * data_max);
  194. label_50.firstChild.data = bandwidth_label(1.1 * 0.50 * data_max);
  195. label_75.firstChild.data = bandwidth_label(1.1 * 0.75 * data_max);
  196. label_rx_cur.innerHTML = bandwidth_label(data_rx[data_rx.length-1], true);
  197. label_tx_cur.innerHTML = bandwidth_label(data_tx[data_tx.length-1], true);
  198. label_rx_avg.innerHTML = bandwidth_label(data_rx_avg, true);
  199. label_tx_avg.innerHTML = bandwidth_label(data_tx_avg, true);
  200. label_rx_peak.innerHTML = bandwidth_label(data_rx_peak, true);
  201. label_tx_peak.innerHTML = bandwidth_label(data_tx_peak, true);
  202. }
  203. );
  204. }
  205. }, 1000
  206. );
  207. //]]></script>
  208. <h2 name="content"><%:Realtime Traffic%></h2>
  209. <ul class="cbi-tabmenu">
  210. <% for _, dev in ipairs(devices) do %>
  211. <li class="cbi-tab<%= dev == curdev and "" or "-disabled" %>"><a href="?dev=<%=pcdata(dev)%>"><%=pcdata(dev)%></a></li>
  212. <% end %>
  213. </ul>
  214. <embed id="bwsvg" style="width:100%; height:300px; border:1px solid #000000; background-color:#FFFFFF" src="<%=resource%>/bandwidth.svg" />
  215. <div style="text-align:right"><small id="scale">-</small></div>
  216. <br />
  217. <table style="width:100%; table-layout:fixed" cellspacing="5">
  218. <tr>
  219. <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid blue"><%:Inbound:%></strong></td>
  220. <td id="rx_bw_cur">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
  221. <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
  222. <td id="rx_bw_avg">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
  223. <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
  224. <td id="rx_bw_peak">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
  225. </tr>
  226. <tr>
  227. <td style="text-align:right; vertical-align:top"><strong style="border-bottom:2px solid green"><%:Outbound:%></strong></td>
  228. <td id="tx_bw_cur">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
  229. <td style="text-align:right; vertical-align:top"><strong><%:Average:%></strong></td>
  230. <td id="tx_bw_avg">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
  231. <td style="text-align:right; vertical-align:top"><strong><%:Peak:%></strong></td>
  232. <td id="tx_bw_peak">0 <%:kbit/s%><br />(0 <%:kB/s%>)</td>
  233. </tr>
  234. </table>
  235. <%+footer%>