blob: bd823df0003fad1b12d3da80863ae392689a056f [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 = ""
48 self.receive_file_list = []
Peter D Phan04aca3b2021-06-21 10:37:18 -050049 self.target_type = remote_type.upper()
Peter D Phan72ce6b82021-06-03 06:18:26 -050050 else:
Peter D Phan8462faf2021-06-16 12:24:15 -050051 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -050052
53 def verify_script_env(self):
54
55 # Import to log version
56 import click
57 import paramiko
58
59 run_env_ok = True
60 print("\n\t---- Script host environment ----")
61 print("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
62 print("\t{:<10} {:<10}".format('Script host os', platform.platform()))
63 print("\t{:<10} {:>10}".format('Python', platform.python_version()))
64 print("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
65 print("\t{:<10} {:>10}".format('click', click.__version__))
66 print("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
67
Peter D Phan8462faf2021-06-16 12:24:15 -050068 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
69 print("\n\tERROR: Python or python packages do not meet minimum version requirement.")
70 print("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050071 run_env_ok = False
72
73 print("\t---- End script host environment ----")
74 return run_env_ok
75
76 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050077 r"""
78 Check if target system is ping-able.
79
80 """
81 response = os.system("ping -c 1 -w 2 %s 2>&1 >/dev/null" % self.hostname)
82 if response == 0:
George Keishing772c9772021-06-16 23:23:42 -050083 print("\n\t[Check] %s is ping-able.\t\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -050084 return True
85 else:
86 print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
87 sys.exit(-1)
88
Peter D Phan04aca3b2021-06-21 10:37:18 -050089 def inspect_target_machine_type(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050090 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -050091 Inspect remote host os-release or uname.
Peter D Phan72ce6b82021-06-03 06:18:26 -050092
93 """
Peter D Phan04aca3b2021-06-21 10:37:18 -050094 command = "cat /etc/os-release"
95 response = self.remoteclient.execute_command(command)
96 if response:
97 print("\n\t[INFO] %s /etc/os-release\n" % self.hostname)
98 print("\t\t %s" % self.find_os_type(response, 'PRETTY_NAME'))
99 identity = self.find_os_type(response, 'ID').split('=')[1].upper()
100 else:
101 response = self.remoteclient.execute_command('uname -a')
102 print("\n\t[INFO] %s uname -a\n" % self.hostname)
103 print("\t\t %s" % ' '.join(response))
104 identity = self.find_os_type(response, 'AIX').split(' ')[0].upper()
Peter D Phan2b8052d2021-06-22 10:55:41 -0500105
106 # If OS does not have /etc/os-release and is not AIX,
107 # script does not yet know what to do.
Peter D Phan04aca3b2021-06-21 10:37:18 -0500108 if not identity:
109 print(">>>>>\tERROR: Script does not yet know about %s" % ' '.join(response))
110 sys.exit(-1)
111
112 if self.target_type not in identity:
113 user_target_type = self.target_type
Peter D Phan2b8052d2021-06-22 10:55:41 -0500114 self.target_type = ""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500115 for each_os in FFDCCollector.supported_oses:
116 if each_os in identity:
117 self.target_type = each_os
118 break
Peter D Phan2b8052d2021-06-22 10:55:41 -0500119
120 # If OS in not one of ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
121 # script does not yet know what to do.
122 if not self.target_type:
123 print(">>>>>\tERROR: Script does not yet know about %s" % identity)
124 sys.exit(-1)
125
Peter D Phan04aca3b2021-06-21 10:37:18 -0500126 print("\n\t[WARN] user request %s does not match remote host type %s.\n"
127 % (user_target_type, self.target_type))
128 print("\t[WARN] FFDC collection continues for %s.\n" % self.target_type)
129
130 def find_os_type(self,
131 listing_from_os,
132 key):
133
134 r"""
135 Return OS information with the requested key
136
137 Description of argument(s):
138
139 listing_from_os list of information returns from OS command
140 key key of the desired data
141
142 """
143
144 for each_item in listing_from_os:
145 if key in each_item:
146 return each_item
147 return ''
Peter D Phan72ce6b82021-06-03 06:18:26 -0500148
149 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500150 r"""
151 Initiate FFDC Collection depending on requested protocol.
152
153 """
154
George Keishing772c9772021-06-16 23:23:42 -0500155 print("\n\t---- Start communicating with %s ----" % self.hostname)
156 working_protocol_list = []
Peter D Phan72ce6b82021-06-03 06:18:26 -0500157 if self.target_is_pingable():
George Keishing772c9772021-06-16 23:23:42 -0500158 # Check supported protocol ping,ssh, redfish are working.
159 if self.ssh_to_target_system():
160 working_protocol_list.append("SSH")
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)
216
217 # For RHEL and UBUNTU, collect common Linux OS FFDC.
Peter D Phan04aca3b2021-06-21 10:37:18 -0500218 if self.target_type == 'RHEL' \
219 or self.target_type == 'UBUNTU':
Peter D Phan72ce6b82021-06-03 06:18:26 -0500220
Peter D Phan04aca3b2021-06-21 10:37:18 -0500221 self.collect_and_copy_ffdc(ffdc_actions['LINUX'])
222
223 # Collect remote host specific FFDC.
224 self.collect_and_copy_ffdc(ffdc_actions[machine_type])
Peter D Phan72ce6b82021-06-03 06:18:26 -0500225 else:
226 print("\n\tProtocol %s is not yet supported by this script.\n"
227 % ffdc_actions[machine_type]['PROTOCOL'][0])
228
Peter D Phan04aca3b2021-06-21 10:37:18 -0500229 # Close network connection after collecting all files
230 self.remoteclient.ssh_remoteclient_disconnect()
231
232 def collect_and_copy_ffdc(self,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500233 ffdc_actions_for_machine_type,
234 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500235 r"""
236 Send commands in ffdc_config file to targeted system.
237
238 Description of argument(s):
239 ffdc_actions_for_machine_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500240 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500241 """
242
243 print("\n\t[Run] Executing commands on %s using %s"
244 % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
245 list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
246 progress_counter = 0
247 for command in list_of_commands:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500248 if form_filename:
249 command = str(command % self.target_type)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500250 self.remoteclient.execute_command(command)
251 progress_counter += 1
252 self.print_progress(progress_counter)
253
254 print("\n\t[Run] Commands execution completed.\t\t [OK]")
255
256 if self.remoteclient.scpclient:
257 print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500258
Peter D Phan04aca3b2021-06-21 10:37:18 -0500259 # Retrieving files from target system
260 list_of_files = ffdc_actions_for_machine_type['FILES']
Peter D Phan2b8052d2021-06-22 10:55:41 -0500261 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500262 else:
263 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
264
Peter D Phan72ce6b82021-06-03 06:18:26 -0500265 def scp_ffdc(self,
266 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500267 targ_file_prefix,
268 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500269 file_list=None,
270 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500271 r"""
272 SCP all files in file_dict to the indicated directory on the local system.
273
274 Description of argument(s):
275 targ_dir_path The path of the directory to receive the files.
276 targ_file_prefix Prefix which will be pre-pended to each
277 target file's name.
278 file_dict A dictionary of files to scp from targeted system to this system
279
280 """
281
Peter D Phan72ce6b82021-06-03 06:18:26 -0500282 progress_counter = 0
283 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500284 if form_filename:
285 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500286 source_file_path = filename
287 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
288
289 # self.remoteclient.scp_file_from_remote() completed without exception,
290 # add file to the receiving file list.
291 scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
292 if scp_result:
293 self.receive_file_list.append(targ_file_path)
294
295 if not quiet:
296 if scp_result:
Peter D Phan8462faf2021-06-16 12:24:15 -0500297 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500298 else:
299 print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
300 else:
301 progress_counter += 1
302 self.print_progress(progress_counter)
303
Peter D Phan72ce6b82021-06-03 06:18:26 -0500304 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500305 r"""
306 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
307 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
308 Individual ffdc file will have timestr_filename.
309
310 Description of class variables:
311 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
312
313 self.ffdc_prefix The prefix to be given to each ffdc file name.
314
315 """
316
317 timestr = time.strftime("%Y%m%d-%H%M%S")
318 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
319 self.ffdc_prefix = timestr + "_"
320 self.validate_local_store(self.ffdc_dir_path)
321
322 def validate_local_store(self, dir_path):
323 r"""
324 Ensure path exists to store FFDC files locally.
325
326 Description of variable:
327 dir_path The dir path where collected ffdc data files will be stored.
328
329 """
330
331 if not os.path.exists(dir_path):
332 try:
333 os.mkdir(dir_path, 0o755)
334 except (IOError, OSError) as e:
335 # PermissionError
336 if e.errno == EPERM or e.errno == EACCES:
337 print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path)
338 else:
339 print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror))
340 sys.exit(-1)
341
342 def print_progress(self, progress):
343 r"""
344 Print activity progress +
345
346 Description of variable:
347 progress Progress counter.
348
349 """
350
351 sys.stdout.write("\r\t" + "+" * progress)
352 sys.stdout.flush()
353 time.sleep(.1)