1#!/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 24 # List of supported OSes. 25 supported_oses = ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU'] 26 27 def __init__(self, hostname, username, password, ffdc_config, location, remote_type): 28 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 35 location where to store collected FFDC 36 remote_type os type of the remote host 37 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.target_type = remote_type.upper() 49 else: 50 sys.exit(-1) 51 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 67 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") 70 run_env_ok = False 71 72 print("\t---- End script host environment ----") 73 return run_env_ok 74 75 def target_is_pingable(self): 76 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: 82 print("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname) 83 return True 84 else: 85 print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname) 86 sys.exit(-1) 87 88 def inspect_target_machine_type(self): 89 r""" 90 Inspect remote host os-release or uname. 91 92 """ 93 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() 104 105 # If OS does not have /etc/os-release and is not AIX, 106 # script does not yet know what to do. 107 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 113 self.target_type = "" 114 for each_os in FFDCCollector.supported_oses: 115 if each_os in identity: 116 self.target_type = each_os 117 break 118 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 125 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 '' 147 148 def collect_ffdc(self): 149 r""" 150 Initiate FFDC Collection depending on requested protocol. 151 152 """ 153 154 print("\n\t---- Start communicating with %s ----" % self.hostname) 155 working_protocol_list = [] 156 if self.target_is_pingable(): 157 # Check supported protocol ping,ssh, redfish are working. 158 if self.ssh_to_target_system(): 159 working_protocol_list.append("SSH") 160 working_protocol_list.append("SCP") 161 # Verify top level directory exists for storage 162 self.validate_local_store(self.location) 163 self.inspect_target_machine_type() 164 print("\n\t---- Completed protocol pre-requisite check ----\n") 165 self.generate_ffdc(working_protocol_list) 166 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() 178 print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname) 179 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() 184 return True 185 186 def generate_ffdc(self, working_protocol_list): 187 r""" 188 Determine actions based on remote host type 189 190 Description of argument(s): 191 working_protocol_list list of confirmed working protocols to connect to remote host. 192 """ 193 194 print("\n\t---- Executing commands on " + self.hostname + " ----") 195 print("\n\tWorking protocol list: %s" % working_protocol_list) 196 with open(self.ffdc_config, 'r') as file: 197 ffdc_actions = yaml.load(file, Loader=yaml.FullLoader) 198 199 # 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 206 for machine_type in ffdc_actions.keys(): 207 208 if machine_type == self.target_type: 209 if (ffdc_actions[machine_type]['PROTOCOL'][0] in working_protocol_list): 210 211 # 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 self.group_copy(ffdc_actions['OPENBMC_DUMPS']) 217 218 # For RHEL and UBUNTU, collect common Linux OS FFDC. 219 if self.target_type == 'RHEL' \ 220 or self.target_type == 'UBUNTU': 221 222 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]) 226 else: 227 print("\n\tProtocol %s is not yet supported by this script.\n" 228 % ffdc_actions[machine_type]['PROTOCOL'][0]) 229 230 # Close network connection after collecting all files 231 self.remoteclient.ssh_remoteclient_disconnect() 232 233 def collect_and_copy_ffdc(self, 234 ffdc_actions_for_machine_type, 235 form_filename=False): 236 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. 241 form_filename if true, pre-pend self.target_type to filename 242 """ 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: 249 if form_filename: 250 command = str(command % self.target_type) 251 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) 259 260 # Retrieving files from target system 261 list_of_files = ffdc_actions_for_machine_type['FILES'] 262 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files) 263 else: 264 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname) 265 266 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: 276 print("\n\tCopying DUMP files from remote system %s.\n" % self.hostname) 277 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 296 def scp_ffdc(self, 297 targ_dir_path, 298 targ_file_prefix, 299 form_filename, 300 file_list=None, 301 quiet=None): 302 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 313 progress_counter = 0 314 for filename in file_list: 315 if form_filename: 316 filename = str(filename % self.target_type) 317 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) 323 324 if not quiet: 325 if scp_result: 326 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n") 327 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 333 def set_ffdc_defaults(self): 334 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) 383