You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
4.1KB

  1. from os.path import dirname
  2. from traceback import format_exc
  3. import psutil
  4. from loguru import logger
  5. from enums.ExceptionEnum import ExceptionType
  6. from exception.CustomerException import ServiceException
  7. def check_cpu(base_dir, requestId=None):
  8. path = dirname(base_dir)
  9. cpu_use = psutil.cpu_percent()
  10. cpu_mem = psutil.virtual_memory().percent
  11. cpu_swap = psutil.swap_memory().percent
  12. cpu_disk = psutil.disk_usage(path).percent
  13. if float(cpu_use) > 70 or float(cpu_mem) > 70 or cpu_swap > 85 or cpu_disk > 90:
  14. if requestId:
  15. logger.info("""###############################################################################################
  16. CPU 使用率:{}, 内存使用:{}, SWAP内存使用率:{}, 服务磁盘使用率:{}, requestId:{}
  17. ###############################################################################################""",
  18. cpu_use, cpu_mem, cpu_swap, cpu_disk, requestId)
  19. else:
  20. logger.info("""###############################################################################################
  21. CPU 使用率:{}, 内存使用:{}, SWAP内存使用率:{}, 服务磁盘使用率:{}
  22. ###############################################################################################""",
  23. cpu_use, cpu_mem, cpu_swap, cpu_disk)
  24. raise ServiceException(ExceptionType.NO_RESOURCES.value[0],
  25. ExceptionType.NO_RESOURCES.value[1])
  26. def print_cpu_ex_status(base_dir, requestId=None):
  27. result = False
  28. try:
  29. path = dirname(base_dir)
  30. cpu_use = psutil.cpu_percent()
  31. cpu_mem = psutil.virtual_memory().percent
  32. cpu_swap = psutil.swap_memory().percent
  33. cpu_disk = psutil.disk_usage(path).percent
  34. if float(cpu_use) > 70 or float(cpu_mem) > 70 or cpu_swap > 85 or cpu_disk > 90:
  35. result = True
  36. if requestId:
  37. logger.info("""###############################################################################################
  38. CPU 使用率:{}, 内存使用:{}, SWAP内存使用率:{}, 服务磁盘使用率:{}, requestId:{}
  39. ###############################################################################################""",
  40. cpu_use, cpu_mem, cpu_swap, cpu_disk, requestId)
  41. else:
  42. logger.info("""###############################################################################################
  43. CPU 使用率:{}, 内存使用:{}, SWAP内存使用率:{}, 服务磁盘使用率:{}
  44. ###############################################################################################""",
  45. cpu_use, cpu_mem, cpu_swap, cpu_disk)
  46. except Exception:
  47. logger.error("打印cpu状态异常: {}", format_exc())
  48. return result
  49. def print_cpu_status(base_dir, requestId=None):
  50. path = dirname(base_dir)
  51. cpu_use = psutil.cpu_percent()
  52. cpu_mem = psutil.virtual_memory().percent
  53. cpu_swap = psutil.swap_memory().percent
  54. cpu_lj = psutil.cpu_count() # 逻辑cpu个数
  55. cpu_wl = psutil.cpu_count(logical=False) # 物理cpu个数
  56. cpu_disk = psutil.disk_usage(path).percent
  57. if requestId:
  58. logger.info("""###############################################################################################
  59. CPU 使用率:{}, 内存使用:{}, SWAP内存使用率:{}, 逻辑个数:{}, 物理个数:{}, 服务磁盘使用率:{}, requestId:{}
  60. ###############################################################################################""", cpu_use,
  61. cpu_mem, cpu_swap, cpu_lj, cpu_wl, cpu_disk, requestId)
  62. else:
  63. logger.info("""###############################################################################################
  64. CPU 使用率:{}, 内存使用:{}, SWAP内存使用率:{}, 逻辑个数:{}, 物理个数:{}, 服务磁盘使用率:{}
  65. ###############################################################################################""", cpu_use,
  66. cpu_mem, cpu_swap, cpu_lj, cpu_wl, cpu_disk)