Skip to content

Instantly share code, notes, and snippets.

@mmalone
Created July 14, 2011 00:10
Show Gist options
  • Save mmalone/1081615 to your computer and use it in GitHub Desktop.
Save mmalone/1081615 to your computer and use it in GitHub Desktop.
python diskstats parser
def diskstats():
file_path = '/proc/diskstats'
# http://lxr.osuosl.org/source/Documentation/iostats.txt
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
else:
continue
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 http://www.xaprb.com/blog/2010/01/09/how-linux-iostat-computes-its-results/ 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