在Linux上,有许多系统命令可以始终监视系统的运行状态,如ps、top和free。 要检索这些系统信息,Python将从subprocess模块中调用以检索结果。 但是这样做很麻烦。 特别是需要写很多分析代码。
在Python上获取系统信息的另一个好方法是使用名为psutil的第三方模块。 苹果月,PS util=processandsystemutilities。 不仅可以通过一两行代码实现系统监控,还可以在跨平台上使用,支持Linux/UNIX/OSX/Windows等,是系统管理员和运输伙伴不可缺少的必备模块。
安装psutil
pip安装PS util获取CPU信息
我们先获取CPU的信息吧。
psutil.cpu_count(#CPU逻辑数为PSutil.CPU_count ) logical=false ) # CPU物理核心是
CPU用户/系统/空闲时间统计信息:
获取内存信息
使用psutil获取物理内存和交换内存信息,分别使用:
in [3] : PS util.virtual _ memory (out [3] : SV mem (total=4148170752,available=1042911232,percent=74.9 )
可以从psutil中获取磁盘分区、磁盘利用率和磁盘I/o信息。
in [5] : PS util.disk _ partitions (out [5] : [ s diskpart ] (设备=' c : (),mount point=' c 3360 s diskpart ) fstype='NTFS ',opts='rw,fixed ' ),sdiskpart fstype='NTFS ',opts='rw,fixed ' ),s diskpart (设备=' f 330 )
psutil可以获取网络接口和网络连接信息。
in [6]获取: PS util.net _ io _ counters (#网络读写字节/数据包的数量In [7]: psutil.net_if_addrs ) #网络网络网络
psutil.net_connections (
获取进程信息
psutil.pids (
psutil还提供了用于模拟ps命令效果的test (函数。