你好,我是李逸皓,我的梦想是:运维,永不背锅!
放个链接,万一有人关注呢
优质文章推荐
↓ ↓ ↓ ↓ ↓
pip install psutil
import psutil
cpu_percent = psutil.cpu_percent(interval=1)
print(f"CPU使用率: {cpu_percent}%")
cpu_count = psutil.cpu_count(logical=True) # 获取逻辑核心数
print(f"CPU逻辑核心数: {cpu_count}")
memory_info = psutil.virtual_memory()
print(f"总内存: {memory_info.total / (1024 ** 3):.2f} GB")
print(f"已用内存: {memory_info.used / (1024 ** 3):.2f} GB")
print(f"可用内存: {memory_info.available / (1024 ** 3):.2f} GB")
print(f"内存使用率: {memory_info.percent}%")
swap_info = psutil.swap_memory()
print(f"交换内存总量: {swap_info.total / (1024 ** 3):.2f} GB")
print(f"已用交换内存: {swap_info.used / (1024 ** 3):.2f} GB")
print(f"交换内存使用率: {swap_info.percent}%")
disk_usage = psutil.disk_usage('/')
print(f"磁盘总容量: {disk_usage.total / (1024 ** 3):.2f} GB")
print(f"已用磁盘: {disk_usage.used / (1024 ** 3):.2f} GB")
print(f"剩余磁盘: {disk_usage.free / (1024 ** 3):.2f} GB")
print(f"磁盘使用率: {disk_usage.percent}%")
partitions = psutil.disk_partitions()
for partition in partitions:
print(f"设备: {partition.device}")
print(f"挂载点: {partition.mountpoint}")
print(f"文件系统类型: {partition.fstype}")
net_io = psutil.net_io_counters()
print(f"发送字节数: {net_io.bytes_sent}")
print(f"接收字节数: {net_io.bytes_recv}")
connections = psutil.net_connections(kind='inet')
for conn in connections:
print(f"协议: {conn.type}, 本地地址: {conn.laddr}, 远程地址: {conn.raddr}")
process_list = psutil.pids()
print(f"当前运行的进程ID列表: {process_list}")
p = psutil.Process(1234) # 假设进程ID为1234
print(f"进程名: {p.name()}")
print(f"进程状态: {p.status()}")
print(f"进程CPU时间: {p.cpu_times()}")
print(f"进程内存信息: {p.memory_info()}")
p.terminate()
print(f"进程{p.pid} 已终止")
单击进入:粉丝进群传送门
↓↓↓ 点个在看,无需赞赏!