阅读量:4
psutil 是一个跨平台的 Python 库,用于获取系统使用情况和性能指标。以下是 psutil 可以监测的一些常见硬件指标:
- CPU 使用率(CPU usage):获取系统或特定进程的 CPU 使用情况,以百分比表示。
import psutil
cpu_usage = psutil.cpu_percent()
print(f"CPU usage: {cpu_usage}%")
- 内存使用情况(Memory usage):获取系统的内存使用情况,包括总内存、已使用内存、空闲内存等。
import psutil
memory_info = psutil.virtual_memory()
print(f"Total memory: {memory_info.total} bytes")
print(f"Used memory: {memory_info.used} bytes")
print(f"Free memory: {memory_info.free} bytes")
- 磁盘使用情况(Disk usage):获取系统的磁盘空间使用情况,包括总磁盘空间、已使用磁盘空间、空闲磁盘空间等。
import psutil
disk_usage = psutil.disk_usage('/')
print(f"Total disk space: {disk_usage.total} bytes")
print(f"Used disk space: {disk_usage.used} bytes")
print(f"Free disk space: {disk_usage.free} bytes")
- 网络接口使用情况(Network interface usage):获取系统的网络接口使用情况,包括发送和接收的字节数、数据包数等。
import psutil
net_io_counters = psutil.net_io_counters()
print(f"Bytes sent: {net_io_counters.bytes_sent} bytes")
print(f"Bytes received: {net_io_counters.bytes_recv} bytes")
- CPU 温度(CPU temperature):获取系统的 CPU 温度(仅适用于支持硬件监控芯片的设备)。
import psutil
try:
cpu_temps = psutil.sensors_temperatures()
for name, entries in cpu_temps.items():
for entry in entries:
print(f"{name}: {entry.current}°C")
except NotImplementedError:
print("CPU temperature monitoring is not supported on this platform.")
- 其他硬件指标:
psutil还支持其他硬件指标,如风扇速度、功耗等,但需要根据具体硬件和操作系统进行支持。
请注意,某些硬件指标的可用性可能因操作系统和设备而异。在使用 psutil 时,请确保已正确安装并导入库,并根据需要调整代码以适应不同的平台和硬件。
以上就是关于“psutil python能监测哪些硬件指标”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm