我想从python程序监视系统IO负载,访问类似于linux中/ proc / diskstats中提供的统计信息(尽管显然跨平台库会很棒).是否有一个现有的python库可以用来查询linux上的磁盘IO统计信息?
最佳答案
万一其他人试图用Python解析/ proc / diskstats像Alex建议:
原文链接:https://www.f2er.com/linux/440648.htmldef 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','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