安装环境
pip install psutil pynvml terminaltables -i https://pypi.tuna.tsinghua.edu.cn/simple
import psutil
import pynvml
from terminaltables import AsciiTable
class OutOfMemory(Exception):
def __init__(self, err='内存使用率过高'):
Exception.__init__(self, err)
class OutOfGpu(Exception):
def __init__(self, err='显存使用率过高'):
Exception.__init__(self, err)
class OutOfDisk(Exception):
def __init__(self, err='硬盘使用率过高'):
Exception.__init__(self, err)
class OutOfCpu(Exception):
def __init__(self, err='cpu使用率过高'):
Exception.__init__(self, err)
class Monitor():
# 设置相关阈值,超过即提示
THRESH_CPU_PERCENT = 10
THRESH_MEM_USED = 5
THRESH_MEM_PERCENT = 10
THRESH_GPU_USED = 5
THRESH_GPU_PERCENT = 10
THRESH_DISK_USED = 1
THRESH_DISK_PERCENT = 10
# cpu使用信息
def cpu_info(self):
cpu_count = psutil.cpu_count() # cpu核数
cpu_percent = psutil.cpu_percent(1) # cpu已使用率
info = {
'code': 200,
'msg': '',
'percent': cpu_percent,
'count': cpu_count,
'total': 100,
'free': 100 - cpu_percent,
'used': cpu_percent,
'device': 'CPU'
}
info_txt = {
'code': 200,
'msg': '',
'percent': f'{cpu_percent}%',
'count': cpu_count,
'total': '{}%'.format(100),
'free': '{}%'.format(100 - cpu_percent),
'used': f'{cpu_percent}%',
'device': 'CPU'
}
return info, info_txt
# 内存信息
def mem_info(self):
info = psutil.virtual_memory()
mem_total = round(info.total / 1024 ** 3, 1) # 总内存大小(GB)
mem_used = round(info.used / 1024 ** 3, 1) # 已使用内存(GB)
mem_free = round(info.available / 1024 ** 3, 1) # 剩余内存(GB)
mem_percent = info.percent # 内存使用率
info = {
'code': 200,
'msg': '',
'mused': mem_used,
'free': mem_free,
'total': mem_total,
'percent': mem_percent,
'device': '内存'
}
info_txt = {
'code': 200,
'msg': '',
'used': f'{mem_used}GB',
'free': f'{mem_free}GB',
'total': f'{mem_total}GB',
'percent': f'{mem_percent}%',
'device': '内存'
}
return info, info_txt
# GPU信息
def gpu_info(self):
try:
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0) # 0表示显卡标号,即第一块GPU
meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
gpu_total = round(meminfo.total / 1024 ** 3, 1) # 总显存大小(GB)
gpu_used = round(meminfo.used / 1024 ** 3, 1) # 已使用显存大小(GB)
gpu_free = round(meminfo.free / 1024 ** 3, 1) # 剩余显存大小(GB)
gpu_percent = round(gpu_used / gpu_total * 100, 1) # 已使用显存百分比
info = {
'code': 200,
'msg': '',
'total': gpu_total,
'used': gpu_used,
'free': gpu_free,
'percent': gpu_percent,
'device': '显卡'
}
info_txt = {
'code': 200,
'msg': '',
'total': f'{gpu_total}GB',
'used': f'{gpu_used}GB',
'free': f'{gpu_free}GB',
'percent': f'{gpu_percent}%',
'device': '显卡'
}
return info, info_txt
except:
info = {
'code': 500,
'msg': '未检测到显卡',
'total': '/',
'used': '/',
'free': '/',
'percent': '/',
'device': '显卡'
}
return info, info
# 硬盘信息
def disk_info(self):
try:
disk = psutil.disk_usage(r'c:') # 指定盘符
except:
disk = psutil.disk_usage(r'/')
dist_total = round(disk[0] / 1024 ** 3, 1) # 总磁盘大小(GB)
disk_used = round(disk[1] / 1024 ** 3, 1) # 已使用大小(GB)
disk_free = round(disk[2] / 1024 ** 3, 1) # 剩余大小(GB)
disk_percent = disk[3]
info = {'total': dist_total,
'used': disk_used,
'free': disk_free,
'percent': disk_percent,
'device': '硬盘',
'code': 200,
'msg': ''}
info_txt = {'total': f'{dist_total}GB',
'used': f'{disk_used}GB',
'free': f'{disk_free}GB',
'percent': f'{disk_percent}%',
'device': '硬盘',
'code': 200,
'msg': ''}
return info, info_txt
def get_device_info(self):
'''
获取设备信息
:return:
'''
cpu_info, cpu_info_txt = self.cpu_info()
mem_info, mem_info_txt = self.mem_info()
gpu_info, gpu_info_txt = self.gpu_info()
disk_info, disk_info_txt = self.disk_info()
info = {
'cpu': cpu_info,
'mem': mem_info,
'gpy': gpu_info,
'disk': disk_info
}
return info
def main(self):
exceptions_list = []
cpu_info, cpu_info_txt = self.cpu_info()
mem_info, mem_info_txt = self.mem_info()
gpu_info, gpu_info_txt = self.gpu_info()
disk_info, disk_info_txt = self.disk_info()
if cpu_info['code'] == 200:
if cpu_info['percent'] > Monitor.THRESH_CPU_PERCENT:
exceptions_list.append(OutOfCpu)
cpu_info_txt['msg'] = 'cpu使用率超过{}%'.format(Monitor.THRESH_CPU_PERCENT)
if mem_info['code'] == 200:
if mem_info['percent'] > Monitor.THRESH_MEM_PERCENT:
exceptions_list.append(OutOfMemory)
mem_info_txt['msg'] = '内存使用率超过{}%'.format(Monitor.THRESH_MEM_PERCENT)
if disk_info['code'] == 200:
if disk_info['percent'] > Monitor.THRESH_DISK_PERCENT:
exceptions_list.append(OutOfDisk)
disk_info_txt['msg'] = '硬盘使用率超过{}%'.format(Monitor.THRESH_DISK_PERCENT)
if gpu_info['code'] == 200:
if gpu_info['percent'] > Monitor.THRESH_GPU_PERCENT:
exceptions_list.append(OutOfGpu)
gpu_info_txt['msg'] = '显存使用率超过{}%'.format(Monitor.THRESH_GPU_PERCENT)
cpu_info_txt['thresh'] = '{}%'.format(Monitor.THRESH_CPU_PERCENT)
mem_info_txt['thresh'] = '{}%'.format(Monitor.THRESH_MEM_PERCENT)
gpu_info_txt['thresh'] = '{}%'.format(Monitor.THRESH_GPU_PERCENT)
disk_info_txt['thresh'] = '{}%'.format(Monitor.THRESH_DISK_PERCENT)
metrics_info_list = [cpu_info_txt, mem_info_txt, gpu_info_txt, disk_info_txt]
metric_str = self._gen_str_table(metrics_info_list)
print(metric_str)
return metric_str
def _gen_str_table(self, metrics_info_list, other_info_list=None, metric_table=None):
metric_str = '\n'
if other_info_list:
metric_str = '\n'.join([str(i) for i in other_info_list]) + '\n'
if not metric_table:
metric_table = [["设备名称", '总容量', '已使用', '剩余', '使用率', '预警值', '预警信息']]
metric_list = ['device', 'total', 'used', 'free', 'percent', 'thresh', 'msg']
for info in metrics_info_list:
row_metrics = ['{}'.format(info.get(key, None))[-12:] for key in metric_list]
metric_table += [row_metrics]
metric_str += AsciiTable(metric_table).table
return metric_str
if __name__ == '__main__':
monitor = Monitor()
monitor.main()
运行结果:
+----------+--------+--------+--------+--------+--------+-------------------+
| 设备名称 | 总容量 | 已使用 | 剩余 | 使用率 | 预警值 | 预警信息 |
+----------+--------+--------+--------+--------+--------+-------------------+
| CPU | 100% | 27.3% | 72.7% | 27.3% | 10% | cpu使用率超过10% |
| 内存 | 11.9GB | 7.4GB | 4.5GB | 62.3% | 10% | 内存使用率超过10% |
| 显卡 | 2.0GB | 0.0GB | 2.0GB | 0.0% | 10% | |
| 硬盘 | 78.5GB | 63.5GB | 15.0GB | 80.9% | 10% | 硬盘使用率超过10% |
+----------+--------+--------+--------+--------+--------+-------------------+