Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python library for monitoring /proc/diskstats?

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?

like image 299
kdt Avatar asked Jul 25 '10 12:07

kdt


2 Answers

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
like image 120
Borgenk Avatar answered Nov 15 '22 04:11

Borgenk


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)

like image 38
Matt Avatar answered Nov 15 '22 05:11

Matt