ssview.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. # coding: utf-8
  3. # Update by : https://github.com/cppla/ServerStatus, Update date: 20211009
  4. # 支持Python版本:2.7 to 3.9; requirements.txt: requests, PrettyTable
  5. # 主要是为了受到CC attack时候方便查看机器状态
  6. import os
  7. import sys
  8. import requests
  9. import time
  10. from prettytable import PrettyTable
  11. scroll = True
  12. clear = lambda: os.system('clear' if 'linux' in sys.platform or 'darwin' in sys.platform else 'cls')
  13. def sscmd(address):
  14. while True:
  15. r = requests.get(
  16. url=address,
  17. headers={
  18. "User-Agent": "ServerStatus/20181203",
  19. }
  20. )
  21. jsonR = r.json()
  22. ss = PrettyTable(
  23. [
  24. "月流量 ↓|↑",
  25. "节点名",
  26. "位置",
  27. "在线时间",
  28. "负载",
  29. "网络 ↓|↑",
  30. "总流量 ↓|↑",
  31. "处理器",
  32. "内存",
  33. "硬盘"
  34. ],
  35. )
  36. for i in jsonR["servers"]:
  37. if i["online4"] is False and i["online6"] is False:
  38. ss.add_row(
  39. [
  40. '0.00G',
  41. "%s" % i["name"],
  42. "%s" % i["location"],
  43. '-',
  44. '-',
  45. '-',
  46. '-',
  47. '-',
  48. '-',
  49. '-',
  50. ]
  51. )
  52. else:
  53. ss.add_row(
  54. [
  55. "%.2fG|%.2fG" % (float(i["last_network_in"]) / 1024 / 1024 / 1024, float(i["last_network_out"]) / 1024 / 1024 / 1024),
  56. "%s" % i["name"],
  57. # "%s" % i["type"],
  58. "%s" % i["location"],
  59. "%s" % i["uptime"],
  60. "%s" % (i["load_1"]),
  61. "%.2fM|%.2fM" % (float(i["network_rx"]) / 1000 / 1000, float(i["network_tx"]) / 1000 / 1000),
  62. "%.2fG|%.2fG" % (
  63. float(i["network_in"]) / 1024 / 1024 / 1024, float(i["network_out"]) / 1024 / 1024 / 1024),
  64. "%d%%" % (i["cpu"]),
  65. "%d%%" % (float(i["memory_used"]) / i["memory_total"] * 100),
  66. "%d%%" % (float(i["hdd_used"]) / i["hdd_total"] * 100),
  67. ]
  68. )
  69. if scroll is True:
  70. clear()
  71. print(ss)
  72. time.sleep(1)
  73. if __name__ == '__main__':
  74. default = 'https://tz.cloudcpp.com/json/stats.json'
  75. ads = sys.argv[1] if len(sys.argv) == 2 else default
  76. sscmd(ads)