/
must_gather.py
268 lines (231 loc) · 9.21 KB
/
must_gather.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
import os
import logging
import shutil
import tempfile
import re
import tarfile
from pathlib import Path
from ocs_ci.framework import config
from ocs_ci.helpers.helpers import storagecluster_independent_check
from ocs_ci.ocs.resources.pod import get_all_pods
from ocs_ci.ocs.utils import collect_ocs_logs
from ocs_ci.ocs.must_gather.const_must_gather import (
GATHER_COMMANDS_VERSION,
GATHER_COMMANDS_LOG,
)
from ocs_ci.utility import version
from ocs_ci.ocs.constants import MANAGED_SERVICE_PLATFORMS
logger = logging.getLogger(__name__)
class MustGather(object):
"""
MustGather Class
"""
def __init__(self):
self.type_log = None
self.root = None
self.files_path = dict()
self.empty_files = list()
self.files_not_exist = list()
self.files_content_issue = list()
self.ocs_version = version.get_semantic_ocs_version_from_config()
@property
def log_type(self):
return self.type_log
@log_type.setter
def log_type(self, type_log):
if not isinstance(type_log, str):
raise ValueError("log type arg must be a string")
self.type_log = type_log
def collect_must_gather(self):
"""
Collect ocs_must_gather and copy the logs to a temporary folder.
"""
temp_folder = tempfile.mkdtemp()
collect_ocs_logs(dir_name=temp_folder, ocp=False)
self.root = temp_folder + "_ocs_logs"
def search_file_path(self):
"""
Search File Path
"""
ocs_version = float(
f"{version.get_ocs_version_from_csv(only_major_minor=True)}"
)
if (
self.type_log == "OTHERS"
and config.ENV_DATA["platform"] in MANAGED_SERVICE_PLATFORMS
):
files = GATHER_COMMANDS_VERSION[ocs_version]["OTHERS_MANAGED_SERVICES"]
elif self.type_log == "OTHERS" and storagecluster_independent_check():
files = GATHER_COMMANDS_VERSION[ocs_version]["OTHERS_EXTERNAL"]
else:
files = GATHER_COMMANDS_VERSION[ocs_version][self.type_log]
for file in files:
self.files_not_exist.append(file)
for dir_name, subdir_list, files_list in os.walk(self.root):
if file in files_list:
self.files_path[file] = os.path.join(dir_name, file)
self.files_not_exist.remove(file)
break
def validate_file_size(self):
"""
Validate the file is not empty
"""
if self.type_log != "OTHERS":
return
for path, subdirs, files in os.walk(self.root):
for file in files:
file_path = os.path.join(path, file)
if (
Path(file_path).stat().st_size == 0
and "noobaa-db-pg-0-init.log" not in file_path
):
logger.error(f"log file {file} empty!")
self.empty_files.append(file)
def validate_expected_files(self):
"""
Make sure all the relevant files exist
"""
self.search_file_path()
# https://bugzilla.redhat.com/show_bug.cgi?id=2125204
# https://bugzilla.redhat.com/show_bug.cgi?id=2049204
# self.verify_ceph_file_content()
for file, file_path in self.files_path.items():
if not Path(file_path).is_file():
self.files_not_exist.append(file)
elif re.search(r"\.yaml$", file):
with open(file_path, "r") as f:
if "kind" not in f.read().lower():
self.files_content_issue.append(file)
def verify_ceph_file_content(self):
"""
Verify ceph command does not return an error
https://bugzilla.redhat.com/show_bug.cgi?id=2014849
https://bugzilla.redhat.com/show_bug.cgi?id=2021427
"""
if self.type_log != "CEPH" or self.ocs_version < version.VERSION_4_9:
return
pattern = re.compile("exit code [1-9]+")
for root, dirs, files in os.walk(self.root):
for file in files:
try:
with open(os.path.join(root, file), "r") as f:
data_file = f.read()
exit_code_error = pattern.findall(data_file.lower())
if len(exit_code_error) > 0 and "gather-debug" not in file:
self.files_content_issue.append(os.path.join(root, file))
except Exception as e:
logger.error(f"There is no option to read {file}, error: {e}")
def print_must_gather_debug(self) -> None:
try:
with open(os.path.join(self.root, GATHER_COMMANDS_LOG), "r") as f:
logger.info("Printing must-gather internal log file")
logger.info(f.readlines())
except FileNotFoundError:
logger.error("must-gather internal log file not found")
def compare_running_pods(self):
"""
Compare running pods list to "/pods" subdirectories
"""
if self.type_log != "OTHERS":
return
pod_objs = get_all_pods(namespace=config.ENV_DATA["cluster_namespace"])
pod_names = []
logger.info("Get pod names on openshift-storage project")
for pod in pod_objs:
pattern = self.check_pod_name_pattern(pod.name)
if pattern is False:
pod_names.append(pod.name)
for dir_name, subdir_list, files_list in os.walk(self.root):
if re.search("openshift-storage/pods$", dir_name):
pod_path = dir_name
break
pod_files = []
logger.info("Get pod names on openshift-storage/pods directory")
for pod_file in os.listdir(pod_path):
pattern = self.check_pod_name_pattern(pod_file)
if pattern is False:
pod_files.append(pod_file)
diff = list(set(pod_files) - set(pod_names)) + list(
set(pod_names) - set(pod_files)
)
assert set(sorted(pod_files)) == set(sorted(pod_names)), (
f"List of openshift-storage pods are not equal to list of logs\n"
f"directories list of pods: {pod_names} list of log directories: {pod_files}\n"
f"The difference between pod files and actual pods is: {diff}\n"
)
def check_pod_name_pattern(self, pod_name):
"""
check Pod Name Pattern
Args:
pod_name (str): pod name
return:
bool: True if match pattern, False otherwise
"""
regular_ex_list = [
"must-gather-.*.-helper",
r"^compute-*",
r"^ip-*",
r"^j-*",
r"^argo-*",
r"^vmware-*",
"^must-gather",
r"-debug$",
# https://bugzilla.redhat.com/show_bug.cgi?id=2245246
r"^csi-addons-controller-manager*",
]
for regular_ex in regular_ex_list:
if re.search(regular_ex, pod_name) is not None:
return True
return False
def print_invalid_files(self):
"""
Print Invalid Files
"""
if any([self.empty_files, self.files_not_exist, self.files_content_issue]):
error = (
f"Files don't exist:\n{self.files_not_exist}\n"
f"Empty files:\n{self.empty_files}\n"
f"Content issues:\n{self.files_content_issue}"
)
self.empty_files = list()
self.files_not_exist = list()
self.files_content_issue = list()
raise Exception(error)
def verify_noobaa_diagnostics(self):
"""
Verify noobaa diagnostics folder exist
"""
ocs_version = version.get_ocs_version_from_csv(only_major_minor=True)
if self.type_log == "OTHERS" and ocs_version >= version.VERSION_4_6:
flag = False
logger.info("Verify noobaa_diagnostics folder exist")
for path, subdirs, files in os.walk(self.root):
for file in files:
if re.search(r"noobaa_diagnostics_.*.tar.gz", file):
flag = True
logger.info(f"Extract noobaa_diagnostics dir {file}")
path_noobaa_diag = os.path.join(path, file)
files_noobaa_diag = tarfile.open(path_noobaa_diag)
files_noobaa_diag.extractall(path)
break
if not flag:
logger.error("noobaa_diagnostics.tar.gz does not exist")
self.files_not_exist.append("noobaa_diagnostics.tar.gz")
def validate_must_gather(self):
"""
Validate must-gather
"""
if config.ENV_DATA["platform"] not in MANAGED_SERVICE_PLATFORMS:
self.verify_noobaa_diagnostics()
self.validate_file_size()
self.validate_expected_files()
self.print_invalid_files()
self.compare_running_pods()
self.print_must_gather_debug()
def cleanup(self):
"""
Delete temporary folder.
"""
logger.info(f"Delete must gather folder {self.root}")
if re.search("_ocs_logs$", self.root):
shutil.rmtree(path=self.root, ignore_errors=False, onerror=None)