I've GPL'd a simple io monitor I recently wrote in Python. Enjoy.

###############################################################################
#
#    iomon.py - a simple server io monitor for Linux, in Python, by Paul Reiber
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You may have received a copy of the GNU General Public License
#    along with this program.  If not, see .
#
###############################################################################
#
#    for every running process 'PID' on the server, iomon reads values from the
#    pseudo-file /proc/PID/io and emits statistics if it notes any differences
#    from what it saw last time it checked.
#
#    example usage, with 5 second polling interval:
#
#        sudo python ./iomon.py 5
#
###############################################################################

import sys
import time
from glob import glob

saved_values = {}

def print_deltas():

    deltas = {}

    process_directories = glob('/proc/[0-9]*/')

    for pid in saved_values:
        if not '/proc/'+pid+'/' in process_directories:
            saved_values[pid]={}

    for directory in process_directories:

        pid = directory.split('/')[2]

        if not pid in saved_values:
            saved_values[pid] = {}

        with open( directory + '/io' , "r") as inputfile:
            for line in inputfile:

                (key,value) = line.rstrip('\n').split(': ')

                if not key in saved_values[pid]:
                    saved_values[pid][key] = 0

                deltas[key] = int(value) - saved_values[pid][key]
                saved_values[pid][key] = int(value)

        inputfile.close()

        if ((deltas['rchar']!=0) or (deltas['wchar']!=0) or 
            (deltas['syscr']!=0) or (deltas['syscw']!=0)):
            print('pid:{} rchar:{} wchar:{} cr:{} cw:{}'.format( pid,
                deltas['rchar'], deltas['wchar'],
                deltas['syscr'], deltas['syscw']))


def iomon(sleep_seconds):
    while(True):
        print_deltas()
        time.sleep(sleep_seconds)

if __name__ == "__main__":
    iomon(60 if len(sys.argv) == 1 else int(sys.argv[1]))
Topic revision: r2 - 2016.09.28 - PaulReiber
 
Copyright © is by author. All material on this collaboration platform is the property of its contributing author.