Welcome, guest | Sign In | My Account | Store | Cart

Calculates IOPS for all disks in Linux/Unix system

Python, 53 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python

import os, time, math

run_tests = 3

devices = os.listdir('/sys/block/')
check_devices = []

reads = {}
writes = {}

for dev in devices:
	if dev.startswith('md') or dev.startswith('sd') or dev.startswith('hd'):
		check_devices.append(dev)
		reads[dev] = []
		writes[dev] = []

check_devices = sorted(check_devices)

for t in range(run_tests + 1):
	for dev in check_devices:
		file_data = open('/sys/block/%s/stat' % dev).readline().strip().split(' ')
		clean = []
		for num in file_data:
			if num != '':
				clean.append(int(num))
		
		reads[dev].append(clean[0])
		writes[dev].append(clean[4])
	
	time.sleep(1)

print "Device        Read        Write"
print "--------------------------------------"
for dev in check_devices:
	clean_reads = []
	reads[dev].reverse()
	for test, result in enumerate(reads[dev]):
		if test > 0:
			clean_reads.append(float(reads[dev][test - 1] - result))
	
	rops = int(math.ceil(sum(clean_reads) / len(clean_reads)))
	
	clean_writes = []
	writes[dev].reverse()
	for test, result in enumerate(writes[dev]):
		if test > 0:
			clean_writes.append(float(writes[dev][test - 1] - result))
	
	wops = int(math.ceil(sum(clean_writes) / len(clean_writes)))
	
	print "%s %s %s" % (dev.ljust(13), repr(rops).ljust(11), repr(wops))