自动化运维之psutil模块~获取系统性能信息

版权声明:如需转载,请注明出处! https://blog.csdn.net/ouyangzhenxin/article/details/81941291

一、CPU信息

>>> import psutil  #导入psutil模块
>>> psutil.cpu_times()  #查看CPU的完整信息
scputimes(user=262.23, nice=0.27, system=170.81, idle=153135.17, iowait=50.04, irq=0.0, softirq=0.08, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times(percpu=False)  # 查看CPU完整信息的另一种方式
scputimes(user=262.24, nice=0.27, system=170.83, idle=153151.74, iowait=50.04, irq=0.0, softirq=0.08, steal=0.0, guest=0.0, guest_nice=0.0)
>>> psutil.cpu_times().user  # 查看单类信息,例如用户占用的CPU时间比
262.3
>>> psutil.cpu_count()  # 查看CPU的逻辑个数
1
>>> psutil.cpu_count(logical=False)  # 查看CPU的物理个数
1
>>> 

二、内存信息

total(内存总数)

used(已使用的内存数)

free(空闲内存数)

buffers(缓冲使用数)

cache(缓存使用数)

swap(交换分区使用数)

>>> import psutil  # 导入psutil模块
>>> psutil.virtual_memory()  # 查看内存整体信息
svmem(total=1040551936, available=701288448, percent=32.6, used=180355072, free=452206592, active=275603456, inactive=224522240, buffers=60026880, cached=347963392, shared=352256, slab=65908736)
>>> jier = psutil.virtual_memory()  # 将内存信息赋值给一个变量
>>> jier.total  # 查看内存总数是多少
1040551936
>>> jier.free  # 查看内存空余数
452206592
>>> 

三、磁盘信息

read_count(读IO数)

write_count(写IO数)

扫描二维码关注公众号,回复: 3681933 查看本文章

read_bytes(IO读字节数)

write_bytes(IO写字节数)

read_time(磁盘读时间)

write_time(磁盘写时间)

>>> import psutil  # 导入模块
>>> psutil.disk_partitions()  # 查看完整磁盘信息
[sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext4', opts='rw,relatime,data=ordered')]
>>> psutil.disk_usage('/')  # 查看根分区的磁盘使用信息
sdiskusage(total=42139451392, used=3381141504, free=36594155520, percent=8.5)
>>> psutil.disk_usage('/run/')  # 查看run分区的磁盘使用信息
sdiskusage(total=520273920, used=352256, free=519921664, percent=0.1)
>>> psutil.disk_io_counters()  # 查看磁盘的IO读写情况
sdiskio(read_count=16014, write_count=37632, read_bytes=329921536, write_bytes=278237184, read_time=146130, write_time=148078, read_merged_count=13, write_merged_count=16619, busy_time=55554)
>>> psutil.disk_io_counters(perdisk=True)  # 获取单个分区的IO个数和读写信息
{'vda': sdiskio(read_count=16014, write_count=37632, read_bytes=329921536, write_bytes=278237184, read_time=146130, write_time=148078, read_merged_count=13, write_merged_count=16619, busy_time=55554), 'vda1': sdiskio(read_count=15930, write_count=36897, read_bytes=327808000, write_bytes=278237184, read_time=146088, write_time=147968, read_merged_count=13, write_merged_count=16619, busy_time=55428)}
>>> 

四、网络信息

bytes_sent(发送字节数)

bytes_recv=28220119(接收字节数)

packets_sent=200978(发送数据包数)

packets_recv=212672(接收数据包数)

>>> import psutil  # 导入模块
>>> psutil.net_io_counters()  # 查看整体网络IO信息流量
snetio(bytes_sent=9978681, bytes_recv=5290561, packets_sent=37611, packets_recv=44983, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True)  # 查看单独接口IO信息流量
{'lo': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=9982647, bytes_recv=5294811, packets_sent=37641, packets_recv=45036, errin=0, errout=0, dropin=0, dropout=0)}
>>> 

五、其它系统信息

>>> import psutil  # 导入模块
>>> import datetime  # 导入时间模块
>>> psutil.users()  # 获取登陆用户信息
[suser(name='root', terminal='pts/0', host='124.205.208.206', started=1534906496.0, pid=22583), suser(name='root', terminal='pts/1', host='124.205.208.206', started=1534907136.0, pid=22739)]
>>> psutil.boot_time()  # 获取系统启动时间信息
1534755312.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %G:%M:%S")  # 系统启动时间信息栅格化
'2018-08-20 2018:55:12'
>>> 

猜你喜欢

转载自blog.csdn.net/ouyangzhenxin/article/details/81941291
今日推荐