Skip to content

Instantly share code, notes, and snippets.

Created July 14, 2011 00:10
What would you like to do?
python diskstats parser
def diskstats():
file_path = '/proc/diskstats'
columns_disk = ['major_dev_num', 'minor_dev_num', 'device', 'reads', 'reads_merged', 'sectors_read', 'ms_reading', 'writes', 'writes_merged', 'sectors_written', 'ms_writing', 'current_ios', 'ms_doing_io', 'weighted_ms_doing_io']
columns_partition = ['major_dev_num', 'minor_dev_num', 'device', 'reads', 'sectors_read', 'writes', 'sectors_written']
result = {}
for line in (l for l in open(file_path, 'r').xreadlines() if l != ''):
parts = line.split()
if len(parts) == len(columns_disk):
columns = columns_disk
elif len(parts) == len(columns_partition):
columns = columns_partition
data = dict(zip(columns_disk, parts))
result[data['device']] = dict((k, int(v)) for k, v in data.iteritems() if k != 'device')
return result
if __name__ == '__main__':
for device, stats in diskstats().iteritems():
print device
for name, stat in stats.iteritems():
print ' %s: %s' % (name.replace('_', ' '), stat)
# See for how to make the output more iostats-y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment