Spaces:
Sleeping
Sleeping
Commit ·
9429cb8
1
Parent(s): b482470
fix error.
Browse files- fast_app/fast_app.py +1 -0
- fast_app/systeminfo.py +176 -0
fast_app/fast_app.py
CHANGED
|
@@ -4,6 +4,7 @@ from pydantic import BaseModel
|
|
| 4 |
from typing import Optional
|
| 5 |
import time
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI(
|
| 9 |
title="FastAPI Service",
|
|
|
|
| 4 |
from typing import Optional
|
| 5 |
import time
|
| 6 |
import os
|
| 7 |
+
import sys
|
| 8 |
|
| 9 |
app = FastAPI(
|
| 10 |
title="FastAPI Service",
|
fast_app/systeminfo.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import psutil
|
| 3 |
+
import platform
|
| 4 |
+
import socket
|
| 5 |
+
import uuid
|
| 6 |
+
|
| 7 |
+
def get_cpu_info():
|
| 8 |
+
"""获取CPU信息"""
|
| 9 |
+
cpu_count_logical = psutil.cpu_count(logical=True)
|
| 10 |
+
cpu_count_physical = psutil.cpu_count(logical=False)
|
| 11 |
+
cpu_percent = psutil.cpu_percent(interval=1)
|
| 12 |
+
try:
|
| 13 |
+
import cpuinfo
|
| 14 |
+
brand = cpuinfo.get_cpu_info().get('brand_raw', 'Unknown')
|
| 15 |
+
except ImportError:
|
| 16 |
+
brand = platform.processor()
|
| 17 |
+
|
| 18 |
+
return {
|
| 19 |
+
"物理核心数": cpu_count_physical,
|
| 20 |
+
"逻辑核心数": cpu_count_logical,
|
| 21 |
+
"当前使用率": f"{cpu_percent}%",
|
| 22 |
+
"处理器型号": brand
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def get_memory_info():
|
| 26 |
+
"""获取内存信息"""
|
| 27 |
+
mem = psutil.virtual_memory()
|
| 28 |
+
swap = psutil.swap_memory()
|
| 29 |
+
return {
|
| 30 |
+
"总内存(GB)": round(mem.total / (1024**3), 2),
|
| 31 |
+
"已用内存(GB)": round(mem.used / (1024**3), 2),
|
| 32 |
+
"可用内存(GB)": round(mem.available / (1024**3), 2),
|
| 33 |
+
"内存使用率": f"{mem.percent}%",
|
| 34 |
+
"交换分区总大小(GB)": round(swap.total / (1024**3), 2),
|
| 35 |
+
"交换分区使用率": f"{swap.percent}%"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
def get_disk_info():
|
| 39 |
+
"""获取磁盘信息"""
|
| 40 |
+
partitions = psutil.disk_partitions()
|
| 41 |
+
disk_info = []
|
| 42 |
+
for partition in partitions:
|
| 43 |
+
try:
|
| 44 |
+
usage = psutil.disk_usage(partition.mountpoint)
|
| 45 |
+
disk_info.append({
|
| 46 |
+
"设备": partition.device,
|
| 47 |
+
"挂载点": partition.mountpoint,
|
| 48 |
+
"文件系统": partition.fstype,
|
| 49 |
+
"总大小(GB)": round(usage.total / (1024**3), 2),
|
| 50 |
+
"已用(GB)": round(usage.used / (1024**3), 2),
|
| 51 |
+
"空闲(GB)": round(usage.free / (1024**3), 2),
|
| 52 |
+
"使用率": f"{usage.percent}%"
|
| 53 |
+
})
|
| 54 |
+
except PermissionError:
|
| 55 |
+
continue
|
| 56 |
+
return disk_info
|
| 57 |
+
|
| 58 |
+
def get_gpu_info():
|
| 59 |
+
"""获取显卡信息"""
|
| 60 |
+
gpu_info = []
|
| 61 |
+
# 尝试使用 GPUtil (NVIDIA)
|
| 62 |
+
try:
|
| 63 |
+
import GPUtil
|
| 64 |
+
gpus = GPUtil.getGPUs()
|
| 65 |
+
for gpu in gpus:
|
| 66 |
+
gpu_info.append({
|
| 67 |
+
"ID": gpu.id,
|
| 68 |
+
"名称": gpu.name,
|
| 69 |
+
"显存总大小(MB)": gpu.memoryTotal,
|
| 70 |
+
"显存已用(MB)": gpu.memoryUsed,
|
| 71 |
+
"显存使用率": f"{gpu.memoryUtil*100}%",
|
| 72 |
+
"GPU使用率": f"{gpu.load*100}%",
|
| 73 |
+
"温度": f"{gpu.temperature}°C"
|
| 74 |
+
})
|
| 75 |
+
except ImportError:
|
| 76 |
+
pass
|
| 77 |
+
|
| 78 |
+
# 如果 GPUtil 不可用或未检测到,尝试 pynvml
|
| 79 |
+
if not gpu_info:
|
| 80 |
+
try:
|
| 81 |
+
import pynvml
|
| 82 |
+
pynvml.nvmlInit()
|
| 83 |
+
device_count = pynvml.nvmlDeviceGetCount()
|
| 84 |
+
for i in range(device_count):
|
| 85 |
+
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
|
| 86 |
+
name = pynvml.nvmlDeviceGetName(handle)
|
| 87 |
+
mem_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
|
| 88 |
+
temp = pynvml.nvmlDeviceGetTemperature(handle, 0)
|
| 89 |
+
utilization = pynvml.nvmlDeviceGetUtilizationRates(handle)
|
| 90 |
+
|
| 91 |
+
if isinstance(name, bytes):
|
| 92 |
+
name = name.decode('utf-8')
|
| 93 |
+
|
| 94 |
+
gpu_info.append({
|
| 95 |
+
"索引": i,
|
| 96 |
+
"名称": name,
|
| 97 |
+
"显存总大小(MB)": mem_info.total // (1024**2),
|
| 98 |
+
"显存已用(MB)": mem_info.used // (1024**2),
|
| 99 |
+
"GPU使用率": f"{utilization.gpu}%",
|
| 100 |
+
"显存使用率": f"{utilization.memory}%",
|
| 101 |
+
"温度": f"{temp}°C"
|
| 102 |
+
})
|
| 103 |
+
pynvml.nvmlShutdown()
|
| 104 |
+
except Exception:
|
| 105 |
+
pass
|
| 106 |
+
|
| 107 |
+
if not gpu_info:
|
| 108 |
+
return [{"提示": "未检测到NVIDIA显卡或未安装GPUtil/pynvml库"}]
|
| 109 |
+
|
| 110 |
+
return gpu_info
|
| 111 |
+
|
| 112 |
+
def get_network_info():
|
| 113 |
+
"""获取网络IP和MAC信息"""
|
| 114 |
+
net_info = {}
|
| 115 |
+
# 获取所有网络接口的地址
|
| 116 |
+
addrs = psutil.net_if_addrs()
|
| 117 |
+
for interface, addr_list in addrs.items():
|
| 118 |
+
ipv4 = None
|
| 119 |
+
mac = None
|
| 120 |
+
for addr in addr_list:
|
| 121 |
+
if addr.family == socket.AF_INET:
|
| 122 |
+
ipv4 = addr.address
|
| 123 |
+
elif addr.family == psutil.AF_LINK: # MAC address
|
| 124 |
+
mac = addr.address
|
| 125 |
+
|
| 126 |
+
if ipv4 or mac:
|
| 127 |
+
net_info[interface] = {
|
| 128 |
+
"IPv4": ipv4,
|
| 129 |
+
"MAC": mac
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
# 获取默认上网IP
|
| 133 |
+
try:
|
| 134 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 135 |
+
s.connect(("8.8.8.8", 80))
|
| 136 |
+
default_ip = s.getsockname()
|
| 137 |
+
s.close()
|
| 138 |
+
except Exception:
|
| 139 |
+
default_ip = "127.0.0.1"
|
| 140 |
+
|
| 141 |
+
return {
|
| 142 |
+
"默认上网IP": default_ip,
|
| 143 |
+
"所有接口详情": net_info
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
print("="*30 + " CPU信息 " + "="*30)
|
| 148 |
+
cpu_data = get_cpu_info()
|
| 149 |
+
for k, v in cpu_data.items():
|
| 150 |
+
print(f"{k}: {v}")
|
| 151 |
+
|
| 152 |
+
print("\n" + "="*30 + " 内存信息 " + "="*30)
|
| 153 |
+
mem_data = get_memory_info()
|
| 154 |
+
for k, v in mem_data.items():
|
| 155 |
+
print(f"{k}: {v}")
|
| 156 |
+
|
| 157 |
+
print("\n" + "="*30 + " 磁盘信息 " + "="*30)
|
| 158 |
+
disk_data = get_disk_info()
|
| 159 |
+
for disk in disk_data:
|
| 160 |
+
for k, v in disk.items():
|
| 161 |
+
print(f"{k}: {v}")
|
| 162 |
+
print("-" * 20)
|
| 163 |
+
|
| 164 |
+
print("\n" + "="*30 + " 显卡信息 " + "="*30)
|
| 165 |
+
gpu_data = get_gpu_info()
|
| 166 |
+
for gpu in gpu_data:
|
| 167 |
+
for k, v in gpu.items():
|
| 168 |
+
print(f"{k}: {v}")
|
| 169 |
+
print("-" * 20)
|
| 170 |
+
|
| 171 |
+
print("\n" + "="*30 + " 网络信息 " + "="*30)
|
| 172 |
+
net_data = get_network_info()
|
| 173 |
+
print(f"默认上网IP: {net_data['默认上网IP']}")
|
| 174 |
+
print("接口详情:")
|
| 175 |
+
for iface, info in net_data['所有接口详情'].items():
|
| 176 |
+
print(f" 接口: {iface}, IPv4: {info['IPv4']}, MAC: {info['MAC']}")
|