#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v2.6#Author: [email protected]#monitoring per-process disk I/O activity ImportSYSImportOSImport TimeImportSignalImportReclassDiskio:def __init__(Self, Pname=none, Pid=none, Reads=0, writes=0): Self.pname=pname self.pid=PID Self.reads=0 self.writes=0defMain (): ARGC=Len (SYS.ARGV)ifARGC! = 1: Print "Usage:please Run this script like [./diskio.py]"sys.exit (0)ifOs.getuid ()! =0:Print "Error:this script must be run as root"sys.exit (0) signal.signal (signal. SIGINT, Signal_handler) Os.system ('echo 1 >/proc/sys/vm/block_dump') Print "TASK PID READ WRITE" whileTrue:os.system ('dmesg-c >/tmp/diskio.log') L=[] f= Open ('/tmp/diskio.log','R') Line=F.readline () whileline:m=Re.match ('^ (\s+) \ ((\d+) \): (read| WRITE) block (\d+) on (\s+)', line)ifM! =None:if notl:l.append (Diskio (M.group (1), M.group (2 )) line=F.readline ()Continuefound=False forIteminchL:ifItem.pid = = M.group (2): Found=TrueifM.group (3) = ="READ": Item.reads= item.reads + 1elifM.group (3) = ="WRITE": Item.writes= item.writes + 1if notfound:l.append (Diskio (M.group (1), M.group (2 )) line=f.readline () time.sleep (1) forIteminchL:Print "%-10s%10s%10d%10d"%(Item.pname, Item.pid, Item.reads, item.writes)defSignal_handler (signal, frame): Os.system ('echo 0 >/proc/sys/vm/block_dump') sys.exit (0)if __name__=="__main__": Main ()
Output Result:
[Email protected] python]#.diskio.pytask PID READ writedmesg3018 0 0DMESG3170 0 0DMESG3172 0 0jbd2/sda6-8 the 0 3DMESG3174 0 0DMESG3177 0 0DMESG3179 0 0jbd2/sda3-8 1040 0 4jbd2/sda3-8 1040 0 0DMESG3181 0 0DMESG3183 0 0jbd2/sda6-8 the 0 2
"Python Ops script" to view disk IO read and write by each process