ssview.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Update by : https://github.com/cppla/ServerStatus
  4. # 支持Python版本:2.7 to 3.5; requirements.txt: requests, PrettyTable
  5. # 时间: 20180828
  6. '''
  7. maybe better by youself
  8. '''
  9. import os
  10. import sys
  11. import requests
  12. import time
  13. from prettytable import PrettyTable
  14. # todo: 程序在非gui环境下目前有闪屏的bug
  15. scroll = True
  16. clear = lambda: os.system('clear' if 'linux' in sys.platform else 'cls')
  17. def sscmd(address):
  18. while True:
  19. r = requests.get(
  20. url=address,
  21. headers={
  22. "User-Agent": "ServerStatus/20181203",
  23. }
  24. )
  25. jsonR = r.json()
  26. ss = PrettyTable(
  27. [
  28. "Flight",
  29. "节点名",
  30. # "虚拟化",
  31. "位置",
  32. "在线时间",
  33. "负载",
  34. "网络",
  35. "流量",
  36. "处理器",
  37. "内存",
  38. "硬盘"
  39. ],
  40. )
  41. for i in jsonR["servers"]:
  42. ss.add_row(
  43. [
  44. "%s" % 'MH361' if i["ip_status"] is True else 'MH370',
  45. "%s" % i["name"],
  46. # "%s" % i["type"],
  47. "%s" % i["location"],
  48. "%s" % i["uptime"],
  49. "%s" % (i["load_1"]),
  50. "%.2fM|%.2fM" % (float(i["network_rx"]) / 1000 / 1000, float(i["network_tx"]) / 1000 / 1000),
  51. "%.2fG|%.2fG" % (
  52. float(i["network_in"]) / 1024 / 1024 / 1024, float(i["network_out"]) / 1024 / 1024 / 1024),
  53. "%d%%" % (i["cpu"]),
  54. "%d%%" % (float(i["memory_used"]) / i["memory_total"] * 100),
  55. "%d%%" % (float(i["hdd_used"]) / i["hdd_total"] * 100),
  56. ]
  57. )
  58. if scroll is True:
  59. clear()
  60. print(ss)
  61. time.sleep(1)
  62. if __name__ == '__main__':
  63. default = 'https://tz.cloudcpp.com/json/stats.json'
  64. ads = sys.argv[1] if len(sys.argv)==2 else default
  65. sscmd(ads)