forked from madmaze/pyNmonAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyNmonParser.py
142 lines (122 loc) · 3.79 KB
/
pyNmonParser.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
'''
Copyright (c) 2012-2013 Matthias Lee
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import logging as log
import datetime
class pyNmonParser:
fname = ""
outdir = ""
# Holds final 2D arrays of each stat
processedData = {}
# Holds System Info gathered by nmon
sysInfo=[]
bbbInfo=[]
# Holds timestamps for later lookup
tStamp={}
def __init__(self, fname="./test.nmon",outdir="./data/",overwrite=False,debug=False):
# TODO: check input vars or "die"
self.fname = fname
self.outdir = outdir
self.debug = debug
def outputCSV(self, stat):
outFile = open(os.path.join(self.outdir,stat+".csv"),"w")
line=""
if len(self.processedData[stat]) > 0:
# Iterate over each row
for n in range(len(self.processedData[stat][0])):
line=""
# Iterate over each column
for col in self.processedData[stat]:
if line == "":
# expecting first column to be date times
if n == 0:
# skip headings
line+=col[n]
else:
tstamp = datetime.datetime.strptime(col[n], "%d-%b-%Y %H:%M:%S")
line += tstamp.strftime("%Y-%m-%d %H:%M:%S")
else:
line+=","+col[n]
outFile.write(line+"\n")
def processLine(self,header,line):
if "AAA" in header:
# we are looking at the basic System Specs
self.sysInfo.append(line[1:])
elif "BBB" in header:
# more detailed System Spec
# do more grandular processing
# refer to pg 11 of analyzer handbook
self.bbbInfo.append(line)
elif "ZZZZ" in header:
self.tStamp[line[1]]=line[3]+" "+line[2]
else:
if line[0] in self.processedData.keys():
table=self.processedData[line[0]]
for n,col in enumerate(table):
# line[1] give you the T####
if n == 0 and line[n+1] in self.tStamp.keys():
# lookup the time stamp in tStamp
col.append(self.tStamp[line[n+1]])
elif n == 0 and line[n+1] not in self.tStamp.keys():
log.warn("Discarding line with missing Timestamp %s" % line)
break
else:
# TODO: do parsing(str2float) here
col.append(line[n+1])
# this should always be a float
#try:
# col.append(float(line[n+1]))
#except:
# print line[n+1]
# col.append(line[n+1])
else:
# new collumn, hoping these are headers
header=[]
for h in line[1:]:
# make it an array
tmp=[]
tmp.append(h)
header.append(tmp)
self.processedData[line[0]]=header
def parse(self):
# TODO: check fname
f = open(self.fname,"r")
rawdata = f.readlines()
for l in rawdata:
l=l.strip()
bits=l.split(',')
self.processLine(bits[0],bits)
return self.processedData
def output(self,outType="csv"):
if len(self.processedData) <= 0:
# nothing has been parsed yet
log.error("Output called before parsing")
exit()
# make output dir
self.outdir = os.path.join(self.outdir,outType)
if not (os.path.exists(self.outdir)):
try:
os.makedirs(self.outdir)
except:
log.error("Creating results dir:",self.outdir)
exit()
# switch for different output types
if outType.lower()=="csv":
# Write out to multiple CSV files
for l in self.processedData.keys():
self.outputCSV(l)
else:
log.error("Output type: %s has not been implemented." % (outType))
exit()