Files
gitlab-instance-0a899031_d2…/web2_system_metrics.py
DESKTOP-77LRNCU\dsx2016 ae9531f2c2 网络页焕新:IP 画像/质量检测、链路诊断与功能开关
新增 /ip_profile、/ip_quality API 与网络 UI 组件,默认隐藏摄像头/拉流导航;同步更新 README。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 12:47:53 +08:00

101 lines
2.6 KiB
Python

"""
本机硬件与系统信息(供「系统」页轮询)。
"""
from __future__ import annotations
import platform
import time
from typing import Any
import psutil
from web2_host_caps import gpu_status_payload, machine_summary_payload
def _cpu_freq() -> dict[str, float | None] | None:
try:
f = psutil.cpu_freq()
if not f:
return None
return {
"current_mhz": round(f.current, 1) if f.current else None,
"max_mhz": round(f.max, 1) if f.max else None,
}
except Exception:
return None
def snapshot() -> dict[str, Any]:
try:
psutil.cpu_percent(interval=None)
cpu = psutil.cpu_percent(interval=0.15)
except Exception:
cpu = None
try:
vm = psutil.virtual_memory()
mem = {
"percent": vm.percent,
"used": vm.used,
"total": vm.total,
"available": vm.available,
}
except Exception:
mem = {}
try:
sm = psutil.swap_memory()
swap = {"percent": sm.percent, "used": sm.used, "total": sm.total}
except Exception:
swap = {}
disks: list[dict[str, Any]] = []
try:
for part in psutil.disk_partitions(all=False):
try:
usage = psutil.disk_usage(part.mountpoint)
disks.append(
{
"device": part.device,
"mountpoint": part.mountpoint,
"fstype": part.fstype,
"percent": usage.percent,
"free": usage.free,
"total": usage.total,
}
)
except (PermissionError, OSError):
continue
except Exception:
pass
boot: int | None = None
try:
boot = int(psutil.boot_time())
except Exception:
pass
machine = machine_summary_payload()
processor = (platform.processor() or "").strip() or None
return {
"ts": time.time(),
"system": {
**machine,
"os_version": platform.version(),
"processor": processor,
"boot_time_unix": boot,
},
"hardware": {
"cpu_logical": psutil.cpu_count(logical=True),
"cpu_physical": psutil.cpu_count(logical=False),
"cpu_freq": _cpu_freq(),
"memory_total": mem.get("total"),
"gpu": gpu_status_payload(),
},
"cpu_percent": cpu,
"memory": mem,
"swap": swap,
"disks": disks,
}