MCPcopy Create free account
hub / github.com/WraySmith/log-anomaly / parse

Method parse

parse/logparser/Spell/Spell.py:227–273  ·  view source on GitHub ↗
(self, logname)

Source from the content-addressed store, hash-verified

225
226
227 def parse(self, logname):
228 starttime = datetime.now()
229 print('Parsing file: ' + os.path.join(self.path, logname))
230 self.logname = logname
231 self.load_data()
232 rootNode = Node()
233 logCluL = []
234
235 count = 0
236 for idx, line in self.df_log.iterrows():
237 logID = line['LineId']
238 logmessageL = list(filter(lambda x: x != '', re.split(r'[\s=:,]', self.preprocess(line['Content']))))
239 constLogMessL = [w for w in logmessageL if w != '<*>']
240
241 #Find an existing matched log cluster
242 matchCluster = self.PrefixTreeMatch(rootNode, constLogMessL, 0)
243
244 if matchCluster is None:
245 matchCluster = self.SimpleLoopMatch(logCluL, constLogMessL)
246
247 if matchCluster is None:
248 matchCluster = self.LCSMatch(logCluL, logmessageL)
249
250 # Match no existing log cluster
251 if matchCluster is None:
252 newCluster = LCSObject(logTemplate=logmessageL, logIDL=[logID])
253 logCluL.append(newCluster)
254 self.addSeqToPrefixTree(rootNode, newCluster)
255 #Add the new log message to the existing cluster
256 else:
257 newTemplate = self.getTemplate(self.LCS(logmessageL, matchCluster.logTemplate),
258 matchCluster.logTemplate)
259 if ' '.join(newTemplate) != ' '.join(matchCluster.logTemplate):
260 self.removeSeqFromPrefixTree(rootNode, matchCluster)
261 matchCluster.logTemplate = newTemplate
262 self.addSeqToPrefixTree(rootNode, matchCluster)
263 if matchCluster:
264 matchCluster.logIDL.append(logID)
265 count += 1
266 if count % 1000 == 0 or count == len(self.df_log):
267 print('Processed {0:.1f}% of log lines.'.format(count * 100.0 / len(self.df_log)))
268
269 if not os.path.exists(self.savePath):
270 os.makedirs(self.savePath)
271
272 self.outputResult(logCluL)
273 print('Parsing done. [Time taken: {!s}]'.format(datetime.now() - starttime))
274
275 def load_data(self):
276 headers, regex = self.generate_logformat_regex(self.logformat)

Callers

nothing calls this directly

Calls 12

load_dataMethod · 0.95
preprocessMethod · 0.95
PrefixTreeMatchMethod · 0.95
SimpleLoopMatchMethod · 0.95
LCSMatchMethod · 0.95
addSeqToPrefixTreeMethod · 0.95
getTemplateMethod · 0.95
LCSMethod · 0.95
outputResultMethod · 0.95
LCSObjectClass · 0.85
NodeClass · 0.70

Tested by

no test coverage detected