I would like to monitor system IO load from a python progr开发者_运维知识库am, 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)
What's wrong with just periodically reading /proc/diskstats
, e.g. using sched
to repeat the operation every minute or whatever? Linux's procfs
is nice exactly because it provides a textual way for the kernel to supply info to userland programs, as text is easiest to read and use in a huge variety of languages...!
I haven't seen a library, but you may want to check out the Python tool named "dstat" [1] for reading Linux kernel stats.
[1] - http://dag.wieers.com/home-made/dstat/
精彩评论