1)CPU信息
[root@localhost ~]# python
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
Type "help", "copyright", "credits" or "license" for more information.
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
>>> import psutil
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
#使用cpu_times方法获取CPU完整信息
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
>>> psutil.cpu_times()
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
scputimes(user=14571.83, nice=10.13, system=10459.3, idle=9390763.99, iowait=716.03, irq=0.0, softirq=3.8, steal=0.0, guest=0.0, guest_nice=0.0)
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
#获取单项数据信息,如用户user的时间比
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
>>> psutil.cpu_times().user
文章源自小柒网-https://www.yangxingzhen.cn/1614.html
>>> psutil.cpu_count(logical=False)
#使用psutil.virtual_memory方法获取内存完整信息
>>> mem = psutil.virtual_memory()
svmem(total=1928695808L, available=1699635200L, percent=11.9, used=1617731584L, free=310964224L, active=733216768, inactive=700944384, buffers=194965504L, cached=1193705472)
sswap(total=0L, used=0L, free=0L, percent=0.0, sin=0, sout=0)
#使用psutil.disk_partitions方法获取磁盘完整信息
[sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered')]
#使用psutil.disk_usage方法获取分区(参数)的使用情况
sdiskusage(total=42139451392, used=3013500928, free=36961796096, percent=7.2)
#使用psutil.disk_io_counters方法获取磁盘总的IO个数、读写信息
sdiskio(read_count=26141, write_count=1716089, read_bytes=474932224, write_bytes=13267562496, read_time=109538, write_time=15169214)
#"perdisk=True"参数获取单个分区IO个数、读写信息
>>> psutil.disk_io_counters(perdisk=True)
{'vda1': sdiskio(read_count=26141, write_count=1716089, read_bytes=474932224, write_bytes=13267562496, read_time=109538, write_time=15169214)}
#使用psutil.net_io_counters获取网络总的IO信息,默认pernic=False
snetio(bytes_sent=287203066, bytes_recv=887595622, packets_sent=2422790, packets_recv=2704748, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)
{'lo': snetio(bytes_sent=502615, bytes_recv=502615, packets_sent=2200, packets_recv=2200, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=286703163, bytes_recv=887096509, packets_sent=2420612, packets_recv=2702590, errin=0, errout=0, dropin=0, dropout=0)}
#使用psutil.users方法返回当前登录系统的用户信息
[suser(name='root', terminal='tty1', host='', started=1536040960.0), suser(name='root', terminal='pts/1', host='14.155.220.101', started=1542177536.0), suser(name='root', terminal='pts/2', host='14.155.220.101', started=1542181760.0)]
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!
广东省深圳市南山区 电信 1F
亲测,写的不错,感谢博主