I would like to monitor system IO load from a python program, accessing statistics similar to those provided in /proc/diskstats
in linux (although obviously a cross-platform library would be great). Is there an existing python library that I could use to query disk IO statistics on linux?
In case anyone else is trying to parse /proc/diskstats with Python like Alex suggested:
def diskstats_parse(dev=None):
file_path = '/proc/diskstats'
result = {}
# ref: http://lxr.osuosl.org/source/Documentation/iostats.txt
columns_disk = ['m', 'mm', 'dev', 'reads', 'rd_mrg', 'rd_sectors',
'ms_reading', 'writes', 'wr_mrg', 'wr_sectors',
'ms_writing', 'cur_ios', 'ms_doing_io', 'ms_weighted']
columns_partition = ['m', 'mm', 'dev', 'reads', 'rd_sectors', 'writes', 'wr_sectors']
lines = open(file_path, 'r').readlines()
for line in lines:
if line == '': continue
split = line.split()
if len(split) == len(columns_disk):
columns = columns_disk
elif len(split) == len(columns_partition):
columns = columns_partition
else:
# No match
continue
data = dict(zip(columns, split))
if dev != None and dev != data['dev']:
continue
for key in data:
if key != 'dev':
data[key] = int(data[key])
result[data['dev']] = data
return result
PSUtil provides a number of disk and fs stats and is also cross-platform.
You should look at psutil.disk_io_counters(perdisk=True)
which returns a number of useful metrics:
read_count: number of reads write_count: number of writes read_bytes: number of bytes read write_bytes: number of bytes written read_time: time spent reading from disk (in milliseconds) write_time: time spent writing to disk (in milliseconds)
These metrics come from /proc/diskstats
(on Linux)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With