2
0

container_stats.htm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <script>
  2. let last_bw_tx
  3. let last_bw_rx
  4. let interval = 3
  5. function progressbar(v, m, pc, np, f) {
  6. m = m || 100
  7. return String.format(
  8. '<div style="width:100%%; max-width:500px; position:relative; border:1px solid #999999">' +
  9. '<div style="background-color:#CCCCCC; width:%d%%; height:15px">' +
  10. '<div style="position:absolute; left:0; top:0; text-align:center; width:100%%; color:#000000">' +
  11. '<small>%s '+(f?f:'/')+' %s ' + (np ? "" : '(%d%%)') + '</small>' +
  12. '</div>' +
  13. '</div>' +
  14. '</div>', pc, v, m, pc, f
  15. );
  16. }
  17. function niceBytes(bytes, decimals) {
  18. if (bytes == 0) return '0 Bytes';
  19. var k = 1000,
  20. dm = decimals + 1 || 3,
  21. sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
  22. i = Math.floor(Math.log(bytes) / Math.log(k));
  23. return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
  24. }
  25. XHR.poll(interval, '<%=luci.dispatcher.build_url("admin/docker/container_stats")%>/<%=self.container_id%>', { status: 1 },
  26. function (x, info) {
  27. var e;
  28. if (e = document.getElementById('cbi-table-cpu-value'))
  29. e.innerHTML = progressbar(
  30. (info.cpu_percent), 100, (info.cpu_percent ? info.cpu_percent : 0));
  31. if (e = document.getElementById('cbi-table-memory-value'))
  32. e.innerHTML = progressbar(
  33. niceBytes(info.memory.mem_useage),
  34. niceBytes(info.memory.mem_limit),
  35. ((100 / (info.memory.mem_limit ? info.memory.mem_limit : 100)) * (info.memory.mem_useage ? info.memory.mem_useage : 0))
  36. );
  37. for (var eth in info.bw_rxtx) {
  38. if (!document.getElementById("cbi-table-speed_" + eth + "-value")) {
  39. let tab = document.getElementById("cbi-table-cpu").parentNode
  40. let div = document.getElementById('cbi-table-cpu').cloneNode(true);
  41. div.id = "cbi-table-speed_" + eth;
  42. div.children[0].innerHTML = "<%:Upload/Download%>: " + eth
  43. div.children[1].id = "cbi-table-speed_" + eth + "-value"
  44. tab.appendChild(div)
  45. }
  46. if (!document.getElementById("cbi-table-network_" + eth + "-value")) {
  47. let tab = document.getElementById("cbi-table-cpu").parentNode
  48. let div = document.getElementById('cbi-table-cpu').cloneNode(true);
  49. div.id = "cbi-table-network_" + eth;
  50. div.children[0].innerHTML = "<%:TX/RX%>: " + eth
  51. div.children[1].id = "cbi-table-network_" + eth + "-value"
  52. tab.appendChild(div)
  53. }
  54. e = document.getElementById("cbi-table-network_" + eth + "-value")
  55. e.innerHTML = progressbar(
  56. '↑'+niceBytes(info.bw_rxtx[eth].bw_tx),
  57. '↓'+niceBytes(info.bw_rxtx[eth].bw_rx),
  58. null,
  59. true, " "
  60. );
  61. e = document.getElementById("cbi-table-speed_" + eth + "-value")
  62. if (! last_bw_tx) last_bw_tx = info.bw_rxtx[eth].bw_tx
  63. if (! last_bw_rx) last_bw_rx = info.bw_rxtx[eth].bw_rx
  64. e.innerHTML = progressbar(
  65. '↑'+niceBytes((info.bw_rxtx[eth].bw_tx - last_bw_tx)/interval)+'/s',
  66. '↓'+niceBytes((info.bw_rxtx[eth].bw_rx - last_bw_rx)/interval)+'/s',
  67. null,
  68. true, " "
  69. );
  70. last_bw_tx = info.bw_rxtx[eth].bw_tx
  71. last_bw_rx = info.bw_rxtx[eth].bw_rx
  72. }
  73. });
  74. </script>