-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathioutil.py
executable file
·51 lines (43 loc) · 1.29 KB
/
ioutil.py
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
#! /usr/bin/python
import os
import random
import sys
import time
HZ = 100
def calc_ioutil(old, new, intv):
return ((new - old) / intv * HZ)/10
def read_disk_stat(dev):
disk_stat = os.path.join('/sys/block/', dev, 'stat')
with open(disk_stat, 'r') as df:
ds = df.readline()
return float(ds.split()[9])
def get_uptime():
with open('/proc/uptime', 'r') as uf:
ut = uf.readline()
return (float(ut.split()[0])*HZ + float(ut.split()[1])*HZ/100)
if __name__ == '__main__':
if len(sys.argv) < 3:
interval = 1.0 * 1000
dev = 'vda'
else:
interval = float(sys.argv[1]) * 1000
dev = sys.argv[2]
old_tot_ticks = 0
new_tot_ticks = 0
old_uptime = 0
new_uptime = 0
first_time = True
while True:
if first_time:
print 'first time, ignore output'
first_time = False
new_tot_ticks = read_disk_stat(dev)
new_uptime = get_uptime()
else:
old_tot_ticks = new_tot_ticks
new_tot_ticks = read_disk_stat(dev)
old_uptime = new_uptime
new_uptime = get_uptime()
intv = new_uptime - old_uptime
print '%util: '+str(calc_ioutil(old_tot_ticks, new_tot_ticks, 100))+'%'
time.sleep(interval/1000)