Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | See class prolog below for details. |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import sys |
| 9 | import yaml |
| 10 | import time |
| 11 | import platform |
| 12 | from errno import EACCES, EPERM |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 13 | import subprocess |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 14 | from ssh_utility import SSHRemoteclient |
| 15 | |
| 16 | |
| 17 | class FFDCCollector: |
| 18 | |
| 19 | r""" |
| 20 | Sends commands from configuration file to the targeted system to collect log files. |
| 21 | Fetch and store generated files at the specified location. |
| 22 | |
| 23 | """ |
| 24 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 25 | # List of supported OSes. |
| 26 | supported_oses = ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU'] |
| 27 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 28 | def __init__(self, |
| 29 | hostname, |
| 30 | username, |
| 31 | password, |
| 32 | ffdc_config, |
| 33 | location, |
| 34 | remote_type, |
| 35 | remote_protocol): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 36 | r""" |
| 37 | Description of argument(s): |
| 38 | |
| 39 | hostname name/ip of the targeted (remote) system |
| 40 | username user on the targeted system with access to FFDC files |
| 41 | password password for user on targeted system |
| 42 | ffdc_config configuration file listing commands and files for FFDC |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 43 | location where to store collected FFDC |
| 44 | remote_type os type of the remote host |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 45 | |
| 46 | """ |
| 47 | if self.verify_script_env(): |
| 48 | self.hostname = hostname |
| 49 | self.username = username |
| 50 | self.password = password |
| 51 | self.ffdc_config = ffdc_config |
| 52 | self.location = location |
| 53 | self.remote_client = None |
| 54 | self.ffdc_dir_path = "" |
| 55 | self.ffdc_prefix = "" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 56 | self.target_type = remote_type.upper() |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 57 | self.remote_protocol = remote_protocol.upper() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 58 | else: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 59 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 60 | |
| 61 | def verify_script_env(self): |
| 62 | |
| 63 | # Import to log version |
| 64 | import click |
| 65 | import paramiko |
| 66 | |
| 67 | run_env_ok = True |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 68 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 69 | redfishtool_version = self.run_redfishtool('-V').split(' ')[2].strip('\n') |
| 70 | ipmitool_version = self.run_ipmitool('-V').split(' ')[2] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 71 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 72 | print("\n\t---- Script host environment ----") |
| 73 | print("\t{:<10} {:<10}".format('Script hostname', os.uname()[1])) |
| 74 | print("\t{:<10} {:<10}".format('Script host os', platform.platform())) |
| 75 | print("\t{:<10} {:>10}".format('Python', platform.python_version())) |
| 76 | print("\t{:<10} {:>10}".format('PyYAML', yaml.__version__)) |
| 77 | print("\t{:<10} {:>10}".format('click', click.__version__)) |
| 78 | print("\t{:<10} {:>10}".format('paramiko', paramiko.__version__)) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 79 | print("\t{:<10} {:>9}".format('redfishtool', redfishtool_version)) |
| 80 | print("\t{:<10} {:>12}".format('ipmitool', ipmitool_version)) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 81 | |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 82 | if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1): |
| 83 | print("\n\tERROR: Python or python packages do not meet minimum version requirement.") |
| 84 | print("\tERROR: PyYAML version 5.4.1 or higher is needed.\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 85 | run_env_ok = False |
| 86 | |
| 87 | print("\t---- End script host environment ----") |
| 88 | return run_env_ok |
| 89 | |
| 90 | def target_is_pingable(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 91 | r""" |
| 92 | Check if target system is ping-able. |
| 93 | |
| 94 | """ |
| 95 | response = os.system("ping -c 1 -w 2 %s 2>&1 >/dev/null" % self.hostname) |
| 96 | if response == 0: |
George Keishing | 615fe32 | 2021-06-24 01:24:36 -0500 | [diff] [blame] | 97 | print("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 98 | return True |
| 99 | else: |
| 100 | print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname) |
| 101 | sys.exit(-1) |
| 102 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 103 | def inspect_target_machine_type(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 104 | r""" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 105 | Inspect remote host os-release or uname. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 106 | |
| 107 | """ |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 108 | command = "cat /etc/os-release" |
| 109 | response = self.remoteclient.execute_command(command) |
| 110 | if response: |
| 111 | print("\n\t[INFO] %s /etc/os-release\n" % self.hostname) |
Peter D Phan | 0c42a94 | 2021-06-29 09:17:27 -0500 | [diff] [blame] | 112 | for each_info in response: |
| 113 | print("\t\t %s" % each_info) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 114 | identity = self.find_os_type(response, 'ID').split('=')[1].upper() |
| 115 | else: |
| 116 | response = self.remoteclient.execute_command('uname -a') |
| 117 | print("\n\t[INFO] %s uname -a\n" % self.hostname) |
| 118 | print("\t\t %s" % ' '.join(response)) |
| 119 | identity = self.find_os_type(response, 'AIX').split(' ')[0].upper() |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 120 | |
| 121 | # If OS does not have /etc/os-release and is not AIX, |
| 122 | # script does not yet know what to do. |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 123 | if not identity: |
| 124 | print(">>>>>\tERROR: Script does not yet know about %s" % ' '.join(response)) |
| 125 | sys.exit(-1) |
| 126 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 127 | if (self.target_type not in identity): |
| 128 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 129 | user_target_type = self.target_type |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 130 | self.target_type = "" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 131 | for each_os in FFDCCollector.supported_oses: |
| 132 | if each_os in identity: |
| 133 | self.target_type = each_os |
| 134 | break |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 135 | |
| 136 | # If OS in not one of ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU'] |
| 137 | # script does not yet know what to do. |
| 138 | if not self.target_type: |
| 139 | print(">>>>>\tERROR: Script does not yet know about %s" % identity) |
| 140 | sys.exit(-1) |
| 141 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 142 | print("\n\t[WARN] user request %s does not match remote host type %s.\n" |
| 143 | % (user_target_type, self.target_type)) |
| 144 | print("\t[WARN] FFDC collection continues for %s.\n" % self.target_type) |
| 145 | |
| 146 | def find_os_type(self, |
| 147 | listing_from_os, |
| 148 | key): |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 149 | r""" |
| 150 | Return OS information with the requested key |
| 151 | |
| 152 | Description of argument(s): |
| 153 | |
| 154 | listing_from_os list of information returns from OS command |
| 155 | key key of the desired data |
| 156 | |
| 157 | """ |
| 158 | |
| 159 | for each_item in listing_from_os: |
| 160 | if key in each_item: |
| 161 | return each_item |
| 162 | return '' |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 163 | |
| 164 | def collect_ffdc(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 165 | r""" |
| 166 | Initiate FFDC Collection depending on requested protocol. |
| 167 | |
| 168 | """ |
| 169 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 170 | print("\n\t---- Start communicating with %s ----" % self.hostname) |
| 171 | working_protocol_list = [] |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 172 | if self.target_is_pingable(): |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 173 | # Check supported protocol ping,ssh, redfish are working. |
| 174 | if self.ssh_to_target_system(): |
| 175 | working_protocol_list.append("SSH") |
George Keishing | 615fe32 | 2021-06-24 01:24:36 -0500 | [diff] [blame] | 176 | working_protocol_list.append("SCP") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 177 | |
| 178 | # Redfish |
| 179 | if self.verify_redfish(): |
| 180 | working_protocol_list.append("REDFISH") |
| 181 | print("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname) |
| 182 | else: |
| 183 | print("\n\t[Check] %s Redfish Service.\t\t [FAILED]" % self.hostname) |
| 184 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 185 | if self.verify_ipmi(): |
| 186 | working_protocol_list.append("IPMI") |
| 187 | print("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname) |
| 188 | else: |
| 189 | print("\n\t[Check] %s IPMI LAN Service.\t\t [FAILED]" % self.hostname) |
| 190 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 191 | # Verify top level directory exists for storage |
| 192 | self.validate_local_store(self.location) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 193 | self.inspect_target_machine_type() |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 194 | print("\n\t---- Completed protocol pre-requisite check ----\n") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 195 | |
| 196 | if ((self.remote_protocol not in working_protocol_list) and (self.remote_protocol != 'ALL')): |
| 197 | print("\n\tWorking protocol list: %s" % working_protocol_list) |
| 198 | print( |
| 199 | '>>>>>\tERROR: Requested protocol %s is not in working protocol list.\n' |
| 200 | % self.remote_protocol) |
| 201 | sys.exit(-1) |
| 202 | else: |
| 203 | self.generate_ffdc(working_protocol_list) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 204 | |
| 205 | def ssh_to_target_system(self): |
| 206 | r""" |
| 207 | Open a ssh connection to targeted system. |
| 208 | |
| 209 | """ |
| 210 | |
| 211 | self.remoteclient = SSHRemoteclient(self.hostname, |
| 212 | self.username, |
| 213 | self.password) |
| 214 | |
| 215 | self.remoteclient.ssh_remoteclient_login() |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 216 | print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 217 | |
| 218 | # Check scp connection. |
| 219 | # If scp connection fails, |
| 220 | # continue with FFDC generation but skip scp files to local host. |
| 221 | self.remoteclient.scp_connection() |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 222 | return True |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 223 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 224 | def generate_ffdc(self, working_protocol_list): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 225 | r""" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 226 | Determine actions based on remote host type |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 227 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 228 | Description of argument(s): |
| 229 | working_protocol_list list of confirmed working protocols to connect to remote host. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 230 | """ |
| 231 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 232 | print("\n\t---- Executing commands on " + self.hostname + " ----") |
| 233 | print("\n\tWorking protocol list: %s" % working_protocol_list) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 234 | with open(self.ffdc_config, 'r') as file: |
| 235 | ffdc_actions = yaml.load(file, Loader=yaml.FullLoader) |
| 236 | |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 237 | # Set prefix values for scp files and directory. |
| 238 | # Since the time stamp is at second granularity, these values are set here |
| 239 | # to be sure that all files for this run will have same timestamps |
| 240 | # and they will be saved in the same directory. |
| 241 | # self.location == local system for now |
| 242 | self.set_ffdc_defaults() |
| 243 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 244 | for machine_type in ffdc_actions.keys(): |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 245 | if self.target_type != machine_type: |
| 246 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 247 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 248 | print("\tSystem Type: %s" % machine_type) |
| 249 | for k, v in ffdc_actions[machine_type].items(): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 250 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 251 | if self.remote_protocol != ffdc_actions[machine_type][k]['PROTOCOL'][0] \ |
| 252 | and self.remote_protocol != 'ALL': |
| 253 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 254 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 255 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SSH': |
| 256 | self.protocol_ssh(ffdc_actions, machine_type, k) |
| 257 | |
| 258 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'REDFISH': |
| 259 | self.protocol_redfish(ffdc_actions, machine_type, k) |
| 260 | |
| 261 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'IPMI': |
| 262 | self.protocol_ipmi(ffdc_actions, machine_type, k) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 263 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 264 | # Close network connection after collecting all files |
| 265 | self.remoteclient.ssh_remoteclient_disconnect() |
| 266 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 267 | def protocol_ssh(self, |
| 268 | ffdc_actions, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 269 | machine_type, |
| 270 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 271 | r""" |
| 272 | Perform actions using SSH and SCP protocols. |
| 273 | |
| 274 | Description of argument(s): |
| 275 | ffdc_actions List of actions from ffdc_config.yaml. |
| 276 | machine_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 277 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 278 | """ |
| 279 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 280 | if sub_type == 'DUMP_LOGS': |
| 281 | self.group_copy(ffdc_actions[machine_type][sub_type]) |
| 282 | else: |
| 283 | self.collect_and_copy_ffdc(ffdc_actions[machine_type][sub_type]) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 284 | |
| 285 | def protocol_redfish(self, |
| 286 | ffdc_actions, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 287 | machine_type, |
| 288 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 289 | r""" |
| 290 | Perform actions using Redfish protocol. |
| 291 | |
| 292 | Description of argument(s): |
| 293 | ffdc_actions List of actions from ffdc_config.yaml. |
| 294 | machine_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 295 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 296 | """ |
| 297 | |
| 298 | print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH')) |
| 299 | redfish_files_saved = [] |
| 300 | progress_counter = 0 |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 301 | list_of_URL = ffdc_actions[machine_type][sub_type]['URL'] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 302 | for index, each_url in enumerate(list_of_URL, start=0): |
| 303 | redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \ |
| 304 | + self.hostname + ' -S Always raw GET ' + each_url |
| 305 | |
| 306 | result = self.run_redfishtool(redfish_parm) |
| 307 | if result: |
| 308 | try: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 309 | targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 310 | except IndexError: |
| 311 | targ_file = each_url.split('/')[-1] |
| 312 | print("\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url) |
| 313 | print("\t[WARN] Data will be stored in %s." % targ_file) |
| 314 | |
| 315 | targ_file_with_path = (self.ffdc_dir_path |
| 316 | + self.ffdc_prefix |
| 317 | + targ_file) |
| 318 | |
| 319 | # Creates a new file |
| 320 | with open(targ_file_with_path, 'w') as fp: |
| 321 | fp.write(result) |
| 322 | fp.close |
| 323 | redfish_files_saved.append(targ_file) |
| 324 | |
| 325 | progress_counter += 1 |
| 326 | self.print_progress(progress_counter) |
| 327 | |
| 328 | print("\n\t[Run] Commands execution completed.\t\t [OK]") |
| 329 | |
| 330 | for file in redfish_files_saved: |
| 331 | print("\n\t\tSuccessfully save file " + file + ".") |
| 332 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 333 | def protocol_ipmi(self, |
| 334 | ffdc_actions, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 335 | machine_type, |
| 336 | sub_type): |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 337 | r""" |
| 338 | Perform actions using ipmitool over LAN protocol. |
| 339 | |
| 340 | Description of argument(s): |
| 341 | ffdc_actions List of actions from ffdc_config.yaml. |
| 342 | machine_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 343 | sub_type Group type of commands. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 344 | """ |
| 345 | |
| 346 | print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI')) |
| 347 | ipmi_files_saved = [] |
| 348 | progress_counter = 0 |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 349 | list_of_cmd = ffdc_actions[machine_type][sub_type]['COMMANDS'] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 350 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
| 351 | ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \ |
| 352 | + self.hostname + ' ' + each_cmd |
| 353 | |
| 354 | result = self.run_ipmitool(ipmi_parm) |
| 355 | if result: |
| 356 | try: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 357 | targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 358 | except IndexError: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame^] | 359 | targ_file = each_cmd.split('/')[-1] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 360 | print("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd) |
| 361 | print("\t[WARN] Data will be stored in %s." % targ_file) |
| 362 | |
| 363 | targ_file_with_path = (self.ffdc_dir_path |
| 364 | + self.ffdc_prefix |
| 365 | + targ_file) |
| 366 | |
| 367 | # Creates a new file |
| 368 | with open(targ_file_with_path, 'w') as fp: |
| 369 | fp.write(result) |
| 370 | fp.close |
| 371 | ipmi_files_saved.append(targ_file) |
| 372 | |
| 373 | progress_counter += 1 |
| 374 | self.print_progress(progress_counter) |
| 375 | |
| 376 | print("\n\t[Run] Commands execution completed.\t\t [OK]") |
| 377 | |
| 378 | for file in ipmi_files_saved: |
| 379 | print("\n\t\tSuccessfully save file " + file + ".") |
| 380 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 381 | def collect_and_copy_ffdc(self, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 382 | ffdc_actions_for_machine_type, |
| 383 | form_filename=False): |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 384 | r""" |
| 385 | Send commands in ffdc_config file to targeted system. |
| 386 | |
| 387 | Description of argument(s): |
| 388 | ffdc_actions_for_machine_type commands and files for the selected remote host type. |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 389 | form_filename if true, pre-pend self.target_type to filename |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 390 | """ |
| 391 | |
| 392 | print("\n\t[Run] Executing commands on %s using %s" |
| 393 | % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0])) |
| 394 | list_of_commands = ffdc_actions_for_machine_type['COMMANDS'] |
| 395 | progress_counter = 0 |
| 396 | for command in list_of_commands: |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 397 | if form_filename: |
| 398 | command = str(command % self.target_type) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 399 | self.remoteclient.execute_command(command) |
| 400 | progress_counter += 1 |
| 401 | self.print_progress(progress_counter) |
| 402 | |
| 403 | print("\n\t[Run] Commands execution completed.\t\t [OK]") |
| 404 | |
| 405 | if self.remoteclient.scpclient: |
| 406 | print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname) |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 407 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 408 | # Retrieving files from target system |
| 409 | list_of_files = ffdc_actions_for_machine_type['FILES'] |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 410 | self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 411 | else: |
| 412 | print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname) |
| 413 | |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 414 | def group_copy(self, |
| 415 | ffdc_actions_for_machine_type): |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 416 | r""" |
| 417 | scp group of files (wild card) from remote host. |
| 418 | |
| 419 | Description of argument(s): |
| 420 | ffdc_actions_for_machine_type commands and files for the selected remote host type. |
| 421 | """ |
| 422 | if self.remoteclient.scpclient: |
George Keishing | 615fe32 | 2021-06-24 01:24:36 -0500 | [diff] [blame] | 423 | print("\n\tCopying DUMP files from remote system %s.\n" % self.hostname) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 424 | |
| 425 | # Retrieving files from target system, if any |
| 426 | list_of_files = ffdc_actions_for_machine_type['FILES'] |
| 427 | |
| 428 | for filename in list_of_files: |
| 429 | command = 'ls -AX ' + filename |
| 430 | response = self.remoteclient.execute_command(command) |
| 431 | # self.remoteclient.scp_file_from_remote() completed without exception, |
| 432 | # if any |
| 433 | if response: |
| 434 | scp_result = self.remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path) |
| 435 | if scp_result: |
| 436 | print("\t\tSuccessfully copied from " + self.hostname + ':' + filename) |
| 437 | else: |
| 438 | print("\t\tThere is no " + filename) |
| 439 | |
| 440 | else: |
| 441 | print("\n\n\tSkip copying files from remote system %s.\n" % self.hostname) |
| 442 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 443 | def scp_ffdc(self, |
| 444 | targ_dir_path, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 445 | targ_file_prefix, |
| 446 | form_filename, |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 447 | file_list=None, |
| 448 | quiet=None): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 449 | r""" |
| 450 | SCP all files in file_dict to the indicated directory on the local system. |
| 451 | |
| 452 | Description of argument(s): |
| 453 | targ_dir_path The path of the directory to receive the files. |
| 454 | targ_file_prefix Prefix which will be pre-pended to each |
| 455 | target file's name. |
| 456 | file_dict A dictionary of files to scp from targeted system to this system |
| 457 | |
| 458 | """ |
| 459 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 460 | progress_counter = 0 |
| 461 | for filename in file_list: |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 462 | if form_filename: |
| 463 | filename = str(filename % self.target_type) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 464 | source_file_path = filename |
| 465 | targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1] |
| 466 | |
| 467 | # self.remoteclient.scp_file_from_remote() completed without exception, |
| 468 | # add file to the receiving file list. |
| 469 | scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 470 | |
| 471 | if not quiet: |
| 472 | if scp_result: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 473 | print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 474 | else: |
| 475 | print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n") |
| 476 | else: |
| 477 | progress_counter += 1 |
| 478 | self.print_progress(progress_counter) |
| 479 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 480 | def set_ffdc_defaults(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 481 | r""" |
| 482 | Set a default value for self.ffdc_dir_path and self.ffdc_prefix. |
| 483 | Collected ffdc file will be stored in dir /self.location/hostname_timestr/. |
| 484 | Individual ffdc file will have timestr_filename. |
| 485 | |
| 486 | Description of class variables: |
| 487 | self.ffdc_dir_path The dir path where collected ffdc data files should be put. |
| 488 | |
| 489 | self.ffdc_prefix The prefix to be given to each ffdc file name. |
| 490 | |
| 491 | """ |
| 492 | |
| 493 | timestr = time.strftime("%Y%m%d-%H%M%S") |
| 494 | self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/" |
| 495 | self.ffdc_prefix = timestr + "_" |
| 496 | self.validate_local_store(self.ffdc_dir_path) |
| 497 | |
| 498 | def validate_local_store(self, dir_path): |
| 499 | r""" |
| 500 | Ensure path exists to store FFDC files locally. |
| 501 | |
| 502 | Description of variable: |
| 503 | dir_path The dir path where collected ffdc data files will be stored. |
| 504 | |
| 505 | """ |
| 506 | |
| 507 | if not os.path.exists(dir_path): |
| 508 | try: |
| 509 | os.mkdir(dir_path, 0o755) |
| 510 | except (IOError, OSError) as e: |
| 511 | # PermissionError |
| 512 | if e.errno == EPERM or e.errno == EACCES: |
| 513 | print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path) |
| 514 | else: |
| 515 | print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror)) |
| 516 | sys.exit(-1) |
| 517 | |
| 518 | def print_progress(self, progress): |
| 519 | r""" |
| 520 | Print activity progress + |
| 521 | |
| 522 | Description of variable: |
| 523 | progress Progress counter. |
| 524 | |
| 525 | """ |
| 526 | |
| 527 | sys.stdout.write("\r\t" + "+" * progress) |
| 528 | sys.stdout.flush() |
| 529 | time.sleep(.1) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 530 | |
| 531 | def verify_redfish(self): |
| 532 | r""" |
| 533 | Verify remote host has redfish service active |
| 534 | |
| 535 | """ |
| 536 | redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \ |
| 537 | + self.hostname + ' -S Always raw GET /redfish/v1/' |
| 538 | return(self.run_redfishtool(redfish_parm, True)) |
| 539 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 540 | def verify_ipmi(self): |
| 541 | r""" |
| 542 | Verify remote host has IPMI LAN service active |
| 543 | |
| 544 | """ |
| 545 | ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \ |
| 546 | + self.hostname + ' power status' |
| 547 | return(self.run_ipmitool(ipmi_parm, True)) |
| 548 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 549 | def run_redfishtool(self, |
| 550 | parms_string, |
| 551 | quiet=False): |
| 552 | r""" |
| 553 | Run CLI redfishtool |
| 554 | |
| 555 | Description of variable: |
| 556 | parms_string redfishtool subcommand and options. |
| 557 | quiet do not print redfishtool error message if True |
| 558 | """ |
| 559 | |
| 560 | result = subprocess.run(['redfishtool ' + parms_string], |
| 561 | stdout=subprocess.PIPE, |
| 562 | stderr=subprocess.PIPE, |
| 563 | shell=True, |
| 564 | universal_newlines=True) |
| 565 | |
| 566 | if result.stderr and not quiet: |
| 567 | print('\n\t\tERROR with redfishtool ' + parms_string) |
| 568 | print('\t\t' + result.stderr) |
| 569 | |
| 570 | return result.stdout |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 571 | |
| 572 | def run_ipmitool(self, |
| 573 | parms_string, |
| 574 | quiet=False): |
| 575 | r""" |
| 576 | Run CLI IPMI tool. |
| 577 | |
| 578 | Description of variable: |
| 579 | parms_string ipmitool subcommand and options. |
| 580 | quiet do not print redfishtool error message if True |
| 581 | """ |
| 582 | |
| 583 | result = subprocess.run(['ipmitool -I lanplus -C 17 ' + parms_string], |
| 584 | stdout=subprocess.PIPE, |
| 585 | stderr=subprocess.PIPE, |
| 586 | shell=True, |
| 587 | universal_newlines=True) |
| 588 | |
| 589 | if result.stderr and not quiet: |
| 590 | print('\n\t\tERROR with ipmitool -I lanplus -C 17 ' + parms_string) |
| 591 | print('\t\t' + result.stderr) |
| 592 | |
| 593 | return result.stdout |