-
Notifications
You must be signed in to change notification settings - Fork 1
/
sarparse2.py
120 lines (89 loc) · 3.13 KB
/
sarparse2.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Parse various 'sar' output files
"""
import pandas
def parse_cpu(fp):
line = fp.readline()
assert line.startswith('Linux ')
line = fp.readline()
assert not line.strip()
header = fp.readline()
assert ' CPU ' in header
x = []
while 1:
line = fp.readline()
if line.startswith('Average:'): # end line
break
if not line:
assert 0, "unexpected end of file"
tup = line.strip().split()
timestr, cpu, puser, pnice, psystem, piowait, psteal, pidle = tup
timestr = list(map(int, timestr.split(':')))
timesec = timestr[0]*3600 + timestr[1]*60 + timestr[2]
puser, pnice, psystem, piowait, psteal, pidle = map(float, tup[2:])
d = {}
d['timesec'] = timesec
d['cpu'] = cpu
d['puser'] = puser
d['pnice'] = pnice
d['psystem'] = psystem
d['piowait'] = piowait
d['psteal'] = psteal
d['pidle'] = pidle
x.append(d)
return pandas.DataFrame(x)
def parse_disk(fp, device='nvme1n1'):
line = fp.readline()
assert line.startswith('Linux ')
line = fp.readline()
assert not line.strip()
header = fp.readline()
assert ' DEV ' in header
x = []
while 1:
line = fp.readline()
if line.startswith('Average:'): # end line
break
if not line:
assert 0, "unexpected end of file"
tup = line.strip().split()
if not tup:
continue
this_device = tup[1]
if this_device != device: # skip reporting on other stuff
continue
timestr = tup[0]
timestr = list(map(int, timestr.split(':')))
timesec = timestr[0]*3600 + timestr[1]*60 + timestr[2]
tps, read_kbps, write_kbps, d_kbps, areq_sz, aqu_sz, aawait, putil = map(float, tup[2:])
d = {}
d['timesec'] = timesec
for key in 'tps, read_kbps, write_kbps, d_kbps, areq_sz, aqu_sz, aawait, putil'.split(', '):
d[key] = locals()[key]
x.append(d)
return pandas.DataFrame(x)
def parse_ram(fp):
line = fp.readline()
assert line.startswith('Linux ')
line = fp.readline()
assert not line.strip()
header = fp.readline()
assert ' kbmemfree ' in header
x = []
while 1:
line = fp.readline()
if line.startswith('Average:'): # end line
break
if not line:
assert 0, "unexpected end of file"
tup = line.strip().split()
timestr, kbmemfree, kbavail, kbmemused, pmemused, kbbuffers, kbcached, kbcommit, pcommit, kbactive, kbinact, kbdirty = tup
timestr = list(map(int, timestr.split(':')))
timesec = timestr[0]*3600 + timestr[1]*60 + timestr[2]
kbmemfree, kbavail, kbmemused, pmemused, kbbuffers, kbcached, kbcommit, pcommit, kbactive, kbinact, kbdirty = map(float, tup[1:])
d = {}
d['timesec'] = timesec
for key in 'kbmemfree, kbavail, kbmemused, pmemused, kbbuffers, kbcached, kbcommit, pcommit, kbactive, kbinact, kbdirty'.split(', '):
d[key] = locals()[key]
x.append(d)
return pandas.DataFrame(x)