forked from petergreen86/tldr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtldr.py
439 lines (405 loc) · 17.1 KB
/
tldr.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#
# Author: Alex Sullivan, Peter Green
# Version: 1.2
# Usage:
# python3 tldr.py --log /path/to/blackduck_bds.zip
# python3 tldr.py --log /path/to/blackduck_bds
# Optional properties:
# --size SMALL/STANDARD
# --string "StringToSearch"
# --scanid "ID to search/analyze"
# --skip-summary true
# --fuzzy-search "String to fuzzy search"
# --keywords "comma,delimited,list,of,words,to,search,for"
import sys
import argparse
from zipfile import ZipFile
import zipfile
import os
import re
from glob import glob
from shutil import copyfile
from pathlib import Path
import datetime
from fuzzywuzzy import fuzz
parser = argparse.ArgumentParser("Parse anomalies from BD logs")
parser.add_argument("--log", dest="logPath", required=True, help="Path to log directory or zip")
parser.add_argument("--size", dest='pkgSize', required=False, help="Set which size logs to examine")
parser.add_argument("--string", dest='string', required=False, help="Set string for string searching")
parser.add_argument("--scanid", dest='scanid', required=False, help="Set string for scanID searching")
parser.add_argument("--skip-summary", dest='skipsum', required=False, help="Set to skip log summary")
parser.add_argument("--fuzzy-search", dest='fuzz', required=False,
help="Set string to fuzzy search against log package")
parser.add_argument("--keywords", dest='keywords', required=False,
help="Quote wrapped comma delimited list of keywords to search against log package.")
parser.add_argument("--isCoverity", dest='isCoverity', required=False,
help="If set to TRUE, will disable Black Duck specific features like ")
args = parser.parse_args()
if str(args.isCoverity).upper() == "TRUE":
isCoverity = True
else:
isCoverity = False
logPath = str(args.logPath)
logName = os.path.basename(os.path.splitext(logPath)[0])
pkgSize = args.pkgSize
stringPattern = str(args.string)
targetScanId = str(args.scanid)
skipSum = str(args.skipsum)
fuzzString = str(args.fuzz)
keywords = str(args.keywords)
api_pattern = r"limit=[3-9]\d{2,}"
pattern = "WARN|ERROR|^\tat |Exception|^Caused by: |\t... \d+ more"
grepString = re.compile(pattern)
cwd = os.getcwd()
logDir = (cwd + "/" + logName)
currentTime = datetime.datetime.now()
timestamp = currentTime.timestamp()
tldrDir = (cwd + "/tldr-" + logName + "-" + str(int(timestamp)))
def printError():
print("Error: " + str(sys.exc_info()[0]))
if os.path.isdir(logPath):
logDir = logPath
if not os.path.exists(logDir):
try:
os.mkdir(logDir)
except IOError:
printError()
if not os.path.exists(tldrDir):
os.mkdir(tldrDir)
def banner():
print("----------------------------------")
print(" _____ _ ____________ ")
print("|_ _| | | _ \ ___ \ ")
print(" | | | | | | | | |_/ / ")
print(" | | | | | | | | / ")
print(" | | | |___| |/ /| |\ \ ")
print(" \_/ \_____/___/ \_| \_| ")
print("----------------------------------")
print("Black Duck / Coverity Log Summary Tool")
print("----------------------------------")
def unpack(size):
sizestr = str(size)
if os.path.isdir(logPath):
try:
if sizestr.upper() == 'SMALL':
print("Unpacking small.zip")
if os.path.isfile(logDir + "/small.zip"):
with ZipFile(logDir + "/small.zip", 'r') as logs:
logs.extractall(logDir)
elif sizestr.upper() == 'STANDARD':
print("Unpacking standard.zip")
print("WARNING: parsing standard.zip may take longer than expected.")
if os.path.isfile(logDir + "/standard.zip"):
with ZipFile(logDir + "/standard.zip", 'r') as logs:
logs.extractall(logDir)
else:
print('No size configured. Defaulting to small')
if os.path.isfile(logDir + "/small.zip"):
with ZipFile(logDir + "/small.zip", 'r') as logs:
logs.extractall(logDir)
except IOError:
printError()
if zipfile.is_zipfile(logPath):
try:
with ZipFile(args.logPath, 'r') as zipObj:
zipObj.extractall(logName)
if sizestr.upper() == 'SMALL':
print("Unpacking small.zip")
if os.path.isfile(logDir + "/small.zip"):
with ZipFile(logDir + "/small.zip", 'r') as logs:
logs.extractall(logDir)
elif sizestr.upper() == 'STANDARD':
print("Unpacking standard.zip")
print("WARNING: parsing standard.zip may take longer than expected.")
if os.path.isfile(logDir + "/standard.zip"):
with ZipFile(logDir + "/standard.zip", 'r') as logs:
logs.extractall(logDir)
else:
print('No size configured. Defaulting to small')
if os.path.isfile(logDir + "/small.zip"):
with ZipFile(logDir + "/small.zip", 'r') as logs:
logs.extractall(logDir)
except IOError:
printError()
finally:
print("----------------------------------")
def summarize():
print("Summarizing Errors, Warns, Exceptions, Stack traces...")
print("----------------------------------")
if str(isCoverity).upper() == "TRUE":
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.txt*'))])
else:
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.log*'))])
for i in result:
print(i[len(logDir) + 1:len(i)])
container = str(os.path.basename(os.path.normpath(Path(i).resolve().parents[1])))
try:
file = open(i, "r")
output = open(tldrDir + "/" + container + ".log", "a")
output.write("\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nFILE: " + str(
i[len(logDir) + 1:len(i)]) + "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n")
for line in file:
if re.findall(grepString, line):
try:
output.write(line)
except IOError:
printError()
output.close()
file.close()
except IOError:
printError()
print("----------------------------------")
def stringSearch(pattern):
print("Running string search on: \"" + pattern + "\"")
print("----------------------------------")
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.log'))])
for i in result:
print(i[len(logDir) + 1:len(i)])
try:
file = open(i, "r")
output = open(tldrDir + "/string.log", "a")
output.write("\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nFILE: " + str(
i[len(logDir) + 1:len(i)]) + "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n")
for line in file:
if re.findall(pattern, line):
try:
output.write(line)
except IOError:
printError()
output.close()
file.close()
except IOError:
printError()
print("----------------------------------")
def api_problem_calls(api_pattern):
print("Running api search on: \"" + api_pattern + "\"")
print("----------------------------------")
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.log'))])
include_pattern = "hub-webserver"
include = re.compile(include_pattern)
exclude_pattern = "app-log|gc-log|nginx-errors|scansummary|debug"
exclude = re.compile(exclude_pattern)
try:
for i in result:
if not re.findall(include, i):
continue
if re.findall(exclude, i):
continue
print(i)
file = open(i, "r")
output = open(tldrDir + "/suspect_api_calls.log", "a")
output.write("\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nFILE: " + str(
i[len(logDir) + 1:len(i)]) + "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n")
for line in file:
if re.findall(api_pattern, line):
try:
output.write(line)
except IOError:
printError()
output.close()
file.close()
except IOError:
printError()
print("----------------------------------")
# Thanks Madhu
def scanTimeToComplete():
print("Analyzing time to completion per scan")
print("----------------------------------")
underTen = []
overTen = []
overFifteen = []
allTime = []
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.log*'))])
for i in result:
try:
file = open(i, "r")
output = open(tldrDir + "/scanTime.log", "a")
for line in file:
if re.findall("updated to status COMPLETE", line):
splitString = line.split(" ")
splitString1 = line.split("updated to status COMPLETE ")
scanID = (splitString[9])
scanTime = str(re.findall("\d+\.\d+", splitString1[1]))[2:-2]
try:
output.write("ScanID: " + scanID + "\n" + "Time to completion: " + scanTime + " seconds\n\n")
if float(scanTime) < 600:
underTen.append(scanID)
allTime.append(float(scanTime))
elif 600 <= float(scanTime) < 895:
overTen.append(scanID)
allTime.append(float(scanTime))
elif 895 <= float(scanTime):
overFifteen.append(scanID)
allTime.append(float(scanTime))
except IOError:
printError()
output.close()
file.close()
except IOError:
printError()
print("Number of scans under 10min: " + str(len(underTen)))
print("Number of scans over 10min: " + str(len(overTen)))
print("Number of scans over 15min: " + str(len(overFifteen)))
allTime.sort(reverse=True)
if len(overFifteen) == 0:
print("No scans exceeded the Detect timeout")
else:
print("Suggested timeout limit to allow under 1% of failure: " + str(
round(float(allTime[int(len(allTime) / 100)]))) + " seconds")
try:
print("Longest scan time: " + str(round(float(allTime[0]))) + " seconds")
except IndexError:
print("Did not find any scans.")
def searchScanId(scanid):
print("Searching for scan ID: " + scanid)
print("----------------------------------")
if str(isCoverity).upper() == "TRUE":
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.txt*'))])
else:
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.log*'))])
dates = []
includePattern = "hub-scan|blackduck-bomengine|rabbitmq|jobrunner"
include = re.compile(includePattern)
excludePattern = "access-log|scansummary|debug"
exclude = re.compile(excludePattern)
try:
for i in result:
if not re.findall(include, i):
continue
if re.findall(exclude, i):
continue
print(i)
file = open(i, "r")
output = open(tldrDir + "/scanid.log", "a")
for line in file:
if re.findall(scanid, line):
try:
splitline = line.split(" ")
dates.append([splitline[1], splitline[2], splitline[0], line])
except IOError:
printError()
file.close()
except IOError:
printError()
print("----------------------------------")
if not len(dates):
print("No instances of scan ID: " + str(scanid))
else:
sorteddates = sorted(dates, key=lambda x: x[1])
print("First instance of scan: " + str(sorteddates[0][-1]))
print("Last instance of scan: " + str(sorteddates[-1][-1]))
output = open(tldrDir + "/scanid.log", "a")
count = 0
for i in sorteddates:
output.write(str(sorteddates[count][-1]))
count += 1
output.close()
print("----------------------------------")
def sysinfo():
print("Grabbing system, scan, and job info...")
print("----------------------------------")
debugDir = logName + "/debug/"
try:
for file in os.listdir(debugDir):
if file.startswith("sysinfo"):
copyfile(debugDir + file, tldrDir + "/sysinfo.log")
if file.startswith("jobinfo"):
copyfile(debugDir + file, tldrDir + "/jobinfo.log")
if file.startswith("scaninfo"):
copyfile(debugDir + file, tldrDir + "/scaninfo.log")
for file in os.listdir(logDir):
if file.startswith("systemcheck"):
copyfile(logDir + "/" + file, tldrDir + "/" + file)
except FileNotFoundError:
print("File not found.")
def keywordSearch(kwords):
keywordList = kwords.split(",")
keywordPattern = kwords.replace(",", "|")
keywordString = re.compile(keywordPattern)
print("Keywords: ")
for i in keywordList:
print(i)
print("----------------------------------")
if str(isCoverity).upper() == "TRUE":
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.txt*'))])
else:
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.log*'))])
for i in result:
try:
print(i[len(logDir) + 1:len(i)])
file = open(i, "r")
output = open(tldrDir + "/keywordSearch.log", "a")
output.write("\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nFILE: " + str(
i[len(logDir) + 1:len(i)]) + "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n")
for line in file:
try:
if re.findall(keywordString, line):
output.write(line)
except IOError:
printError()
except IOError:
printError()
output.close()
file.close()
print("----------------------------------")
def fuzzySearch(searchString):
print("Running fuzzy search on: \"" + searchString + "\"")
print("----------------------------------")
if str(isCoverity).upper() == "TRUE":
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.txt*'))])
else:
result = sorted([y for x in os.walk(logDir) for y in glob(os.path.join(x[0], '*.log*'))])
for i in result:
print(i[len(logDir) + 1:len(i)])
try:
file = open(i, "r")
output = open(tldrDir + "/fuzzySearch.log", "a")
output.write("\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\nFILE: " + str(
i[len(logDir) + 1:len(i)]) + "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n")
for line in file:
if fuzzString.count(" ") > 0 and fuzz.partial_ratio(fuzzString.upper(), line.upper()) > 76:
try:
output.write(line)
except IOError:
printError()
else:
subline = str(re.sub("[^0-9a-zA-Z]+", " ", line))
splitLine = subline.split(" ")
if fuzz.partial_ratio(searchString.upper(), subline.upper()) > 50:
for word in splitLine:
word = str(re.sub("[^0-9a-zA-Z]+", " ", word))
if fuzz.partial_ratio(fuzzString.upper(), word.upper()) > 75:
if len(word) < len(fuzzString) and fuzz.ratio(fuzzString.upper(), word.upper()) < 75:
continue
if re.findall("^\tat", line):
continue
try:
output = open(tldrDir + "/fuzzySearch.log", "a")
output.write(line)
break
except IOError:
printError()
except IOError:
printError()
output.close()
file.close()
print("----------------------------------")
if __name__ == '__main__':
banner()
unpack(pkgSize)
if skipSum.upper() != "TRUE":
summarize()
if stringPattern != "None":
stringSearch(stringPattern)
if targetScanId != "None":
searchScanId(targetScanId)
if keywords != "None":
keywordSearch(keywords)
if fuzzString != "None":
fuzzySearch(fuzzString)
if str(isCoverity).upper() != "TRUE":
sysinfo()
scanTimeToComplete()
api_problem_calls(api_pattern)
print("----------------------------------")
print("All scans complete.")