This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
analyze_troubleshooting.py
63 lines (50 loc) · 1.97 KB
/
analyze_troubleshooting.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
"""
Provided with a path to the file with logs from a reports generator run,
this script looks for all debug file locations, downloads them from S3 bucket
and tries to match them to the well-known errors.
If cannot do so, dumps all the necessary troubleshooting data into OUTPUT_DIR
in a convenient format.
"""
import io
import json
import pathlib
import re
import shutil
import sys
import boto3
S3_BUCKET='zalando-slr-service-level-reporting-s3-bucket'
FILEPATH_PATTERN = re.compile(r"([\w/\-.]+(?:\.debug\.json|.tsv))")
YRANGE_TOO_SMALL = re.compile(r"set\s+yrange\s+\[(.+):\1\]")
OUTPUT_DIR = pathlib.Path('troubleshooting/')
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
s3 = boto3.client('s3')
log_file = open(sys.argv[1])
for line in log_file:
filepath = FILEPATH_PATTERN.findall(line)
if not filepath:
continue
filepath = filepath[0].replace('/var/', '')
stream = io.BytesIO()
s3.download_fileobj(S3_BUCKET, filepath, stream)
tb_data = json.loads(stream.getvalue())
gnuplot_result = ''.join(tb_data['gnuplot_result']).strip()
gnuplot_data = tb_data['gnuplot_data']
issues = set()
if YRANGE_TOO_SMALL.findall(tb_data['gnuplot_data']):
issues.add('YRANGE_TOO_SMALL')
tsv_paths = FILEPATH_PATTERN.findall(gnuplot_data)
for tsv_path in tsv_paths:
tsv = tb_data['tsvs'][tsv_path]
if not tsv:
issues.add('NO_SLI_VALUES')
if not issues:
issues.add('N/A')
OUTPUT_DIR.mkdir(exist_ok=True)
output_path = OUTPUT_DIR / (filepath.replace('/', '_'))
output_path.mkdir()
(output_path / 'tb_data.json').write_bytes(stream.getvalue())
(output_path / 'gnuplot_result').write_text(gnuplot_result)
(output_path / 'gnuplot_data').write_text(gnuplot_data)
for tsv_path in FILEPATH_PATTERN.findall(gnuplot_data):
(output_path / tsv_path.replace('/', '_')).write_text(tb_data['tsvs'][tsv_path])
print(f"{filepath:180} {','.join(issues)}")