细化部分功能

This commit is contained in:
2025-03-13 17:35:03 +08:00
parent 76f4a5ecd9
commit 6daea23f0a
7 changed files with 36 additions and 34 deletions

View File

@ -10,29 +10,29 @@ def get_server_info():
# 1. 系统基本信息
info["system"] = {
"OS": platform.system(),
"OS版本": platform.version(),
"主机名": platform.node(),
"架构": platform.machine(),
"处理器型号": platform.processor(),
"启动时间": datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
"os": platform.system(),
"os_version": platform.version(),
"node": platform.node(),
"machine": platform.machine(),
"processor": platform.processor(),# 处理器型号
"boot_time": datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") # 启动时间
}
# 2. CPU 信息
cpu_usage = psutil.cpu_percent(interval=1)
info["cpu"] = {
"物理核心数": psutil.cpu_count(logical=False),
"逻辑核心数": psutil.cpu_count(logical=True),
"当前使用率 (%)": cpu_usage,
"每个核心使用率": psutil.cpu_percent(interval=1, percpu=True)
"cpu_count": psutil.cpu_count(logical=False),
"cpu_count_logical": psutil.cpu_count(logical=True),
"cpu_usage": cpu_usage,
"cpu_percent": psutil.cpu_percent(interval=1, percpu=True)
}
# 3. 内存信息
mem = psutil.virtual_memory()
info["memory"] = {
"总内存 (GB)": round(mem.total / (1024**3), 2),
"可用内存 (GB)": round(mem.available / (1024**3), 2),
"使用率 (%)": mem.percent
"total": round(mem.total / (1024**3), 2),
"available": round(mem.available / (1024**3), 2),
"percent": mem.percent
}
# 4. 磁盘信息
@ -40,20 +40,20 @@ def get_server_info():
for partition in psutil.disk_partitions():
usage = psutil.disk_usage(partition.mountpoint)
disks.append({
"设备": partition.device,
"挂载点": partition.mountpoint,
"文件系统": partition.fstype,
"总空间 (GB)": round(usage.total / (1024**3), 2),
"已用空间 (GB)": round(usage.used / (1024**3), 2),
"使用率 (%)": usage.percent
"device": partition.device,
"mountpoint": partition.mountpoint,
"fstype": partition.fstype,
"total": round(usage.total / (1024**3), 2),
"used": round(usage.used / (1024**3), 2),
"percent": usage.percent
})
info["disks"] = disks
# 5. 网络信息
net = psutil.net_io_counters()
info["network"] = {
"发送流量 (MB)": round(net.bytes_sent / (1024**2), 2),
"接收流量 (MB)": round(net.bytes_recv / (1024**2), 2)
"bytes_sent": round(net.bytes_sent / (1024**2), 2),
"bytes_recv": round(net.bytes_recv / (1024**2), 2)
}
# 6. 进程信息示例前5个高CPU进程
@ -64,10 +64,10 @@ def get_server_info():
if proc.info['cpu_percent'] > 0:
processes.append({
"PID": proc.info['pid'],
"进程名": proc.info['name'],
"CPU使用率 (%)": proc.info['cpu_percent']
"name": proc.info['name'],
"cpu_percent": proc.info['cpu_percent']
})
info["top_processes"] = sorted(processes, key=lambda x: x["CPU使用率 (%)"], reverse=True)
info["top_processes"] = sorted(processes, key=lambda x: x["cpu_percent"], reverse=True)
return info