monitoring/utils/mem.py

68 lines
2.2 KiB
Python

# _*_ coding: utf-8 _*_
# @Time :2022/5/29 14:54
# @Email :508737091@qq.com
# @Author :qiangyanwen
# @File :mem.py.py
import psutil
import datetime
import time
async def get_sys_info():
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
system_start_info = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
users_count = len(psutil.users())
users_name = ",".join([u.name for u in psutil.users()])
# sys = SysModel(count=users_count, name=users_name, current_time=now_time, start_time=system_start_info)
sys = dict(count=users_count,name=users_name, current_time=now_time, start_time=system_start_info)
return sys
async def get_cpu_info():
cpu_percent = round((psutil.cpu_percent(1)), 2)
cpu = dict(cpu_count=psutil.cpu_count(logical=False), log_cpu=psutil.cpu_count(), cpu_percent=cpu_percent)
return cpu
async def ge_mem_info():
info = psutil.virtual_memory()
total = float(round(info.total,2) / 1024 / 1024 / 1024)
available = float(round(info.available / 1024 / 1024 / 1024,2))
percent = float(round(info.percent,2))
used = float(round(info.used / 1024 / 1024 / 1024,2))
free = float(round(info.free / 1024 / 1024 / 1024,2))
mem_info = {
"total": total,
"available": available,
"percent": percent,
"used": used,
"free": free}
return mem_info
async def get_disk_info():
disk_info = psutil.disk_partitions()
disk_list = []
for disk in disk_info:
content = psutil.disk_usage(disk.mountpoint)
device = disk.device
total = float(round(content.total) / 1024 / 1024 / 1024)
used = float(round(content.used) / 1024 / 1024 / 1024)
percent = float(round(content.percent) / 1024 / 1024 / 1024)
free = float(round(content.free) / 1024 / 1024 / 1024)
fs_type = disk.fstype
opts = disk.opts
info_dict = {
"device": device,
"total": "%.2f" % total,
"used": "%.2f" % used,
"percent": "%.2f" % percent,
"fstype": fs_type,
"opts": opts,
"free": "%.2f" % free
}
disk_list.append(info_dict)
return disk_list