forked from panhuafan/spell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell.py
359 lines (301 loc) · 12.5 KB
/
spell.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
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
358
359
"""
Description : This file implements the Spell algorithm for log parsing
Author : LogPAI team
License : MIT
"""
import re
import os
import sys
import numpy as np
import pandas as pd
import hashlib
import json
from datetime import datetime
from collections import defaultdict
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 Spell:
""" 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='', log_format=None, tau=0.5, rex=[], abstraction_json_file=''):
self.path = indir
self.logName = None
self.savePath = outdir
self.tau = tau
self.logformat = log_format
self.df_log = None
self.rex = rex
self.abstractions = {}
self.abstraction_json_file = abstraction_json_file
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
def SimpleLoopMatch(self, logClustL, seq):
retLogClust = None
for logClust in logClustL:
if float(len(logClust.logTemplate)) < 0.5 * len(seq):
continue
# If the template is a subsequence of seq
it = iter(seq)
if all(token in seq or token == '*' for token in logClust.logTemplate):
return logClust
return retLogClust
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
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):
templates = [0] * self.df_log.shape[0]
ids = [0] * self.df_log.shape[0]
df_event = []
abstraction_temp = defaultdict(list)
for logclust in logClustL:
template_str = ' '.join(logclust.logTemplate)
eid = hashlib.md5(template_str.encode('utf-8')).hexdigest()[0:8]
zerobased_ids = []
for logid in logclust.logIDL:
templates[logid - 1] = template_str
ids[logid - 1] = eid
zerobased_ids.append(logid - 1)
df_event.append([eid, template_str, len(logclust.logIDL)])
abstraction_temp[template_str].extend(zerobased_ids)
# get abstraction
abstraction_id = 0
for abstraction_str, log_ids in abstraction_temp.iteritems():
self.abstractions[abstraction_id] = {
'abstraction': abstraction_str,
'log_id': log_ids
}
abstraction_id += 1
# write abstraction to json
with open(self.abstraction_json_file, 'w') as outfile:
json.dump(self.abstractions, outfile)
df_event = pd.DataFrame(df_event, columns=['EventId', 'EventTemplate', 'Occurrences'])
self.df_log['EventId'] = ids
self.df_log['EventTemplate'] = templates
# 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(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 = []
count = 0
for idx, line in self.df_log.iterrows():
logID = line['LineId']
logmessageL = filter(lambda x: x != '', re.split(r'[\s=:,]', self.preprocess(line['Content'])))
constLogMessL = [w for w in logmessageL if w != '*']
# 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)))
# 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
with open(log_file, 'r') as fin:
for line in fin.readlines():
line = re.sub(r'[^\x00-\x7F]+', '<NASCII>', line)
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])
regex += splitter
else:
header = splitters[k].strip('<').strip('>')
regex += '(?P<%s>.*?)' % header
headers.append(header)
regex = re.compile('^' + regex + '$')
return headers, regex
if __name__ == '__main__':
if len(sys.argv) == 4:
input_dir = sys.argv[1]
log_file = sys.argv[2]
abstraction_jsonfile = sys.argv[3]
else:
input_dir = '/home/hudan/Git/pylogabstract/results/iplom/casper-rw/message/' # The input directory of log file
log_file = 'auth.log' # The input log file name
abstraction_jsonfile = 'auth.log.json'
output_dir = '' # The output directory of parsing results
log_format = '<Content>'
tau = 0.5 # Message type threshold (default: 0.5)
regex = [] # Regular expression list for optional preprocessing (default: [])
parser = Spell(indir=input_dir, outdir=output_dir, log_format=log_format, tau=tau, rex=regex,
abstraction_json_file=abstraction_jsonfile)
parser.parse(log_file)