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 |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 10 | import json |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 11 | import time |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 12 | import logging |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 13 | import platform |
| 14 | from errno import EACCES, EPERM |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 15 | import subprocess |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 16 | from ssh_utility import SSHRemoteclient |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 17 | from telnet_utility import TelnetRemoteclient |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 18 | |
| 19 | |
| 20 | class FFDCCollector: |
| 21 | |
| 22 | r""" |
| 23 | Sends commands from configuration file to the targeted system to collect log files. |
| 24 | Fetch and store generated files at the specified location. |
| 25 | |
| 26 | """ |
| 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, |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 35 | remote_protocol, |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 36 | env_vars, |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 37 | log_level): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 38 | r""" |
| 39 | Description of argument(s): |
| 40 | |
| 41 | hostname name/ip of the targeted (remote) system |
| 42 | username user on the targeted system with access to FFDC files |
| 43 | password password for user on targeted system |
| 44 | ffdc_config configuration file listing commands and files for FFDC |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 45 | location where to store collected FFDC |
| 46 | remote_type os type of the remote host |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 47 | |
| 48 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 49 | |
| 50 | self.hostname = hostname |
| 51 | self.username = username |
| 52 | self.password = password |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 53 | # This is for the env vars a user can use in YAML to load it at runtime. |
| 54 | # Example YAML: |
| 55 | # -COMMANDS: |
| 56 | # - my_command ${hostname} ${username} ${password} |
| 57 | os.environ['hostname'] = hostname |
| 58 | os.environ['username'] = username |
| 59 | os.environ['password'] = password |
| 60 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 61 | self.ffdc_config = ffdc_config |
| 62 | self.location = location + "/" + remote_type.upper() |
| 63 | self.ssh_remoteclient = None |
| 64 | self.telnet_remoteclient = None |
| 65 | self.ffdc_dir_path = "" |
| 66 | self.ffdc_prefix = "" |
| 67 | self.target_type = remote_type.upper() |
| 68 | self.remote_protocol = remote_protocol.upper() |
| 69 | self.start_time = 0 |
| 70 | self.elapsed_time = '' |
| 71 | self.logger = None |
| 72 | |
| 73 | # Set prefix values for scp files and directory. |
| 74 | # Since the time stamp is at second granularity, these values are set here |
| 75 | # to be sure that all files for this run will have same timestamps |
| 76 | # and they will be saved in the same directory. |
| 77 | # self.location == local system for now |
| 78 | self.set_ffdc_defaults() |
| 79 | |
| 80 | # Logger for this run. Need to be after set_ffdc_defaults() |
| 81 | self.script_logging(getattr(logging, log_level.upper())) |
| 82 | |
| 83 | # Verify top level directory exists for storage |
| 84 | self.validate_local_store(self.location) |
| 85 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 86 | if self.verify_script_env(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 87 | # Load default or user define YAML configuration file. |
| 88 | with open(self.ffdc_config, 'r') as file: |
| 89 | self.ffdc_actions = yaml.load(file, Loader=yaml.FullLoader) |
| 90 | |
| 91 | if self.target_type not in self.ffdc_actions.keys(): |
| 92 | self.logger.error( |
| 93 | "\n\tERROR: %s is not listed in %s.\n\n" % (self.target_type, self.ffdc_config)) |
| 94 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 95 | else: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 96 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 97 | |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 98 | # Load ENV vars from user. |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 99 | self.logger.info("\n\tENV: User define input YAML variables") |
| 100 | self.env_dict = {} |
| 101 | |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 102 | try: |
| 103 | if env_vars: |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 104 | self.env_dict = json.loads(env_vars) |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 105 | |
| 106 | # Export ENV vars default. |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 107 | for key, value in self.env_dict.items(): |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 108 | os.environ[key] = value |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 109 | |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 110 | except json.decoder.JSONDecodeError as e: |
| 111 | self.logger.error("\n\tERROR: %s " % e) |
| 112 | sys.exit(-1) |
| 113 | |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 114 | # Append default Env. |
| 115 | self.env_dict['hostname'] = self.hostname |
| 116 | self.env_dict['username'] = self.username |
| 117 | self.env_dict['password'] = self.password |
| 118 | self.logger.info(json.dumps(self.env_dict, indent=8, sort_keys=True)) |
| 119 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 120 | def verify_script_env(self): |
| 121 | |
| 122 | # Import to log version |
| 123 | import click |
| 124 | import paramiko |
| 125 | |
| 126 | run_env_ok = True |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 127 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 128 | redfishtool_version = self.run_redfishtool('-V').split(' ')[2].strip('\n') |
| 129 | ipmitool_version = self.run_ipmitool('-V').split(' ')[2] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 130 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 131 | self.logger.info("\n\t---- Script host environment ----") |
| 132 | self.logger.info("\t{:<10} {:<10}".format('Script hostname', os.uname()[1])) |
| 133 | self.logger.info("\t{:<10} {:<10}".format('Script host os', platform.platform())) |
| 134 | self.logger.info("\t{:<10} {:>10}".format('Python', platform.python_version())) |
| 135 | self.logger.info("\t{:<10} {:>10}".format('PyYAML', yaml.__version__)) |
| 136 | self.logger.info("\t{:<10} {:>10}".format('click', click.__version__)) |
| 137 | self.logger.info("\t{:<10} {:>10}".format('paramiko', paramiko.__version__)) |
| 138 | self.logger.info("\t{:<10} {:>9}".format('redfishtool', redfishtool_version)) |
| 139 | self.logger.info("\t{:<10} {:>12}".format('ipmitool', ipmitool_version)) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 140 | |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 141 | if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 142 | self.logger.error("\n\tERROR: Python or python packages do not meet minimum version requirement.") |
| 143 | self.logger.error("\tERROR: PyYAML version 5.4.1 or higher is needed.\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 144 | run_env_ok = False |
| 145 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 146 | self.logger.info("\t---- End script host environment ----") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 147 | return run_env_ok |
| 148 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 149 | def script_logging(self, |
| 150 | log_level_attr): |
| 151 | r""" |
| 152 | Create logger |
| 153 | |
| 154 | """ |
| 155 | self.logger = logging.getLogger() |
| 156 | self.logger.setLevel(log_level_attr) |
| 157 | log_file_handler = logging.FileHandler(self.ffdc_dir_path + "collector.log") |
| 158 | |
| 159 | stdout_handler = logging.StreamHandler(sys.stdout) |
| 160 | self.logger.addHandler(log_file_handler) |
| 161 | self.logger.addHandler(stdout_handler) |
| 162 | |
| 163 | # Turn off paramiko INFO logging |
| 164 | logging.getLogger("paramiko").setLevel(logging.WARNING) |
| 165 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 166 | def target_is_pingable(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 167 | r""" |
| 168 | Check if target system is ping-able. |
| 169 | |
| 170 | """ |
George Keishing | 0662e94 | 2021-07-13 05:12:20 -0500 | [diff] [blame] | 171 | response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 172 | if response == 0: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 173 | self.logger.info("\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] | 174 | return True |
| 175 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 176 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 177 | "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 178 | sys.exit(-1) |
| 179 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 180 | def collect_ffdc(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 181 | r""" |
| 182 | Initiate FFDC Collection depending on requested protocol. |
| 183 | |
| 184 | """ |
| 185 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 186 | self.logger.info("\n\t---- Start communicating with %s ----" % self.hostname) |
Peter D Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 187 | self.start_time = time.time() |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 188 | working_protocol_list = [] |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 189 | if self.target_is_pingable(): |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 190 | working_protocol_list.append("SHELL") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 191 | |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 192 | for machine_type in self.ffdc_actions.keys(): |
| 193 | if self.target_type != machine_type: |
| 194 | continue |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 195 | |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 196 | for k, v in self.ffdc_actions[machine_type].items(): |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 197 | |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 198 | # If config protocol is SSH or SCP |
| 199 | if (self.ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SSH' |
| 200 | or self.ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SCP') \ |
| 201 | and (self.remote_protocol == 'ALL' |
| 202 | or self.remote_protocol == 'SSH' or self.remote_protocol == 'SCP'): |
| 203 | |
| 204 | # Only check SSH/SCP once for both protocols |
| 205 | if 'SSH' not in working_protocol_list \ |
| 206 | and 'SCP' not in working_protocol_list: |
| 207 | if self.ssh_to_target_system(): |
| 208 | working_protocol_list.append("SSH") |
| 209 | working_protocol_list.append("SCP") |
| 210 | |
| 211 | # If config protocol is TELNET |
| 212 | if (self.ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'TELNET') \ |
| 213 | and (self.remote_protocol == 'ALL' or self.remote_protocol == 'TELNET'): |
| 214 | if self.telnet_to_target_system(): |
| 215 | working_protocol_list.append("TELNET") |
| 216 | |
| 217 | # If config protocol is REDFISH |
| 218 | if (self.ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'REDFISH') \ |
| 219 | and (self.remote_protocol == 'ALL' or self.remote_protocol == 'REDFISH'): |
| 220 | if self.verify_redfish(): |
| 221 | working_protocol_list.append("REDFISH") |
| 222 | self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname) |
| 223 | else: |
| 224 | self.logger.info( |
| 225 | "\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname) |
| 226 | |
| 227 | # If config protocol is IPMI |
| 228 | if (self.ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'IPMI') \ |
| 229 | and (self.remote_protocol == 'ALL' or self.remote_protocol == 'IPMI'): |
| 230 | if self.verify_ipmi(): |
| 231 | working_protocol_list.append("IPMI") |
| 232 | self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname) |
| 233 | else: |
| 234 | self.logger.info( |
| 235 | "\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 236 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 237 | # Verify top level directory exists for storage |
| 238 | self.validate_local_store(self.location) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 239 | self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 240 | |
| 241 | if ((self.remote_protocol not in working_protocol_list) and (self.remote_protocol != 'ALL')): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 242 | self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list) |
| 243 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 244 | '\tERROR: Requested protocol %s is not in working protocol list.\n' |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 245 | % self.remote_protocol) |
| 246 | sys.exit(-1) |
| 247 | else: |
| 248 | self.generate_ffdc(working_protocol_list) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 249 | |
| 250 | def ssh_to_target_system(self): |
| 251 | r""" |
| 252 | Open a ssh connection to targeted system. |
| 253 | |
| 254 | """ |
| 255 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 256 | self.ssh_remoteclient = SSHRemoteclient(self.hostname, |
| 257 | self.username, |
| 258 | self.password) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 259 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 260 | if self.ssh_remoteclient.ssh_remoteclient_login(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 261 | self.logger.info("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 262 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 263 | # Check scp connection. |
| 264 | # If scp connection fails, |
| 265 | # continue with FFDC generation but skip scp files to local host. |
| 266 | self.ssh_remoteclient.scp_connection() |
| 267 | return True |
| 268 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 269 | self.logger.info("\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 270 | return False |
| 271 | |
| 272 | def telnet_to_target_system(self): |
| 273 | r""" |
| 274 | Open a telnet connection to targeted system. |
| 275 | """ |
| 276 | self.telnet_remoteclient = TelnetRemoteclient(self.hostname, |
| 277 | self.username, |
| 278 | self.password) |
| 279 | if self.telnet_remoteclient.tn_remoteclient_login(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 280 | self.logger.info("\n\t[Check] %s Telnet connection established.\t [OK]" % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 281 | return True |
| 282 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 283 | self.logger.info("\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 284 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 285 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 286 | def generate_ffdc(self, working_protocol_list): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 287 | r""" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 288 | Determine actions based on remote host type |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 289 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 290 | Description of argument(s): |
| 291 | 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] | 292 | """ |
| 293 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 294 | self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----") |
| 295 | self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 296 | |
George Keishing | 86764b5 | 2021-07-01 04:32:03 -0500 | [diff] [blame] | 297 | ffdc_actions = self.ffdc_actions |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 298 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 299 | for machine_type in ffdc_actions.keys(): |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 300 | if self.target_type != machine_type: |
| 301 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 302 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 303 | self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path) |
| 304 | self.logger.info("\tSystem Type: %s" % machine_type) |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 305 | for k, v in ffdc_actions[machine_type].items(): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 306 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 307 | if self.remote_protocol != ffdc_actions[machine_type][k]['PROTOCOL'][0] \ |
| 308 | and self.remote_protocol != 'ALL': |
| 309 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 310 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 311 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SSH' \ |
| 312 | or ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SCP': |
| 313 | if 'SSH' in working_protocol_list \ |
| 314 | or 'SCP' in working_protocol_list: |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 315 | self.protocol_ssh(ffdc_actions, machine_type, k) |
| 316 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 317 | self.logger.error("\n\tERROR: SSH or SCP is not available for %s." % self.hostname) |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 318 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 319 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'TELNET': |
| 320 | if 'TELNET' in working_protocol_list: |
| 321 | self.protocol_telnet(ffdc_actions, machine_type, k) |
| 322 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 323 | self.logger.error("\n\tERROR: TELNET is not available for %s." % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 324 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 325 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'REDFISH': |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 326 | if 'REDFISH' in working_protocol_list: |
| 327 | self.protocol_redfish(ffdc_actions, machine_type, k) |
| 328 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 329 | self.logger.error("\n\tERROR: REDFISH is not available for %s." % self.hostname) |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 330 | |
| 331 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'IPMI': |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 332 | if 'IPMI' in working_protocol_list: |
| 333 | self.protocol_ipmi(ffdc_actions, machine_type, k) |
| 334 | else: |
Peter D Phan | d1fccd3 | 2021-07-21 06:45:54 -0500 | [diff] [blame] | 335 | self.logger.error("\n\tERROR: IPMI is not available for %s." % self.hostname) |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 336 | |
| 337 | if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SHELL': |
| 338 | if 'SHELL' in working_protocol_list: |
| 339 | self.protocol_shell_script(ffdc_actions, machine_type, k) |
| 340 | else: |
| 341 | self.logger.error("\n\tERROR: can't execute SHELL script") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 342 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 343 | # Close network connection after collecting all files |
Peter D Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 344 | self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time)) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 345 | if self.ssh_remoteclient: |
| 346 | self.ssh_remoteclient.ssh_remoteclient_disconnect() |
| 347 | if self.telnet_remoteclient: |
| 348 | self.telnet_remoteclient.tn_remoteclient_disconnect() |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 349 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 350 | def protocol_ssh(self, |
| 351 | ffdc_actions, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 352 | machine_type, |
| 353 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 354 | r""" |
| 355 | Perform actions using SSH and SCP protocols. |
| 356 | |
| 357 | Description of argument(s): |
| 358 | ffdc_actions List of actions from ffdc_config.yaml. |
| 359 | machine_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 360 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 361 | """ |
| 362 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 363 | if sub_type == 'DUMP_LOGS': |
| 364 | self.group_copy(ffdc_actions[machine_type][sub_type]) |
| 365 | else: |
| 366 | self.collect_and_copy_ffdc(ffdc_actions[machine_type][sub_type]) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 367 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 368 | def protocol_telnet(self, |
| 369 | ffdc_actions, |
| 370 | machine_type, |
| 371 | sub_type): |
| 372 | r""" |
| 373 | Perform actions using telnet protocol. |
| 374 | Description of argument(s): |
| 375 | ffdc_actions List of actions from ffdc_config.yaml. |
| 376 | machine_type OS Type of remote host. |
| 377 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 378 | self.logger.info("\n\t[Run] Executing commands on %s using %s" % (self.hostname, 'TELNET')) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 379 | telnet_files_saved = [] |
| 380 | progress_counter = 0 |
| 381 | list_of_commands = ffdc_actions[machine_type][sub_type]['COMMANDS'] |
| 382 | for index, each_cmd in enumerate(list_of_commands, start=0): |
| 383 | command_txt, command_timeout = self.unpack_command(each_cmd) |
| 384 | result = self.telnet_remoteclient.execute_command(command_txt, command_timeout) |
| 385 | if result: |
| 386 | try: |
| 387 | targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index] |
| 388 | except IndexError: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 389 | targ_file = command_txt |
| 390 | self.logger.warning( |
| 391 | "\n\t[WARN] Missing filename to store data from telnet %s." % each_cmd) |
| 392 | self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 393 | targ_file_with_path = (self.ffdc_dir_path |
| 394 | + self.ffdc_prefix |
| 395 | + targ_file) |
| 396 | # Creates a new file |
| 397 | with open(targ_file_with_path, 'wb') as fp: |
| 398 | fp.write(result) |
| 399 | fp.close |
| 400 | telnet_files_saved.append(targ_file) |
| 401 | progress_counter += 1 |
| 402 | self.print_progress(progress_counter) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 403 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 404 | for file in telnet_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 405 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 406 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 407 | def protocol_redfish(self, |
| 408 | ffdc_actions, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 409 | machine_type, |
| 410 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 411 | r""" |
| 412 | Perform actions using Redfish protocol. |
| 413 | |
| 414 | Description of argument(s): |
| 415 | ffdc_actions List of actions from ffdc_config.yaml. |
| 416 | machine_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 417 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 418 | """ |
| 419 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 420 | self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH')) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 421 | redfish_files_saved = [] |
| 422 | progress_counter = 0 |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 423 | list_of_URL = ffdc_actions[machine_type][sub_type]['URL'] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 424 | for index, each_url in enumerate(list_of_URL, start=0): |
| 425 | redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \ |
| 426 | + self.hostname + ' -S Always raw GET ' + each_url |
| 427 | |
| 428 | result = self.run_redfishtool(redfish_parm) |
| 429 | if result: |
| 430 | try: |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 431 | targ_file = self.get_file_list(ffdc_actions[machine_type][sub_type])[index] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 432 | except IndexError: |
| 433 | targ_file = each_url.split('/')[-1] |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 434 | self.logger.warning( |
| 435 | "\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url) |
| 436 | self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 437 | |
| 438 | targ_file_with_path = (self.ffdc_dir_path |
| 439 | + self.ffdc_prefix |
| 440 | + targ_file) |
| 441 | |
| 442 | # Creates a new file |
| 443 | with open(targ_file_with_path, 'w') as fp: |
| 444 | fp.write(result) |
| 445 | fp.close |
| 446 | redfish_files_saved.append(targ_file) |
| 447 | |
| 448 | progress_counter += 1 |
| 449 | self.print_progress(progress_counter) |
| 450 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 451 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 452 | |
| 453 | for file in redfish_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 454 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 455 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 456 | def protocol_ipmi(self, |
| 457 | ffdc_actions, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 458 | machine_type, |
| 459 | sub_type): |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 460 | r""" |
| 461 | Perform actions using ipmitool over LAN protocol. |
| 462 | |
| 463 | Description of argument(s): |
| 464 | ffdc_actions List of actions from ffdc_config.yaml. |
| 465 | machine_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 466 | sub_type Group type of commands. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 467 | """ |
| 468 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 469 | self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI')) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 470 | ipmi_files_saved = [] |
| 471 | progress_counter = 0 |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 472 | list_of_cmd = self.get_command_list(ffdc_actions[machine_type][sub_type]) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 473 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
| 474 | ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \ |
Peter D Phan | d1fccd3 | 2021-07-21 06:45:54 -0500 | [diff] [blame] | 475 | + self.hostname + ' -I lanplus ' + each_cmd |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 476 | |
| 477 | result = self.run_ipmitool(ipmi_parm) |
| 478 | if result: |
| 479 | try: |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 480 | targ_file = self.get_file_list(ffdc_actions[machine_type][sub_type])[index] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 481 | except IndexError: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 482 | targ_file = each_cmd.split('/')[-1] |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 483 | self.logger.warning("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd) |
| 484 | self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 485 | |
| 486 | targ_file_with_path = (self.ffdc_dir_path |
| 487 | + self.ffdc_prefix |
| 488 | + targ_file) |
| 489 | |
| 490 | # Creates a new file |
| 491 | with open(targ_file_with_path, 'w') as fp: |
| 492 | fp.write(result) |
| 493 | fp.close |
| 494 | ipmi_files_saved.append(targ_file) |
| 495 | |
| 496 | progress_counter += 1 |
| 497 | self.print_progress(progress_counter) |
| 498 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 499 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 500 | |
| 501 | for file in ipmi_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 502 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 503 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 504 | def collect_and_copy_ffdc(self, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 505 | ffdc_actions_for_machine_type, |
| 506 | form_filename=False): |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 507 | r""" |
| 508 | Send commands in ffdc_config file to targeted system. |
| 509 | |
| 510 | Description of argument(s): |
| 511 | 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] | 512 | form_filename if true, pre-pend self.target_type to filename |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 513 | """ |
| 514 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 515 | # Executing commands, if any |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 516 | self.ssh_execute_ffdc_commands(ffdc_actions_for_machine_type, |
| 517 | form_filename) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 518 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 519 | # Copying files |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 520 | if self.ssh_remoteclient.scpclient: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 521 | self.logger.info("\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] | 522 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 523 | # Retrieving files from target system |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 524 | list_of_files = self.get_file_list(ffdc_actions_for_machine_type) |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 525 | 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] | 526 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 527 | self.logger.info("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 528 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 529 | def get_command_list(self, |
| 530 | ffdc_actions_for_machine_type): |
| 531 | r""" |
| 532 | Fetch list of commands from configuration file |
| 533 | |
| 534 | Description of argument(s): |
| 535 | ffdc_actions_for_machine_type commands and files for the selected remote host type. |
| 536 | """ |
| 537 | try: |
| 538 | list_of_commands = ffdc_actions_for_machine_type['COMMANDS'] |
| 539 | except KeyError: |
| 540 | list_of_commands = [] |
| 541 | return list_of_commands |
| 542 | |
| 543 | def get_file_list(self, |
| 544 | ffdc_actions_for_machine_type): |
| 545 | r""" |
| 546 | Fetch list of commands from configuration file |
| 547 | |
| 548 | Description of argument(s): |
| 549 | ffdc_actions_for_machine_type commands and files for the selected remote host type. |
| 550 | """ |
| 551 | try: |
| 552 | list_of_files = ffdc_actions_for_machine_type['FILES'] |
| 553 | except KeyError: |
| 554 | list_of_files = [] |
| 555 | return list_of_files |
| 556 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 557 | def unpack_command(self, |
| 558 | command): |
| 559 | r""" |
| 560 | Unpack command from config file |
| 561 | |
| 562 | Description of argument(s): |
| 563 | command Command from config file. |
| 564 | """ |
| 565 | if isinstance(command, dict): |
| 566 | command_txt = next(iter(command)) |
| 567 | command_timeout = next(iter(command.values())) |
| 568 | elif isinstance(command, str): |
| 569 | command_txt = command |
| 570 | # Default command timeout 60 seconds |
| 571 | command_timeout = 60 |
| 572 | |
| 573 | return command_txt, command_timeout |
| 574 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 575 | def ssh_execute_ffdc_commands(self, |
| 576 | ffdc_actions_for_machine_type, |
| 577 | form_filename=False): |
| 578 | r""" |
| 579 | Send commands in ffdc_config file to targeted system. |
| 580 | |
| 581 | Description of argument(s): |
| 582 | ffdc_actions_for_machine_type commands and files for the selected remote host type. |
| 583 | form_filename if true, pre-pend self.target_type to filename |
| 584 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 585 | self.logger.info("\n\t[Run] Executing commands on %s using %s" |
| 586 | % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0])) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 587 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 588 | list_of_commands = self.get_command_list(ffdc_actions_for_machine_type) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 589 | # If command list is empty, returns |
| 590 | if not list_of_commands: |
| 591 | return |
| 592 | |
| 593 | progress_counter = 0 |
| 594 | for command in list_of_commands: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 595 | command_txt, command_timeout = self.unpack_command(command) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 596 | |
| 597 | if form_filename: |
| 598 | command_txt = str(command_txt % self.target_type) |
| 599 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 600 | cmd_exit_code, err, response = \ |
| 601 | self.ssh_remoteclient.execute_command(command_txt, command_timeout) |
| 602 | |
| 603 | if cmd_exit_code: |
| 604 | self.logger.warning( |
| 605 | "\n\t\t[WARN] %s exits with code %s." % (command_txt, str(cmd_exit_code))) |
| 606 | self.logger.warning("\t\t[WARN] %s " % err) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 607 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 608 | progress_counter += 1 |
| 609 | self.print_progress(progress_counter) |
| 610 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 611 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 612 | |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 613 | def group_copy(self, |
| 614 | ffdc_actions_for_machine_type): |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 615 | r""" |
| 616 | scp group of files (wild card) from remote host. |
| 617 | |
| 618 | Description of argument(s): |
| 619 | ffdc_actions_for_machine_type commands and files for the selected remote host type. |
| 620 | """ |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 621 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 622 | if self.ssh_remoteclient.scpclient: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 623 | self.logger.info("\n\tCopying DUMP files from remote system %s.\n" % self.hostname) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 624 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 625 | list_of_commands = self.get_command_list(ffdc_actions_for_machine_type) |
| 626 | # If command list is empty, returns |
| 627 | if not list_of_commands: |
| 628 | return |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 629 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 630 | for command in list_of_commands: |
| 631 | try: |
| 632 | filename = command.split(' ')[2] |
| 633 | except IndexError: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 634 | self.logger.info("\t\tInvalid command %s for DUMP_LOGS block." % command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 635 | continue |
| 636 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 637 | cmd_exit_code, err, response = \ |
| 638 | self.ssh_remoteclient.execute_command(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 639 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 640 | # If file does not exist, code take no action. |
| 641 | # cmd_exit_code is ignored for this scenario. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 642 | if response: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 643 | scp_result = self.ssh_remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 644 | if scp_result: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 645 | self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + filename) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 646 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 647 | self.logger.info("\t\tThere is no " + filename) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 648 | |
| 649 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 650 | self.logger.info("\n\n\tSkip copying files from remote system %s.\n" % self.hostname) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 651 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 652 | def scp_ffdc(self, |
| 653 | targ_dir_path, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 654 | targ_file_prefix, |
| 655 | form_filename, |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 656 | file_list=None, |
| 657 | quiet=None): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 658 | r""" |
| 659 | SCP all files in file_dict to the indicated directory on the local system. |
| 660 | |
| 661 | Description of argument(s): |
| 662 | targ_dir_path The path of the directory to receive the files. |
| 663 | targ_file_prefix Prefix which will be pre-pended to each |
| 664 | target file's name. |
| 665 | file_dict A dictionary of files to scp from targeted system to this system |
| 666 | |
| 667 | """ |
| 668 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 669 | progress_counter = 0 |
| 670 | for filename in file_list: |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 671 | if form_filename: |
| 672 | filename = str(filename % self.target_type) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 673 | source_file_path = filename |
| 674 | targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1] |
| 675 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 676 | # If source file name contains wild card, copy filename as is. |
| 677 | if '*' in source_file_path: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 678 | scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, self.ffdc_dir_path) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 679 | else: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 680 | scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, targ_file_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 681 | |
| 682 | if not quiet: |
| 683 | if scp_result: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 684 | self.logger.info( |
| 685 | "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 686 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 687 | self.logger.info( |
| 688 | "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 689 | else: |
| 690 | progress_counter += 1 |
| 691 | self.print_progress(progress_counter) |
| 692 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 693 | def set_ffdc_defaults(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 694 | r""" |
| 695 | Set a default value for self.ffdc_dir_path and self.ffdc_prefix. |
| 696 | Collected ffdc file will be stored in dir /self.location/hostname_timestr/. |
| 697 | Individual ffdc file will have timestr_filename. |
| 698 | |
| 699 | Description of class variables: |
| 700 | self.ffdc_dir_path The dir path where collected ffdc data files should be put. |
| 701 | |
| 702 | self.ffdc_prefix The prefix to be given to each ffdc file name. |
| 703 | |
| 704 | """ |
| 705 | |
| 706 | timestr = time.strftime("%Y%m%d-%H%M%S") |
| 707 | self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/" |
| 708 | self.ffdc_prefix = timestr + "_" |
| 709 | self.validate_local_store(self.ffdc_dir_path) |
| 710 | |
| 711 | def validate_local_store(self, dir_path): |
| 712 | r""" |
| 713 | Ensure path exists to store FFDC files locally. |
| 714 | |
| 715 | Description of variable: |
| 716 | dir_path The dir path where collected ffdc data files will be stored. |
| 717 | |
| 718 | """ |
| 719 | |
| 720 | if not os.path.exists(dir_path): |
| 721 | try: |
George Keishing | 7b3a513 | 2021-07-13 09:24:02 -0500 | [diff] [blame] | 722 | os.makedirs(dir_path, 0o755) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 723 | except (IOError, OSError) as e: |
| 724 | # PermissionError |
| 725 | if e.errno == EPERM or e.errno == EACCES: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 726 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 727 | '\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 728 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 729 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 730 | '\tERROR: os.makedirs %s failed with %s.\n' % (dir_path, e.strerror)) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 731 | sys.exit(-1) |
| 732 | |
| 733 | def print_progress(self, progress): |
| 734 | r""" |
| 735 | Print activity progress + |
| 736 | |
| 737 | Description of variable: |
| 738 | progress Progress counter. |
| 739 | |
| 740 | """ |
| 741 | |
| 742 | sys.stdout.write("\r\t" + "+" * progress) |
| 743 | sys.stdout.flush() |
| 744 | time.sleep(.1) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 745 | |
| 746 | def verify_redfish(self): |
| 747 | r""" |
| 748 | Verify remote host has redfish service active |
| 749 | |
| 750 | """ |
| 751 | redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \ |
| 752 | + self.hostname + ' -S Always raw GET /redfish/v1/' |
| 753 | return(self.run_redfishtool(redfish_parm, True)) |
| 754 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 755 | def verify_ipmi(self): |
| 756 | r""" |
| 757 | Verify remote host has IPMI LAN service active |
| 758 | |
| 759 | """ |
| 760 | ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \ |
Peter D Phan | d1fccd3 | 2021-07-21 06:45:54 -0500 | [diff] [blame] | 761 | + self.hostname + ' power status -I lanplus' |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 762 | return(self.run_ipmitool(ipmi_parm, True)) |
| 763 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 764 | def run_redfishtool(self, |
| 765 | parms_string, |
| 766 | quiet=False): |
| 767 | r""" |
| 768 | Run CLI redfishtool |
| 769 | |
| 770 | Description of variable: |
| 771 | parms_string redfishtool subcommand and options. |
| 772 | quiet do not print redfishtool error message if True |
| 773 | """ |
| 774 | |
| 775 | result = subprocess.run(['redfishtool ' + parms_string], |
| 776 | stdout=subprocess.PIPE, |
| 777 | stderr=subprocess.PIPE, |
| 778 | shell=True, |
| 779 | universal_newlines=True) |
| 780 | |
| 781 | if result.stderr and not quiet: |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 782 | self.logger.error('\n\tERROR with redfishtool ' + parms_string) |
| 783 | self.logger.error('\n\t%s' % result.stderr.split('\n')) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 784 | |
| 785 | return result.stdout |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 786 | |
| 787 | def run_ipmitool(self, |
| 788 | parms_string, |
| 789 | quiet=False): |
| 790 | r""" |
| 791 | Run CLI IPMI tool. |
| 792 | |
| 793 | Description of variable: |
| 794 | parms_string ipmitool subcommand and options. |
| 795 | quiet do not print redfishtool error message if True |
| 796 | """ |
| 797 | |
| 798 | result = subprocess.run(['ipmitool -I lanplus -C 17 ' + parms_string], |
| 799 | stdout=subprocess.PIPE, |
| 800 | stderr=subprocess.PIPE, |
| 801 | shell=True, |
| 802 | universal_newlines=True) |
| 803 | |
| 804 | if result.stderr and not quiet: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 805 | self.logger.error('\n\t\tERROR with ipmitool -I lanplus -C 17 ' + parms_string) |
| 806 | self.logger.error('\t\t' + result.stderr) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 807 | |
| 808 | return result.stdout |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 809 | |
| 810 | def run_shell_script(self, |
| 811 | parms_string, |
| 812 | quiet=False): |
| 813 | r""" |
| 814 | Run CLI shell script tool. |
| 815 | |
| 816 | Description of variable: |
| 817 | parms_string script command options. |
| 818 | quiet do not print redfishtool error message if True |
| 819 | """ |
| 820 | |
| 821 | result = subprocess.run([parms_string], |
| 822 | stdout=subprocess.PIPE, |
| 823 | stderr=subprocess.PIPE, |
| 824 | shell=True, |
| 825 | universal_newlines=True) |
| 826 | |
| 827 | if result.stderr and not quiet: |
| 828 | self.logger.error('\n\t\tERROR executing %s' % parms_string) |
| 829 | self.logger.error('\t\t' + result.stderr) |
| 830 | |
| 831 | return result.stdout |
| 832 | |
| 833 | def protocol_shell_script(self, |
| 834 | ffdc_actions, |
| 835 | machine_type, |
| 836 | sub_type): |
| 837 | r""" |
| 838 | Perform SHELL script execution locally. |
| 839 | |
| 840 | Description of argument(s): |
| 841 | ffdc_actions List of actions from ffdc_config.yaml. |
| 842 | machine_type OS Type of remote host. |
| 843 | sub_type Group type of commands. |
| 844 | """ |
| 845 | |
| 846 | self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'SHELL')) |
| 847 | shell_files_saved = [] |
| 848 | progress_counter = 0 |
| 849 | list_of_cmd = self.get_command_list(ffdc_actions[machine_type][sub_type]) |
| 850 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
| 851 | |
| 852 | result = self.run_shell_script(each_cmd) |
| 853 | if result: |
| 854 | try: |
| 855 | targ_file = self.get_file_list(ffdc_actions[machine_type][sub_type])[index] |
| 856 | except IndexError: |
| 857 | targ_file = each_cmd.split('/')[-1] |
| 858 | self.logger.warning("\n\t[WARN] Missing filename to store data %s." % each_cmd) |
| 859 | self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file) |
| 860 | |
| 861 | targ_file_with_path = (self.ffdc_dir_path |
| 862 | + self.ffdc_prefix |
| 863 | + targ_file) |
| 864 | |
| 865 | # Creates a new file |
| 866 | with open(targ_file_with_path, 'w') as fp: |
| 867 | fp.write(result) |
| 868 | fp.close |
| 869 | shell_files_saved.append(targ_file) |
| 870 | |
| 871 | progress_counter += 1 |
| 872 | self.print_progress(progress_counter) |
| 873 | |
| 874 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
| 875 | |
| 876 | for file in shell_files_saved: |
| 877 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |