把原本散落在 /root/zikai 的 FastAPI 服务整理到 server/ 子目录, 开启独立 git 与 venv。运行时数据(config.yaml / keys / uploads) 按 .gitignore 留在工作目录但不入仓。
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""主机 CPU / 内存 / 磁盘信息采集(基于 psutil)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import platform
|
||
import time
|
||
|
||
import psutil
|
||
|
||
from ..schemas.system import DiskUsage, MemoryUsage, SystemStatus
|
||
|
||
# 跳过的伪文件系统
|
||
_IGNORED_FSTYPES = {"squashfs", "tmpfs", "devtmpfs", "overlay"}
|
||
|
||
|
||
class SystemService:
|
||
def get_status(self) -> SystemStatus:
|
||
vm = psutil.virtual_memory()
|
||
memory = MemoryUsage(
|
||
total=vm.total, available=vm.available, used=vm.used, percent=vm.percent,
|
||
)
|
||
|
||
disks: list[DiskUsage] = []
|
||
seen: set[str] = set()
|
||
for part in psutil.disk_partitions(all=False):
|
||
if part.fstype in _IGNORED_FSTYPES or part.mountpoint in seen:
|
||
continue
|
||
try:
|
||
u = psutil.disk_usage(part.mountpoint)
|
||
except (PermissionError, OSError):
|
||
continue
|
||
seen.add(part.mountpoint)
|
||
disks.append(DiskUsage(
|
||
device=part.device, mountpoint=part.mountpoint, fstype=part.fstype,
|
||
total=u.total, used=u.used, free=u.free, percent=u.percent,
|
||
))
|
||
|
||
return SystemStatus(
|
||
hostname=platform.node(),
|
||
cpu_percent=psutil.cpu_percent(interval=0.5),
|
||
cpu_count=psutil.cpu_count(logical=True) or 0,
|
||
memory=memory,
|
||
disks=disks,
|
||
uptime_seconds=max(0.0, time.time() - psutil.boot_time()),
|
||
)
|