blob: 86a324a2a6bb8552ea1289157e26fde4b94dc96a [file] [log] [blame]
Roman Bubyr303820c2022-12-20 14:49:05 +02001import subprocess
2import sys
3import os
4import yaml
Roman Bubyr2a5be092023-03-17 11:34:54 +02005from os import path
6
7try:
8 param_is_yaml = path.isfile(sys.argv[2])
9except IndexError:
10 print('Second argument <tempest-report-yaml>/<resource-ID> is not provided')
11 raise ValueError('ASecond argument <tempest-report-yaml>/<resource-ID> is not provided')
Roman Bubyr303820c2022-12-20 14:49:05 +020012
13
Roman Bubyr2a5be092023-03-17 11:34:54 +020014def log_gather(resource_id, sub_resource, log_level=None):
15 """ Get all log lines related to resource-id with error
16 :param resource_id: ID resource, e.g. request-id, server-id
17 :param sub_resource: name of sub_resource log file, e.g subnet.log for neutron resource
18 :param log_level: substring for resource_id log: e.g. get only ERROR log level, optional
Roman Bubyr303820c2022-12-20 14:49:05 +020019 """
20 try:
21 directory = os.walk(sys.argv[1])
22 except IndexError:
23 print('Argument <folder-with-logs> is not provided')
24 raise ValueError('Argument <folder-with-logs> is not provided')
25
Roman Bubyr2a5be092023-03-17 11:34:54 +020026 if param_is_yaml:
27 for dirs in directory:
28 run_cmd = f"grep -a {resource_id} {dirs[0]}/* >> {sub_resource}"
29 subprocess.run(run_cmd, shell=True)
30
31 else:
32 for dirs in directory:
33 if log_level:
34 run_cmd = f"grep -lE '{resource_id}.*{log_level}|{log_level}.*{resource_id}' {dirs[0]}/* >> 'tmp.log'"
35 else:
36 run_cmd = f"grep -l {resource_id} {dirs[0]}/* >> 'tmp.log'"
37 subprocess.run(run_cmd, shell=True)
38
39 with open('tmp.log') as f:
40 files = f.readlines()
41
42 for file in files:
43 subd = file.split("/")
44 log_dir = subd[-4] + "." + subd[-3] + "." + subd[-2]
45 log_name = subd[-1].replace('\n', '')
46 os.makedirs(os.path.join(sys.argv[2], log_dir), exist_ok=True)
47 path = os.path.join(sys.argv[2], log_dir, log_name)
48 if log_level:
49 run_cmd = f"grep -aE '{resource_id}.*{log_level}|{log_level}.*{resource_id}' {file} >> {path}"
50 else:
51 run_cmd = f"grep -a {resource_id} {file} >> {path}"
52 subprocess.run(run_cmd.replace('\n', ''), shell=True)
53
54 os.remove('tmp.log')
Roman Bubyr303820c2022-12-20 14:49:05 +020055
56
Roman Bubyr2a5be092023-03-17 11:34:54 +020057if param_is_yaml:
58 print('Find all the failed tempest tests from YAML file')
59 with open(sys.argv[2]) as f:
60 test_resources = yaml.safe_load(f)
Roman Bubyr303820c2022-12-20 14:49:05 +020061
Roman Bubyr2a5be092023-03-17 11:34:54 +020062 for test in test_resources.items():
63 # Find all the failed tempest tests from YAML file and gather logs for
64 # related resources in corresponded folders
65 if test[1]['status'] == 'failure':
66 print('Collecting logs for ' + test[0])
67 os.makedirs(test[0], exist_ok=True)
68 for resource in test[1]['resources']:
69 os.makedirs(os.path.join(test[0], resource), exist_ok=True)
70 for sub_resource in test[1]['resources'][resource]:
71 log_gather(list(test[1]['resources'][resource][sub_resource])[0],
72 os.path.join(test[0], resource, sub_resource + '.' + 'log'))
73
74else:
75 print('Find all the related log for one specific test or id with error')
76 os.makedirs(sys.argv[2], exist_ok=True)
77 if len(sys.argv) == 4:
78 log_gather(sys.argv[2], os.path.join(sys.argv[2], 'test' + '.' + 'log'), log_level=sys.argv[3])
79 else:
80 log_gather(sys.argv[2], os.path.join(sys.argv[2], 'test' + '.' + 'log'))
Roman Bubyr303820c2022-12-20 14:49:05 +020081
82print('The logger is finished')
83