blob: e8652f43023ed50d1c698b7a13d6375babffb0ad [file] [log] [blame]
Peter D Phan72ce6b82021-06-03 06:18:26 -05001#!/usr/bin/env python
2
3r"""
4See class prolog below for details.
5"""
6
7import os
8import sys
9import yaml
10import time
11import platform
12from errno import EACCES, EPERM
13from ssh_utility import SSHRemoteclient
14
15
16class FFDCCollector:
17
18 r"""
19 Sends commands from configuration file to the targeted system to collect log files.
20 Fetch and store generated files at the specified location.
21
22 """
23
Peter D Phan04aca3b2021-06-21 10:37:18 -050024 # List of supported OSes.
25 supported_oses = ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
26
27 def __init__(self, hostname, username, password, ffdc_config, location, remote_type):
Peter D Phan72ce6b82021-06-03 06:18:26 -050028 r"""
29 Description of argument(s):
30
31 hostname name/ip of the targeted (remote) system
32 username user on the targeted system with access to FFDC files
33 password password for user on targeted system
34 ffdc_config configuration file listing commands and files for FFDC
Peter D Phan04aca3b2021-06-21 10:37:18 -050035 location where to store collected FFDC
36 remote_type os type of the remote host
Peter D Phan72ce6b82021-06-03 06:18:26 -050037
38 """
39 if self.verify_script_env():
40 self.hostname = hostname
41 self.username = username
42 self.password = password
43 self.ffdc_config = ffdc_config
44 self.location = location
45 self.remote_client = None
46 self.ffdc_dir_path = ""
47 self.ffdc_prefix = ""
Peter D Phan04aca3b2021-06-21 10:37:18 -050048 self.target_type = remote_type.upper()
Peter D Phan72ce6b82021-06-03 06:18:26 -050049 else:
Peter D Phan8462faf2021-06-16 12:24:15 -050050 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -050051
52 def verify_script_env(self):
53
54 # Import to log version
55 import click
56 import paramiko
57
58 run_env_ok = True
59 print("\n\t---- Script host environment ----")
60 print("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
61 print("\t{:<10} {:<10}".format('Script host os', platform.platform()))
62 print("\t{:<10} {:>10}".format('Python', platform.python_version()))
63 print("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
64 print("\t{:<10} {:>10}".format('click', click.__version__))
65 print("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
66
Peter D Phan8462faf2021-06-16 12:24:15 -050067 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
68 print("\n\tERROR: Python or python packages do not meet minimum version requirement.")
69 print("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050070 run_env_ok = False
71
72 print("\t---- End script host environment ----")
73 return run_env_ok
74
75 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050076 r"""
77 Check if target system is ping-able.
78
79 """
80 response = os.system("ping -c 1 -w 2 %s 2>&1 >/dev/null" % self.hostname)
81 if response == 0:
George Keishing615fe322021-06-24 01:24:36 -050082 print("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -050083 return True
84 else:
85 print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
86 sys.exit(-1)
87
Peter D Phan04aca3b2021-06-21 10:37:18 -050088 def inspect_target_machine_type(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050089 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -050090 Inspect remote host os-release or uname.
Peter D Phan72ce6b82021-06-03 06:18:26 -050091
92 """
Peter D Phan04aca3b2021-06-21 10:37:18 -050093 command = "cat /etc/os-release"
94 response = self.remoteclient.execute_command(command)
95 if response:
96 print("\n\t[INFO] %s /etc/os-release\n" % self.hostname)
97 print("\t\t %s" % self.find_os_type(response, 'PRETTY_NAME'))
98 identity = self.find_os_type(response, 'ID').split('=')[1].upper()
99 else:
100 response = self.remoteclient.execute_command('uname -a')
101 print("\n\t[INFO] %s uname -a\n" % self.hostname)
102 print("\t\t %s" % ' '.join(response))
103 identity = self.find_os_type(response, 'AIX').split(' ')[0].upper()
Peter D Phan2b8052d2021-06-22 10:55:41 -0500104
105 # If OS does not have /etc/os-release and is not AIX,
106 # script does not yet know what to do.
Peter D Phan04aca3b2021-06-21 10:37:18 -0500107 if not identity:
108 print(">>>>>\tERROR: Script does not yet know about %s" % ' '.join(response))
109 sys.exit(-1)
110
111 if self.target_type not in identity:
112 user_target_type = self.target_type
Peter D Phan2b8052d2021-06-22 10:55:41 -0500113 self.target_type = ""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500114 for each_os in FFDCCollector.supported_oses:
115 if each_os in identity:
116 self.target_type = each_os
117 break
Peter D Phan2b8052d2021-06-22 10:55:41 -0500118
119 # If OS in not one of ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
120 # script does not yet know what to do.
121 if not self.target_type:
122 print(">>>>>\tERROR: Script does not yet know about %s" % identity)
123 sys.exit(-1)
124
Peter D Phan04aca3b2021-06-21 10:37:18 -0500125 print("\n\t[WARN] user request %s does not match remote host type %s.\n"
126 % (user_target_type, self.target_type))
127 print("\t[WARN] FFDC collection continues for %s.\n" % self.target_type)
128
129 def find_os_type(self,
130 listing_from_os,
131 key):
132
133 r"""
134 Return OS information with the requested key
135
136 Description of argument(s):
137
138 listing_from_os list of information returns from OS command
139 key key of the desired data
140
141 """
142
143 for each_item in listing_from_os:
144 if key in each_item:
145 return each_item
146 return ''
Peter D Phan72ce6b82021-06-03 06:18:26 -0500147
148 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500149 r"""
150 Initiate FFDC Collection depending on requested protocol.
151
152 """
153
George Keishing772c9772021-06-16 23:23:42 -0500154 print("\n\t---- Start communicating with %s ----" % self.hostname)
155 working_protocol_list = []
Peter D Phan72ce6b82021-06-03 06:18:26 -0500156 if self.target_is_pingable():
George Keishing772c9772021-06-16 23:23:42 -0500157 # Check supported protocol ping,ssh, redfish are working.
158 if self.ssh_to_target_system():
159 working_protocol_list.append("SSH")
George Keishing615fe322021-06-24 01:24:36 -0500160 working_protocol_list.append("SCP")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500161 # Verify top level directory exists for storage
162 self.validate_local_store(self.location)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500163 self.inspect_target_machine_type()
George Keishing772c9772021-06-16 23:23:42 -0500164 print("\n\t---- Completed protocol pre-requisite check ----\n")
165 self.generate_ffdc(working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500166
167 def ssh_to_target_system(self):
168 r"""
169 Open a ssh connection to targeted system.
170
171 """
172
173 self.remoteclient = SSHRemoteclient(self.hostname,
174 self.username,
175 self.password)
176
177 self.remoteclient.ssh_remoteclient_login()
George Keishing772c9772021-06-16 23:23:42 -0500178 print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500179
180 # Check scp connection.
181 # If scp connection fails,
182 # continue with FFDC generation but skip scp files to local host.
183 self.remoteclient.scp_connection()
George Keishing772c9772021-06-16 23:23:42 -0500184 return True
Peter D Phan72ce6b82021-06-03 06:18:26 -0500185
George Keishing772c9772021-06-16 23:23:42 -0500186 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500187 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500188 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500189
Peter D Phan04aca3b2021-06-21 10:37:18 -0500190 Description of argument(s):
191 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500192 """
193
George Keishing772c9772021-06-16 23:23:42 -0500194 print("\n\t---- Executing commands on " + self.hostname + " ----")
195 print("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500196 with open(self.ffdc_config, 'r') as file:
197 ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
198
Peter D Phan2b8052d2021-06-22 10:55:41 -0500199 # Set prefix values for scp files and directory.
200 # Since the time stamp is at second granularity, these values are set here
201 # to be sure that all files for this run will have same timestamps
202 # and they will be saved in the same directory.
203 # self.location == local system for now
204 self.set_ffdc_defaults()
205
Peter D Phan72ce6b82021-06-03 06:18:26 -0500206 for machine_type in ffdc_actions.keys():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500207
Peter D Phan04aca3b2021-06-21 10:37:18 -0500208 if machine_type == self.target_type:
George Keishing772c9772021-06-16 23:23:42 -0500209 if (ffdc_actions[machine_type]['PROTOCOL'][0] in working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500210
Peter D Phan2b8052d2021-06-22 10:55:41 -0500211 # For OPENBMC collect general system info.
212 if self.target_type == 'OPENBMC':
213
214 self.collect_and_copy_ffdc(ffdc_actions['GENERAL'],
215 form_filename=True)
Peter D Phan56429a62021-06-23 08:38:29 -0500216 self.group_copy(ffdc_actions['OPENBMC_DUMPS'])
Peter D Phan2b8052d2021-06-22 10:55:41 -0500217
218 # For RHEL and UBUNTU, collect common Linux OS FFDC.
Peter D Phan04aca3b2021-06-21 10:37:18 -0500219 if self.target_type == 'RHEL' \
220 or self.target_type == 'UBUNTU':
Peter D Phan72ce6b82021-06-03 06:18:26 -0500221
Peter D Phan04aca3b2021-06-21 10:37:18 -0500222 self.collect_and_copy_ffdc(ffdc_actions['LINUX'])
223
224 # Collect remote host specific FFDC.
225 self.collect_and_copy_ffdc(ffdc_actions[machine_type])
Peter D Phan72ce6b82021-06-03 06:18:26 -0500226 else:
227 print("\n\tProtocol %s is not yet supported by this script.\n"
228 % ffdc_actions[machine_type]['PROTOCOL'][0])
229
Peter D Phan04aca3b2021-06-21 10:37:18 -0500230 # Close network connection after collecting all files
231 self.remoteclient.ssh_remoteclient_disconnect()
232
233 def collect_and_copy_ffdc(self,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500234 ffdc_actions_for_machine_type,
235 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500236 r"""
237 Send commands in ffdc_config file to targeted system.
238
239 Description of argument(s):
240 ffdc_actions_for_machine_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500241 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500242 """
243
244 print("\n\t[Run] Executing commands on %s using %s"
245 % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
246 list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
247 progress_counter = 0
248 for command in list_of_commands:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500249 if form_filename:
250 command = str(command % self.target_type)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500251 self.remoteclient.execute_command(command)
252 progress_counter += 1
253 self.print_progress(progress_counter)
254
255 print("\n\t[Run] Commands execution completed.\t\t [OK]")
256
257 if self.remoteclient.scpclient:
258 print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500259
Peter D Phan04aca3b2021-06-21 10:37:18 -0500260 # Retrieving files from target system
261 list_of_files = ffdc_actions_for_machine_type['FILES']
Peter D Phan2b8052d2021-06-22 10:55:41 -0500262 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500263 else:
264 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
265
Peter D Phan56429a62021-06-23 08:38:29 -0500266 def group_copy(self,
267 ffdc_actions_for_machine_type):
268
269 r"""
270 scp group of files (wild card) from remote host.
271
272 Description of argument(s):
273 ffdc_actions_for_machine_type commands and files for the selected remote host type.
274 """
275 if self.remoteclient.scpclient:
George Keishing615fe322021-06-24 01:24:36 -0500276 print("\n\tCopying DUMP files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500277
278 # Retrieving files from target system, if any
279 list_of_files = ffdc_actions_for_machine_type['FILES']
280
281 for filename in list_of_files:
282 command = 'ls -AX ' + filename
283 response = self.remoteclient.execute_command(command)
284 # self.remoteclient.scp_file_from_remote() completed without exception,
285 # if any
286 if response:
287 scp_result = self.remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
288 if scp_result:
289 print("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
290 else:
291 print("\t\tThere is no " + filename)
292
293 else:
294 print("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
295
Peter D Phan72ce6b82021-06-03 06:18:26 -0500296 def scp_ffdc(self,
297 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500298 targ_file_prefix,
299 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500300 file_list=None,
301 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500302 r"""
303 SCP all files in file_dict to the indicated directory on the local system.
304
305 Description of argument(s):
306 targ_dir_path The path of the directory to receive the files.
307 targ_file_prefix Prefix which will be pre-pended to each
308 target file's name.
309 file_dict A dictionary of files to scp from targeted system to this system
310
311 """
312
Peter D Phan72ce6b82021-06-03 06:18:26 -0500313 progress_counter = 0
314 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500315 if form_filename:
316 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500317 source_file_path = filename
318 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
319
320 # self.remoteclient.scp_file_from_remote() completed without exception,
321 # add file to the receiving file list.
322 scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500323
324 if not quiet:
325 if scp_result:
Peter D Phan8462faf2021-06-16 12:24:15 -0500326 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500327 else:
328 print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
329 else:
330 progress_counter += 1
331 self.print_progress(progress_counter)
332
Peter D Phan72ce6b82021-06-03 06:18:26 -0500333 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500334 r"""
335 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
336 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
337 Individual ffdc file will have timestr_filename.
338
339 Description of class variables:
340 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
341
342 self.ffdc_prefix The prefix to be given to each ffdc file name.
343
344 """
345
346 timestr = time.strftime("%Y%m%d-%H%M%S")
347 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
348 self.ffdc_prefix = timestr + "_"
349 self.validate_local_store(self.ffdc_dir_path)
350
351 def validate_local_store(self, dir_path):
352 r"""
353 Ensure path exists to store FFDC files locally.
354
355 Description of variable:
356 dir_path The dir path where collected ffdc data files will be stored.
357
358 """
359
360 if not os.path.exists(dir_path):
361 try:
362 os.mkdir(dir_path, 0o755)
363 except (IOError, OSError) as e:
364 # PermissionError
365 if e.errno == EPERM or e.errno == EACCES:
366 print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path)
367 else:
368 print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror))
369 sys.exit(-1)
370
371 def print_progress(self, progress):
372 r"""
373 Print activity progress +
374
375 Description of variable:
376 progress Progress counter.
377
378 """
379
380 sys.stdout.write("\r\t" + "+" * progress)
381 sys.stdout.flush()
382 time.sleep(.1)