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.

70 lines
3.9KB

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