Python——psutil的使用(获取系统性能信息)

时间:2021-10-05 02:00:25
>>> import psutil                                 #导入psutil
>>> a=psutil.virtual_memory()
>>> a.total #总虚拟内存
1023934464
>>> a.used #已用虚拟内存
214511616
>>> a.free #可用虚拟内存
427778048
>>> psutil.cpu_times() #cpu时间信息汇总
scputimes(user=18.26, nice=0.0, system=22.63, idle=5729.08, iowait=18.59, irq=0.0, softirq=0.46, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times().user #cpu用户使用时间
18.27
>>> psutil.cpu_count() #cpu个数
1
>>> psutil.swap_memory() #查看交换内存
sswap(total=2147479552, used=0, free=2147479552, percent=0.0, sin=0, sout=0)
>>> psutil.virtual_memory() #查看虚拟内存
svmem(total=1023934464, available=656711680, percent=35.9, used=214511616, free=427778048, active=314855424, inactive=142458880, buffers=954368, cached=380690432, shared=7376896)
>>> psutil.disk_partitions() #查看分区情况
[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota')]
>>> psutil.disk_usage('/') #查看根目录使用情况
sdiskusage(total=19001245696, used=4089483264, free=14911762432, percent=21.5)
>>> psutil.disk_io_counters() #磁盘I/O个数
sdiskio(read_count=8307, write_count=4357, read_bytes=309452288, write_bytes=90153984, read_time=76537, write_time=9010, read_merged_count=14, write_merged_count=344, busy_time=30724)
>>> psutil.net_io_counters() #网络流量信息
snetio(bytes_sent=464928, bytes_recv=10280356, packets_sent=5316, packets_recv=9885, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.users() #用户信息
[suser(name='root', terminal='tty1', host='', started=1499413632.0), suser(name='root', terminal='pts/0', host='192.168.2.1', started=1499413760.0), suser(name='root', terminal='pts/1', host='192.168.2.1', started=1499414784.0)]

>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")     #获取开机时间,转换为易读的格式
'2018-02-21 19:22:32'
>>>