-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor-iotop.py
executable file
·62 lines (52 loc) · 1.94 KB
/
monitor-iotop.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
54
55
56
57
58
59
60
61
#!/usr/bin/python
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
from utils import Monitor,loginit,now,mkdir
from collections import namedtuple
from dataclasses import dataclass
flog=loginit('/mnt/ram/log/monitor-iotop.log','w')
def log(msg) :
print(msg,file=flog,flush=True)
print(msg)
interests = ['brave', 'chrome', 'chromium','firefox','vivaldi']
writes={'brave':0, 'chrome':0, 'chromium':0,'firefox':0,'vivaldi':0}
@dataclass
class Event:
cmd : str
numbytes : float
ixwrite = 5
ixcmd = 11
def addwrite(process,amount) :
if process not in writes :
writes[process] = 0
writes[process] += float(amount)
def parse(event):
record=event.split()
return Event(record[ixcmd], float(record[ixwrite]))
log(f'starting read')
monitor = Monitor('sudo iotop -b')
while event := monitor.read() :
#
#iotop prints 'unavailable' to replace two columns if
# https://www.suse.com/support/kb/doc/?id=000020888
# and we're going to split the line expecting two fields
#
event=event.replace('unavailable','unavail able')
if any(interest in event.lower() for interest in interests):
try:
record=parse(event)
if record.numbytes > 0.00 :
log(f'{now(":")} {record.cmd} wrote {record.numbytes}')
addwrite(record.cmd, record.numbytes)
log(f'total {record.cmd} : {writes[record.cmd]}')
log('')
except Exception as e :
log(f'Exception {e} on {event}')
log('done')