-
Notifications
You must be signed in to change notification settings - Fork 0
/
discstat.py
executable file
·53 lines (49 loc) · 1.27 KB
/
discstat.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
52
53
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time
# the fields in discstats are:
# majId
# minId
# dev
# rMerged
# secRead
# milsecRead
# wrCompl
# wMerged
# secWrite
# milsecWrite
# IOprocess
# IOsec
# IOWsec
# Initialization
DISCKSTATS = '/proc/diskstats'
old = {}
new = {}
with open(DISCKSTATS, 'r', encoding='utf8') as f:
for line in f:
parts = line.split()
dev = parts[2]
new[dev] = [float(i) for i in parts[3:]]
# Running
while True:
time.sleep(2)
with open(DISCKSTATS, 'r', encoding='utf8') as f:
for line in f:
parts = line.split()
dev = parts[2]
old[dev], new[dev] = new[dev], [float(i) for i in parts[3:]]
if new[dev][5] != old[dev][5]:
READ = '{(512 * (new[dev][4]-old[dev][4]) / (new[dev][5]-old[dev][5])):5.0f}'
else:
if new[dev][4] == old[dev][4]:
READ = ' 0'
else:
READ = ' ∞'
if new[dev][9] != old[dev][9]:
WRITE = '{(512 * (new[dev][8]-old[dev][8]) / (new[dev][9]-old[dev][9])):5.0f}'
else:
if new[dev][8] == old[dev][8]:
WRITE = ' 0'
else:
WRITE = ' ∞'
print(f'{dev:6s}: r/w: {READ}/{WRITE} [b/s]')