Spaces:
Runtime error
Runtime error
File size: 13,406 Bytes
3f90381 | 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | """
Description : This file implements the Spell algorithm for log parsing
Author : LogPAI team
License : MIT
"""
import sys
# import re
import regex as re
import os
import numpy as np
import pandas as pd
import hashlib
from datetime import datetime
import string
from tqdm import tqdm
class LCSObject:
""" Class object to store a log group with the same template
"""
def __init__(self, logTemplate='', logIDL=[]):
self.logTemplate = logTemplate
self.logIDL = logIDL
class Node:
""" A node in prefix tree data structure
"""
def __init__(self, token='', templateNo=0):
self.logClust = None
self.token = token
self.templateNo = templateNo
self.childD = dict()
class LogParser:
""" LogParser class
Attributes
----------
path : the path of the input file
logName : the file name of the input file
savePath : the path of the output file
tau : how much percentage of tokens matched to merge a log message
"""
def __init__(self, indir='./', outdir='./result/', log_format=None, tau=0.5, rex=[], keep_para=True):
self.path = indir
self.logName = None
self.savePath = outdir
self.tau = tau
self.logformat = log_format
self.df_log = None
self.rex = rex
self.keep_para = keep_para
def LCS(self, seq1, seq2):
lengths = [[0 for j in range(len(seq2) + 1)] for i in range(len(seq1) + 1)]
# row 0 and column 0 are initialized to 0 already
for i in range(len(seq1)):
for j in range(len(seq2)):
if seq1[i] == seq2[j]:
lengths[i + 1][j + 1] = lengths[i][j] + 1
else:
lengths[i + 1][j + 1] = max(lengths[i + 1][j], lengths[i][j + 1])
# read the substring out from the matrix
result = []
lenOfSeq1, lenOfSeq2 = len(seq1), len(seq2)
while lenOfSeq1 != 0 and lenOfSeq2 != 0:
if lengths[lenOfSeq1][lenOfSeq2] == lengths[lenOfSeq1 - 1][lenOfSeq2]:
lenOfSeq1 -= 1
elif lengths[lenOfSeq1][lenOfSeq2] == lengths[lenOfSeq1][lenOfSeq2 - 1]:
lenOfSeq2 -= 1
else:
assert seq1[lenOfSeq1 - 1] == seq2[lenOfSeq2 - 1]
result.insert(0, seq1[lenOfSeq1 - 1])
lenOfSeq1 -= 1
lenOfSeq2 -= 1
return result
#for each seq, find the corresponding log key(template)
def SimpleLoopMatch(self, logClustL, seq):
for logClust in logClustL:
if float(len(logClust.logTemplate)) < 0.5 * len(seq):
continue
# Check the template is a subsequence of seq (we use set checking as a proxy here for speedup since
# incorrect-ordering bad cases rarely occur in logs)
token_set = set(seq)
if all(token in token_set or token == '<*>' for token in logClust.logTemplate):
return logClust
return None
def PrefixTreeMatch(self, parentn, seq, idx):
retLogClust = None
length = len(seq)
for i in range(idx, length):
if seq[i] in parentn.childD:
childn = parentn.childD[seq[i]]
if (childn.logClust is not None):
constLM = [w for w in childn.logClust.logTemplate if w != '<*>']
if float(len(constLM)) >= self.tau * length:
return childn.logClust
else:
return self.PrefixTreeMatch(childn, seq, i + 1)
return retLogClust
#for each seq, find the corresponding log template using LCS
def LCSMatch(self, logClustL, seq):
retLogClust = None
maxLen = -1
maxlcs = []
maxClust = None
set_seq = set(seq)
size_seq = len(seq)
for logClust in logClustL:
set_template = set(logClust.logTemplate)
if len(set_seq & set_template) < 0.5 * size_seq:
continue
lcs = self.LCS(seq, logClust.logTemplate)
if len(lcs) > maxLen or (len(lcs) == maxLen and len(logClust.logTemplate) < len(maxClust.logTemplate)):
maxLen = len(lcs)
maxlcs = lcs
maxClust = logClust
# LCS should be large then tau * len(itself)
if float(maxLen) >= self.tau * size_seq:
retLogClust = maxClust
return retLogClust
def getTemplate(self, lcs, seq):
retVal = []
if not lcs:
return retVal
lcs = lcs[::-1]
i = 0
for token in seq:
i += 1
if token == lcs[-1]:
retVal.append(token)
lcs.pop()
else:
retVal.append('<*>')
if not lcs:
break
if i < len(seq):
retVal.append('<*>')
return retVal
def addSeqToPrefixTree(self, rootn, newCluster):
parentn = rootn
seq = newCluster.logTemplate
seq = [w for w in seq if w != '<*>']
for i in range(len(seq)):
tokenInSeq = seq[i]
# Match
if tokenInSeq in parentn.childD:
parentn.childD[tokenInSeq].templateNo += 1
# Do not Match
else:
parentn.childD[tokenInSeq] = Node(token=tokenInSeq, templateNo=1)
parentn = parentn.childD[tokenInSeq]
if parentn.logClust is None:
parentn.logClust = newCluster
def removeSeqFromPrefixTree(self, rootn, newCluster):
parentn = rootn
seq = newCluster.logTemplate
seq = [w for w in seq if w != '<*>']
for tokenInSeq in seq:
if tokenInSeq in parentn.childD:
matchedNode = parentn.childD[tokenInSeq]
if matchedNode.templateNo == 1:
del parentn.childD[tokenInSeq]
break
else:
matchedNode.templateNo -= 1
parentn = matchedNode
def outputResult(self, logClustL):
print("output result", self.savePath)
templates = [0] * self.df_log.shape[0]
ids = [0] * self.df_log.shape[0]
df_event = []
for logclust in tqdm(logClustL):
template_str = ' '.join(logclust.logTemplate)
eid = hashlib.md5(template_str.encode('utf-8')).hexdigest()[0:8]
for logid in logclust.logIDL:
templates[logid - 1] = template_str
ids[logid - 1] = eid
df_event.append([eid, template_str, len(logclust.logIDL)])
df_event = pd.DataFrame(df_event, columns=['EventId', 'EventTemplate', 'Occurrences'])
self.df_log['EventId'] = ids
self.df_log['EventTemplate'] = templates
if self.keep_para:
self.df_log["ParameterList"] = self.df_log.apply(self.get_parameter_list, axis=1)
self.df_log.to_csv(os.path.join(self.savePath, self.logname + '_structured.csv'), index=False)
df_event.to_csv(os.path.join(self.savePath, self.logname + '_templates.csv'), index=False)
def printTree(self, node, dep):
pStr = ''
for i in range(len(dep)):
pStr += '\t'
if node.token == '':
pStr += 'Root'
else:
pStr += node.token
if node.logClust is not None:
pStr += '-->' + ' '.join(node.logClust.logTemplate)
print(pStr + ' (' + str(node.templateNo) + ')')
for child in node.childD:
self.printTree(node.childD[child], dep + 1)
def parse(self, logname):
starttime = datetime.now()
print('Parsing file: ' + os.path.join(self.path, logname))
self.logname = logname
self.load_data()
rootNode = Node()
logCluL = []
punc = re.sub('[<*>]', '', string.punctuation)
count = 0
for idx, line in self.df_log.iterrows():
logID = line['LineId']
logmessageL = list(filter(lambda x: x.strip() != '', re.split(f'[{punc}]', self.preprocess(line['Content']))))
constLogMessL = [w for w in logmessageL if w != '<*>']
#constLogMessL = [w for w in logmessageL]
# Find an existing matched log cluster
matchCluster = self.PrefixTreeMatch(rootNode, constLogMessL, 0)
if matchCluster is None:
matchCluster = self.SimpleLoopMatch(logCluL, constLogMessL)
if matchCluster is None:
matchCluster = self.LCSMatch(logCluL, logmessageL)
# Match no existing log cluster
if matchCluster is None:
newCluster = LCSObject(logTemplate=logmessageL, logIDL=[logID])
logCluL.append(newCluster)
self.addSeqToPrefixTree(rootNode, newCluster)
# Add the new log message to the existing cluster
else:
newTemplate = self.getTemplate(self.LCS(logmessageL, matchCluster.logTemplate),
matchCluster.logTemplate)
if ' '.join(newTemplate) != ' '.join(matchCluster.logTemplate):
self.removeSeqFromPrefixTree(rootNode, matchCluster)
matchCluster.logTemplate = newTemplate
self.addSeqToPrefixTree(rootNode, matchCluster)
if matchCluster:
matchCluster.logIDL.append(logID)
count += 1
if count % 1000 == 0 or count == len(self.df_log):
print('Processed {0:.1f}% of log lines.'.format(count * 100.0 / len(self.df_log)), end='\r')
if not os.path.exists(self.savePath):
os.makedirs(self.savePath)
self.outputResult(logCluL)
print('Parsing done. [Time taken: {!s}]'.format(datetime.now() - starttime))
def load_data(self):
headers, regex = self.generate_logformat_regex(self.logformat)
self.df_log = self.log_to_dataframe(os.path.join(self.path, self.logname), regex, headers, self.logformat)
def preprocess(self, line):
for currentRex in self.rex:
line = re.sub(currentRex, '<*>', line)
return line
def log_to_dataframe(self, log_file, regex, headers, logformat):
""" Function to transform log file to dataframe
"""
log_messages = []
linecount = 0
k = 0
with open(log_file, 'r') as fin:
for line in fin.readlines():
#extract small size data
k += 1
if k%10000 == 0:
print("extracted {0} log lines from {1}".format(k, log_file))
line = re.sub(r'[^\x00-\x7F]+', '<NASCII>', line) #replace non ASCII (\x00-\x7F) character with <NASCII>
try:
match = regex.search(line.strip())
message = [match.group(header) for header in headers]
log_messages.append(message)
linecount += 1
except Exception as e:
pass
logdf = pd.DataFrame(log_messages, columns=headers)
logdf.insert(0, 'LineId', None)
logdf['LineId'] = [i + 1 for i in range(linecount)]
return logdf
def generate_logformat_regex(self, logformat):
""" Function to generate regular expression to split log messages
"""
headers = []
splitters = re.split(r'(<[^<>]+>)', logformat)
regex = ''
for k in range(len(splitters)):
if k % 2 == 0:
splitter = re.sub(' +', '\\\s+', splitters[k]) #re.sub(' +', '\s+', splitters[k])
regex += splitter
else:
header = splitters[k].strip('<').strip('>')
regex += '(?P<%s>.*?)' % header
headers.append(header)
regex = re.compile('^' + regex + '$')
return headers, regex
def get_parameter_list(self, row):
template_regex = re.sub(r"\s<.{1,5}>\s", "<*>", row["EventTemplate"])
if "<*>" not in template_regex: return []
template_regex = re.sub(r'([^A-Za-z0-9])', r'\\\1', template_regex)
template_regex = re.sub(r'\\ +', r'[^A-Za-z0-9]+', template_regex)
template_regex = "^" + template_regex.replace("\<\*\>", "(.*?)") + "$"
parameter_list = re.findall(template_regex, row["Content"])
parameter_list = parameter_list[0] if parameter_list else ()
parameter_list = list(parameter_list) if isinstance(parameter_list, tuple) else [parameter_list]
parameter_list = [para.strip(string.punctuation).strip(' ') for para in parameter_list]
return parameter_list
if __name__ == "__main__":
import os
import pandas as pd
print(os.getcwd())
os.chdir("../")
print(os.getcwd())
lp = LogParser()
# print(lp.LCS(seq1="abcbb", seq2="bc"))
output_dir = 'demo/Spell_result/' # The output directory of parsing results
log_file = 'HDFS.log' # The input log file name
log_structured_file = output_dir + log_file + "_structured.csv"
df = pd.read_csv(log_structured_file)
for _, row in df.iterrows():
lp.get_parameter_list(row)
|