serverstatus.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // serverstatus.js. big data boom today.
  2. var error = 0;
  3. var d = 0;
  4. var server_status = new Array();
  5. function timeSince(date) {
  6. if(date == 0)
  7. return "从未.";
  8. var seconds = Math.floor((new Date() - date) / 1000);
  9. var interval = Math.floor(seconds / 60);
  10. if (interval > 1)
  11. return interval + " 分钟前.";
  12. else
  13. return "几秒前.";
  14. }
  15. function bytesToSize(bytes, precision, si)
  16. {
  17. var ret;
  18. si = typeof si !== 'undefined' ? si : 0;
  19. if(si != 0) {
  20. var megabyte = 1000 * 1000;
  21. var gigabyte = megabyte * 1000;
  22. var terabyte = gigabyte * 1000;
  23. } else {
  24. var megabyte = 1024 * 1024;
  25. var gigabyte = megabyte * 1024;
  26. var terabyte = gigabyte * 1024;
  27. }
  28. if ((bytes >= megabyte) && (bytes < gigabyte)) {
  29. ret = (bytes / megabyte).toFixed(precision) + ' M';
  30. } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
  31. ret = (bytes / gigabyte).toFixed(precision) + ' G';
  32. } else if (bytes >= terabyte) {
  33. ret = (bytes / terabyte).toFixed(precision) + ' T';
  34. } else {
  35. return bytes + ' B';
  36. }
  37. return ret;
  38. /*if(si != 0) {
  39. return ret + 'B';
  40. } else {
  41. return ret + 'iB';
  42. }*/
  43. }
  44. function uptime() {
  45. $.getJSON("json/stats.json", function(result) {
  46. $("#loading-notice").remove();
  47. if(result.reload)
  48. setTimeout(function() { location.reload() }, 1000);
  49. for (var i = 0, rlen=result.servers.length; i < rlen; i++) {
  50. var TableRow = $("#servers tr#r" + i);
  51. var ExpandRow = $("#servers #rt" + i);
  52. var hack; // fuck CSS for making me do this
  53. if(i%2) hack="odd"; else hack="even";
  54. if (!TableRow.length) {
  55. $("#servers").append(
  56. "<tr id=\"r" + i + "\" data-toggle=\"collapse\" data-target=\"#rt" + i + "\" class=\"accordion-toggle " + hack + "\">" +
  57. "<td id=\"online_status\"><div class=\"progress\"><div style=\"width: 100%;\" class=\"progress-bar progress-bar-warning\"><small>加载中</small></div></div></td>" +
  58. "<td id=\"month_traffic\"><div class=\"progress\"><div style=\"width: 100%;\" class=\"progress-bar progress-bar-warning\"><small>加载中</small></div></div></td>" +
  59. "<td id=\"name\">加载中</td>" +
  60. "<td id=\"type\">加载中</td>" +
  61. "<td id=\"location\">加载中</td>" +
  62. "<td id=\"uptime\">加载中</td>" +
  63. "<td id=\"load\">加载中</td>" +
  64. "<td id=\"network\">加载中</td>" +
  65. "<td id=\"traffic\">加载中</td>" +
  66. "<td id=\"cpu\"><div class=\"progress\"><div style=\"width: 100%;\" class=\"progress-bar progress-bar-warning\"><small>加载中</small></div></div></td>" +
  67. "<td id=\"memory\"><div class=\"progress\"><div style=\"width: 100%;\" class=\"progress-bar progress-bar-warning\"><small>加载中</small></div></div></td>" +
  68. "<td id=\"hdd\"><div class=\"progress\"><div style=\"width: 100%;\" class=\"progress-bar progress-bar-warning\"><small>加载中</small></div></div></td>" +
  69. "<td id=\"ping\"><div class=\"progress\"><div style=\"width: 100%;\" class=\"progress-bar progress-bar-warning\"><small>加载中</small></div></div></td>" +
  70. "</tr>" +
  71. "<tr class=\"expandRow " + hack + "\"><td colspan=\"16\"><div class=\"accordian-body collapse\" id=\"rt" + i + "\">" +
  72. "<div id=\"expand_mem\">加载中</div>" +
  73. "<div id=\"expand_swap\">加载中</div>" +
  74. "<div id=\"expand_hdd\">加载中</div>" +
  75. "<div id=\"expand_tupd\">加载中</div>" +
  76. "<div id=\"expand_ping\">加载中</div>" +
  77. "<div id=\"expand_custom\">加载中</div>" +
  78. "</div></td></tr>"
  79. );
  80. TableRow = $("#servers tr#r" + i);
  81. ExpandRow = $("#servers #rt" + i);
  82. server_status[i] = true;
  83. }
  84. TableRow = TableRow[0];
  85. if(error) {
  86. TableRow.setAttribute("data-target", "#rt" + i);
  87. server_status[i] = true;
  88. }
  89. // online_status
  90. if (result.servers[i].online4 && !result.servers[i].online6) {
  91. TableRow.children["online_status"].children[0].children[0].className = "progress-bar progress-bar-success";
  92. TableRow.children["online_status"].children[0].children[0].innerHTML = "<small>IPv4</small>";
  93. } else if (result.servers[i].online4 && result.servers[i].online6) {
  94. TableRow.children["online_status"].children[0].children[0].className = "progress-bar progress-bar-success";
  95. TableRow.children["online_status"].children[0].children[0].innerHTML = "<small>双栈</small>";
  96. } else if (!result.servers[i].online4 && result.servers[i].online6) {
  97. TableRow.children["online_status"].children[0].children[0].className = "progress-bar progress-bar-success";
  98. TableRow.children["online_status"].children[0].children[0].innerHTML = "<small>IPv6</small>";
  99. } else {
  100. TableRow.children["online_status"].children[0].children[0].className = "progress-bar progress-bar-danger";
  101. TableRow.children["online_status"].children[0].children[0].innerHTML = "<small>关闭</small>";
  102. }
  103. // Name
  104. TableRow.children["name"].innerHTML = result.servers[i].name;
  105. // Type
  106. TableRow.children["type"].innerHTML = result.servers[i].type;
  107. // Location
  108. TableRow.children["location"].innerHTML = result.servers[i].location;
  109. if (!result.servers[i].online4 && !result.servers[i].online6) {
  110. if (server_status[i]) {
  111. TableRow.children["uptime"].innerHTML = "–";
  112. TableRow.children["load"].innerHTML = "–";
  113. TableRow.children["network"].innerHTML = "–";
  114. TableRow.children["traffic"].innerHTML = "–";
  115. TableRow.children["month_traffic"].children[0].children[0].className = "progress-bar progress-bar-warning";
  116. TableRow.children["month_traffic"].children[0].children[0].innerHTML = "<small>关闭</small>";
  117. TableRow.children["cpu"].children[0].children[0].className = "progress-bar progress-bar-danger";
  118. TableRow.children["cpu"].children[0].children[0].style.width = "100%";
  119. TableRow.children["cpu"].children[0].children[0].innerHTML = "<small>关闭</small>";
  120. TableRow.children["memory"].children[0].children[0].className = "progress-bar progress-bar-danger";
  121. TableRow.children["memory"].children[0].children[0].style.width = "100%";
  122. TableRow.children["memory"].children[0].children[0].innerHTML = "<small>关闭</small>";
  123. TableRow.children["hdd"].children[0].children[0].className = "progress-bar progress-bar-danger";
  124. TableRow.children["hdd"].children[0].children[0].style.width = "100%";
  125. TableRow.children["hdd"].children[0].children[0].innerHTML = "<small>关闭</small>";
  126. TableRow.children["ping"].children[0].children[0].className = "progress-bar progress-bar-danger";
  127. TableRow.children["ping"].children[0].children[0].style.width = "100%";
  128. TableRow.children["ping"].children[0].children[0].innerHTML = "<small>关闭</small>";
  129. if(ExpandRow.hasClass("in")) {
  130. ExpandRow.collapse("hide");
  131. }
  132. TableRow.setAttribute("data-target", "");
  133. server_status[i] = false;
  134. }
  135. } else {
  136. if (!server_status[i]) {
  137. TableRow.setAttribute("data-target", "#rt" + i);
  138. server_status[i] = true;
  139. }
  140. // month traffic
  141. var monthtraffic = "";
  142. var trafficdiff_in = result.servers[i].network_in - result.servers[i].last_network_in;
  143. var trafficdiff_out = result.servers[i].network_out - result.servers[i].last_network_out;
  144. if(trafficdiff_in < 1024*1024*1024*1024)
  145. monthtraffic += (trafficdiff_in/1024/1024/1024).toFixed(1) + "G";
  146. else
  147. monthtraffic += (trafficdiff_in/1024/1024/1024/1024).toFixed(1) + "T";
  148. monthtraffic += " | "
  149. if(trafficdiff_out < 1024*1024*1024*1024)
  150. monthtraffic += (trafficdiff_out/1024/1024/1024).toFixed(1) + "G";
  151. else
  152. monthtraffic += (trafficdiff_out/1024/1024/1024/1024).toFixed(1) + "T";
  153. TableRow.children["month_traffic"].children[0].children[0].className = "progress-bar progress-bar-success";
  154. TableRow.children["month_traffic"].children[0].children[0].innerHTML = "<small>"+monthtraffic+"</small>";
  155. // Uptime
  156. TableRow.children["uptime"].innerHTML = result.servers[i].uptime;
  157. // Load: default load_1, you can change show: load_1, load_5, load_15
  158. if(result.servers[i].load == -1) {
  159. TableRow.children["load"].innerHTML = "–";
  160. } else {
  161. TableRow.children["load"].innerHTML = result.servers[i].load_1.toFixed(2);
  162. }
  163. // Network
  164. var netstr = "";
  165. if(result.servers[i].network_rx < 1024*1024)
  166. netstr += (result.servers[i].network_rx/1024).toFixed(1) + "K";
  167. else
  168. netstr += (result.servers[i].network_rx/1024/1024).toFixed(1) + "M";
  169. netstr += " | "
  170. if(result.servers[i].network_tx < 1024*1024)
  171. netstr += (result.servers[i].network_tx/1024).toFixed(1) + "K";
  172. else
  173. netstr += (result.servers[i].network_tx/1024/1024).toFixed(1) + "M";
  174. TableRow.children["network"].innerHTML = netstr;
  175. //Traffic
  176. var trafficstr = "";
  177. if(result.servers[i].network_in < 1024*1024*1024*1024)
  178. trafficstr += (result.servers[i].network_in/1024/1024/1024).toFixed(1) + "G";
  179. else
  180. trafficstr += (result.servers[i].network_in/1024/1024/1024/1024).toFixed(1) + "T";
  181. trafficstr += " | "
  182. if(result.servers[i].network_out < 1024*1024*1024*1024)
  183. trafficstr += (result.servers[i].network_out/1024/1024/1024).toFixed(1) + "G";
  184. else
  185. trafficstr += (result.servers[i].network_out/1024/1024/1024/1024).toFixed(1) + "T";
  186. TableRow.children["traffic"].innerHTML = trafficstr;
  187. // CPU
  188. if (result.servers[i].cpu >= 90)
  189. TableRow.children["cpu"].children[0].children[0].className = "progress-bar progress-bar-danger";
  190. else if (result.servers[i].cpu >= 80)
  191. TableRow.children["cpu"].children[0].children[0].className = "progress-bar progress-bar-warning";
  192. else
  193. TableRow.children["cpu"].children[0].children[0].className = "progress-bar progress-bar-success";
  194. TableRow.children["cpu"].children[0].children[0].style.width = result.servers[i].cpu + "%";
  195. TableRow.children["cpu"].children[0].children[0].innerHTML = result.servers[i].cpu + "%";
  196. // Memory
  197. var Mem = ((result.servers[i].memory_used/result.servers[i].memory_total)*100.0).toFixed(0);
  198. if (Mem >= 90)
  199. TableRow.children["memory"].children[0].children[0].className = "progress-bar progress-bar-danger";
  200. else if (Mem >= 80)
  201. TableRow.children["memory"].children[0].children[0].className = "progress-bar progress-bar-warning";
  202. else
  203. TableRow.children["memory"].children[0].children[0].className = "progress-bar progress-bar-success";
  204. TableRow.children["memory"].children[0].children[0].style.width = Mem + "%";
  205. TableRow.children["memory"].children[0].children[0].innerHTML = Mem + "%";
  206. ExpandRow[0].children["expand_mem"].innerHTML = "内存: " + bytesToSize(result.servers[i].memory_used*1024, 2) + " / " + bytesToSize(result.servers[i].memory_total*1024, 2);
  207. // Swap
  208. ExpandRow[0].children["expand_swap"].innerHTML = "交换分区: " + bytesToSize(result.servers[i].swap_used*1024, 2) + " / " + bytesToSize(result.servers[i].swap_total*1024, 2);
  209. // HDD
  210. var HDD = ((result.servers[i].hdd_used/result.servers[i].hdd_total)*100.0).toFixed(0);
  211. if (HDD >= 90)
  212. TableRow.children["hdd"].children[0].children[0].className = "progress-bar progress-bar-danger";
  213. else if (HDD >= 80)
  214. TableRow.children["hdd"].children[0].children[0].className = "progress-bar progress-bar-warning";
  215. else
  216. TableRow.children["hdd"].children[0].children[0].className = "progress-bar progress-bar-success";
  217. TableRow.children["hdd"].children[0].children[0].style.width = HDD + "%";
  218. TableRow.children["hdd"].children[0].children[0].innerHTML = HDD + "%";
  219. // IO Speed for HDD.
  220. // IO, 过小的B字节单位没有意义
  221. var io = "";
  222. if(result.servers[i].io_read < 1024*1024)
  223. io += parseInt(result.servers[i].io_read/1024) + "K";
  224. else
  225. io += parseInt(result.servers[i].io_read/1024/1024) + "M";
  226. io += " / "
  227. if(result.servers[i].io_write < 1024*1024)
  228. io += parseInt(result.servers[i].io_write/1024) + "K";
  229. else
  230. io += parseInt(result.servers[i].io_write/1024/1024) + "M";
  231. // Expand for HDD.
  232. ExpandRow[0].children["expand_hdd"].innerHTML = "硬盘|读写: " + bytesToSize(result.servers[i].hdd_used*1024*1024, 2) + " / " + bytesToSize(result.servers[i].hdd_total*1024*1024, 2) + " | " + io;
  233. // delay time
  234. // tcp, udp, process, thread count
  235. ExpandRow[0].children["expand_tupd"].innerHTML = "TCP/UDP/进/线: " + result.servers[i].tcp_count + " / " + result.servers[i].udp_count + " / " + result.servers[i].process_count+ " / " + result.servers[i].thread_count;
  236. ExpandRow[0].children["expand_ping"].innerHTML = "联通/电信/移动: " + result.servers[i].time_10010 + "ms / " + result.servers[i].time_189 + "ms / " + result.servers[i].time_10086 + "ms"
  237. // ping
  238. var PING_10010 = result.servers[i].ping_10010.toFixed(0);
  239. var PING_189 = result.servers[i].ping_189.toFixed(0);
  240. var PING_10086 = result.servers[i].ping_10086.toFixed(0);
  241. if (PING_10010 >= 20 || PING_189 >= 20 || PING_10086 >= 20)
  242. TableRow.children["ping"].children[0].children[0].className = "progress-bar progress-bar-warning";
  243. else
  244. TableRow.children["ping"].children[0].children[0].className = "progress-bar progress-bar-success";
  245. TableRow.children["ping"].children[0].children[0].innerHTML = PING_10010 + "%💻" + PING_189 + "%💻" + PING_10086 + "%";
  246. // Custom
  247. if (result.servers[i].custom) {
  248. ExpandRow[0].children["expand_custom"].innerHTML = result.servers[i].custom
  249. } else {
  250. ExpandRow[0].children["expand_custom"].innerHTML = ""
  251. }
  252. }
  253. };
  254. d = new Date(result.updated*1000);
  255. error = 0;
  256. }).fail(function(update_error) {
  257. if (!error) {
  258. $("#servers > tr.accordion-toggle").each(function(i) {
  259. var TableRow = $("#servers tr#r" + i)[0];
  260. var ExpandRow = $("#servers #rt" + i);
  261. TableRow.children["online_status"].children[0].children[0].className = "progress-bar progress-bar-error";
  262. TableRow.children["online_status"].children[0].children[0].innerHTML = "<small>错误</small>";
  263. TableRow.children["month_traffic"].children[0].children[0].className = "progress-bar progress-bar-error";
  264. TableRow.children["month_traffic"].children[0].children[0].innerHTML = "<small>错误</small>";
  265. TableRow.children["uptime"].children[0].children[0].className = "progress-bar progress-bar-error";
  266. TableRow.children["uptime"].children[0].children[0].innerHTML = "<small>错误</small>";
  267. TableRow.children["load"].children[0].children[0].className = "progress-bar progress-bar-error";
  268. TableRow.children["load"].children[0].children[0].innerHTML = "<small>错误</small>";
  269. TableRow.children["network"].children[0].children[0].className = "progress-bar progress-bar-error";
  270. TableRow.children["network"].children[0].children[0].innerHTML = "<small>错误</small>";
  271. TableRow.children["traffic"].children[0].children[0].className = "progress-bar progress-bar-error";
  272. TableRow.children["traffic"].children[0].children[0].innerHTML = "<small>错误</small>";
  273. TableRow.children["cpu"].children[0].children[0].className = "progress-bar progress-bar-error";
  274. TableRow.children["cpu"].children[0].children[0].style.width = "100%";
  275. TableRow.children["cpu"].children[0].children[0].innerHTML = "<small>错误</small>";
  276. TableRow.children["memory"].children[0].children[0].className = "progress-bar progress-bar-error";
  277. TableRow.children["memory"].children[0].children[0].style.width = "100%";
  278. TableRow.children["memory"].children[0].children[0].innerHTML = "<small>错误</small>";
  279. TableRow.children["hdd"].children[0].children[0].className = "progress-bar progress-bar-error";
  280. TableRow.children["hdd"].children[0].children[0].style.width = "100%";
  281. TableRow.children["hdd"].children[0].children[0].innerHTML = "<small>错误</small>";
  282. TableRow.children["ping"].children[0].children[0].className = "progress-bar progress-bar-error";
  283. TableRow.children["ping"].children[0].children[0].style.width = "100%";
  284. TableRow.children["ping"].children[0].children[0].innerHTML = "<small>错误</small>";
  285. if(ExpandRow.hasClass("in")) {
  286. ExpandRow.collapse("hide");
  287. }
  288. TableRow.setAttribute("data-target", "");
  289. server_status[i] = false;
  290. });
  291. }
  292. error = 1;
  293. $("#updated").html("更新错误.");
  294. });
  295. }
  296. function updateTime() {
  297. if (!error)
  298. $("#updated").html("最后更新: " + timeSince(d));
  299. }
  300. uptime();
  301. updateTime();
  302. setInterval(uptime, 2000);
  303. setInterval(updateTime, 2000);
  304. // styleswitcher.js
  305. function setActiveStyleSheet(title, cookie=false) {
  306. var i, a, main;
  307. for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  308. if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
  309. a.disabled = true;
  310. if(a.getAttribute("title") == title) a.disabled = false;
  311. }
  312. }
  313. if (true==cookie) {
  314. createCookie("style", title, 365);
  315. }
  316. }
  317. function getActiveStyleSheet() {
  318. var i, a;
  319. for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  320. if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
  321. return a.getAttribute("title");
  322. }
  323. return null;
  324. }
  325. function createCookie(name,value,days) {
  326. if (days) {
  327. var date = new Date();
  328. date.setTime(date.getTime()+(days*24*60*60*1000));
  329. var expires = "; expires="+date.toGMTString();
  330. }
  331. else expires = "";
  332. document.cookie = name+"="+value+expires+"; path=/";
  333. }
  334. function readCookie(name) {
  335. var nameEQ = name + "=";
  336. var ca = document.cookie.split(';');
  337. for(var i=0;i < ca.length;i++) {
  338. var c = ca[i];
  339. while (c.charAt(0)==' ')
  340. c = c.substring(1,c.length);
  341. if (c.indexOf(nameEQ) == 0)
  342. return c.substring(nameEQ.length,c.length);
  343. }
  344. return null;
  345. }
  346. window.onload = function(e) {
  347. var cookie = readCookie("style");
  348. if (cookie && cookie != 'null' ) {
  349. setActiveStyleSheet(cookie);
  350. } else {
  351. function handleChange (mediaQueryListEvent) {
  352. if (mediaQueryListEvent.matches) {
  353. setActiveStyleSheet('dark');
  354. } else {
  355. setActiveStyleSheet('light');
  356. }
  357. }
  358. const mediaQueryListDark = window.matchMedia('(prefers-color-scheme: dark)');
  359. setActiveStyleSheet(mediaQueryListDark.matches ? 'dark' : 'light');
  360. mediaQueryListDark.addEventListener("change",handleChange);
  361. }
  362. }