利用psutil获取磁盘信息

获取磁盘完整信息

[root@bogon python_auto]# vim disk.py

!/usr/bin/env python

import psutil

disk = psutil.disk_partitions() #利用psutil模块的disk_partitions()方法

print disk

执行查看结果

[root@bogon python_auto]# python disk.py
[sdiskpart(device='/dev/sda2', mountpoint='/', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext4', opts='rw')]

获取分区的使用情况

[root@bogon python_auto]# vim disk.py

!/usr/bin/env python

import psutil

disk = psutil.disk_partitions()

partition = psutil.disk_usage('/')
print partition

执行查看结果

[root@bogon python_auto]# python disk.py
sdiskusage(total=18779398144, used=3674472448, free=14150983680, percent=19.600000000000001)
[root@bogon python_auto]#

获取磁盘总的IO个数、读写信息

[root@bogon python_auto]# vim disk.py

!/usr/bin/env python

import psutil

disk = psutil.disk_partitions()

partition = psutil.disk_usage('/')

print partition

io = psutil.disk_io_counters()
print io

执行查看结果

[root@bogon python_auto]# python disk.py
sdiskio(read_count=245556, write_count=118038, read_bytes=5248733184, write_bytes=3704962048, read_time=1424144, write_time=3252612)
[root@bogon python_auto]#

获取单个分区io个数、读写信息。

[root@bogon python_auto]# vim disk.py

!/usr/bin/env python

import psutil

disk = psutil.disk_partitions()

partition = psutil.disk_usage('/')

print partition

io = psutil.disk_io_counters()

print io

perdisk = psutil.disk_io_counters(perdisk=True)
print perdisk

执行查看结果
[root@bogon python_auto]# python disk.py
{'sda2': sdiskio(read_count=243109, write_count=117669, read_bytes=5193733120, write_bytes=3656708096, read_time=1409641, write_time=3249864), 'sda3': sdiskio(read_count=1223, write_count=466, read_bytes=10375168, write_bytes=49188864, read_time=10872, write_time=2831), 'sda1': sdiskio(read_count=1224, write_count=29, read_bytes=44624896, write_bytes=48128, read_time=3631, write_time=83)}
[root@bogon python_auto]#

可以看到磁盘每个分区的详细信息都有了。

你可能感兴趣的:(利用psutil获取磁盘信息)