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