一个很好的python脚本,用来监控linux的I/O情况。
#!/usr/bin/python
# Monitoring per-process disk I/O activity
import sys, os, time, signal, re
class DiskIO:
def __init__(self, pname=None, pid=None, reads=0, writes=0):
self.pname = pname
self.pid = pid
self.reads = 0
self.writes = 0
def main():
argc = len(sys.argv)
if argc != 1:
print "usage: ./iotop"
sys.exit(0)
if os.getuid() != 0:
print "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"
while True:
os.system('dmesg -c > /tmp/diskio.log')
l = []
f = open('/tmp/diskio.log', 'r')
line = f.readline()
while line:
m = re.match(\
'^(\S+)\((\d+)\): (READ|WRITE) block (\d+) on (\S+)', line)
if m != None:
if not l:
l.append(DiskIO(m.group(1), m.group(2)))
line = f.readline()
continue
found = False
for item in l:
if item.pid == m.group(2):
found = True
if m.group(3) == "READ":
item.reads = item.reads + 1
elif m.group(3) == "WRITE":
item.writes = item.writes + 1
if not found:
l.append(DiskIO(m.group(1), m.group(2)))
line = f.readline()
time.sleep(1)
for item in l:
print "%-10s %10s %10d %10d" % \
(item.pname, item.pid, item.reads, item.writes)
def signal_handler(signal, frame):
os.system('echo 0 > /proc/sys/vm/block_dump')
sys.exit(0)
if __name__=="__main__":
main()
--使用方法
[root@node1 pub]# python iomonitor.py
TASK PID READ WRITE
kjournald 1358 0 55
ocssd.bin 6796 0 0
oracle 8160 1 0
ologgerd 7617 0 61
kjournald 3943 0 183
ocssd.bin 6790 0 0
oracle 8200 0 0
ocssd.bin 6793 0 0
oracle 8196 0 1
oracle 8204 0 1
oracle 8200 0 6
oracle 8160 1 0
oracle 9886 3 0
ocssd.bin 6796 0 0
ocssd.bin 6790 0 0
ocssd.bin 6793 0 0
oracle 8200 0 2
oracle 8160 1 0
.....
--可以使用LINUX自带的命令查询IO使用状况,但不能看到每个进程的IO使用情况
[root@node1 pub]# iostat -x -k 1 10
Linux 2.6.18-274.el5 (node1) 2013年07月18日
avg-cpu: %user %nice %system %iowait %steal %idle
0.54 0.00 0.12 0.66 0.00 98.68
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
sda 0.03 26.72 0.08 14.80 2.26 166.10 22.62 0.98 65.75 3.06 4.55
sda1 0.00 0.00 0.00 0.00 0.64 0.00 570.61 0.00 9.49 2.01 0.00
sda2 0.01 23.93 0.04 13.67 0.86 150.40 22.07 0.87 63.51 2.91 3.98
sda3 0.00 0.00 0.00 0.00 0.00 0.00 32.17 0.00 9.57 6.15 0.00
sda4 0.00 0.00 0.00 0.00 0.00 0.00 1.80 0.00 8.20 8.20 0.00
sda5 0.02 2.79 0.04 1.13 0.76 15.70 28.00 0.11 92.03 11.02 1.30
sdb 3.21 0.46 6.25 11.17 619.09 164.16 89.96 0.10 5.81 1.78 3.10
sdb1 0.00 0.00 0.01 1.00 0.88 0.50 2.75 0.00 0.95 0.94 0.09
sdb2 3.21 0.13 6.24 10.01 618.20 107.41 89.34 0.09 5.75 1.86 3.02
sdb3 0.00 0.33 0.00 0.16 0.01 56.24 672.08 0.01 41.39 13.16 0.22
sdc 3.21 0.46 5.32 11.51 604.66 172.85 92.39 0.08 4.59 1.76 2.95
sdc1 0.00 0.00 0.01 1.00 1.05 0.50 3.08 0.00 0.93 0.92 0.09
sdc2 3.21 0.14 5.31 10.36 603.61 116.13 91.89 0.07 4.58 1.83 2.87
sdc3 0.00 0.33 0.00 0.16 0.00 56.22 695.35 0.00 28.43 11.45 0.19
sdd 3.21 0.46 5.02 11.42 599.98 167.34 93.37 0.05 3.09 1.62 2.66