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.receive_file_list = [] 49 self.target_type = remote_type.upper() 50 else: 51 sys.exit(-1) 52 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 68 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") 71 run_env_ok = False 72 73 print("\t---- End script host environment ----") 74 return run_env_ok 75 76 def target_is_pingable(self): 77 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: 83 print("\n\t[Check] %s is ping-able.\t\t\t [OK]" % self.hostname) 84 return True 85 else: 86 print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname) 87 sys.exit(-1) 88 89 def inspect_target_machine_type(self): 90 r""" 91 Inspect remote host os-release or uname. 92 93 """ 94 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() 105 106 # If OS does not have /etc/os-release and is not AIX, 107 # script does not yet know what to do. 108 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 114 self.target_type = "" 115 for each_os in FFDCCollector.supported_oses: 116 if each_os in identity: 117 self.target_type = each_os 118 break 119 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 126 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 '' 148 149 def collect_ffdc(self): 150 r""" 151 Initiate FFDC Collection depending on requested protocol. 152 153 """ 154 155 print("\n\t---- Start communicating with %s ----" % self.hostname) 156 working_protocol_list = [] 157 if self.target_is_pingable(): 158 # Check supported protocol ping,ssh, redfish are working. 159 if self.ssh_to_target_system(): 160 working_protocol_list.append("SSH") 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 217 # For RHEL and UBUNTU, collect common Linux OS FFDC. 218 if self.target_type == 'RHEL' \ 219 or self.target_type == 'UBUNTU': 220 221 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]) 225 else: 226 print("\n\tProtocol %s is not yet supported by this script.\n" 227 % ffdc_actions[machine_type]['PROTOCOL'][0]) 228 229 # Close network connection after collecting all files 230 self.remoteclient.ssh_remoteclient_disconnect() 231 232 def collect_and_copy_ffdc(self, 233 ffdc_actions_for_machine_type, 234 form_filename=False): 235 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. 240 form_filename if true, pre-pend self.target_type to filename 241 """ 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: 248 if form_filename: 249 command = str(command % self.target_type) 250 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) 258 259 # Retrieving files from target system 260 list_of_files = ffdc_actions_for_machine_type['FILES'] 261 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files) 262 else: 263 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname) 264 265 def scp_ffdc(self, 266 targ_dir_path, 267 targ_file_prefix, 268 form_filename, 269 file_list=None, 270 quiet=None): 271 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 282 progress_counter = 0 283 for filename in file_list: 284 if form_filename: 285 filename = str(filename % self.target_type) 286 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: 297 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n") 298 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 304 def set_ffdc_defaults(self): 305 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) 354