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() |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 188 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 189 | # Find the list of target and protocol supported. |
| 190 | check_protocol_list = [] |
| 191 | config_dict = self.ffdc_actions |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 192 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 193 | for target_type in config_dict.keys(): |
| 194 | if self.target_type != target_type: |
| 195 | continue |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 196 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 197 | for k, v in config_dict[target_type].items(): |
| 198 | if config_dict[target_type][k]['PROTOCOL'][0] not in check_protocol_list: |
| 199 | check_protocol_list.append(config_dict[target_type][k]['PROTOCOL'][0]) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 200 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 201 | self.logger.info("\n\t %s protocol type: %s" % (self.target_type, check_protocol_list)) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 202 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 203 | verified_working_protocol = self.verify_protocol(check_protocol_list) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 204 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 205 | if verified_working_protocol: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 206 | self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 207 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 208 | # Verify top level directory exists for storage |
| 209 | self.validate_local_store(self.location) |
| 210 | |
| 211 | if ((self.remote_protocol not in verified_working_protocol) and (self.remote_protocol != 'ALL')): |
| 212 | self.logger.info("\n\tWorking protocol list: %s" % verified_working_protocol) |
| 213 | self.logger.error( |
| 214 | '\tERROR: Requested protocol %s is not in working protocol list.\n' |
| 215 | % self.remote_protocol) |
| 216 | sys.exit(-1) |
| 217 | else: |
| 218 | self.generate_ffdc(verified_working_protocol) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 219 | |
| 220 | def ssh_to_target_system(self): |
| 221 | r""" |
| 222 | Open a ssh connection to targeted system. |
| 223 | |
| 224 | """ |
| 225 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 226 | self.ssh_remoteclient = SSHRemoteclient(self.hostname, |
| 227 | self.username, |
| 228 | self.password) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 229 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 230 | if self.ssh_remoteclient.ssh_remoteclient_login(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 231 | 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] | 232 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 233 | # Check scp connection. |
| 234 | # If scp connection fails, |
| 235 | # continue with FFDC generation but skip scp files to local host. |
| 236 | self.ssh_remoteclient.scp_connection() |
| 237 | return True |
| 238 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 239 | 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] | 240 | return False |
| 241 | |
| 242 | def telnet_to_target_system(self): |
| 243 | r""" |
| 244 | Open a telnet connection to targeted system. |
| 245 | """ |
| 246 | self.telnet_remoteclient = TelnetRemoteclient(self.hostname, |
| 247 | self.username, |
| 248 | self.password) |
| 249 | if self.telnet_remoteclient.tn_remoteclient_login(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 250 | 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] | 251 | return True |
| 252 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 253 | 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] | 254 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 255 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 256 | def generate_ffdc(self, working_protocol_list): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 257 | r""" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 258 | Determine actions based on remote host type |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 259 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 260 | Description of argument(s): |
| 261 | 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] | 262 | """ |
| 263 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 264 | self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----") |
| 265 | self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 266 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 267 | config_dict = self.ffdc_actions |
| 268 | for target_type in config_dict.keys(): |
| 269 | if self.target_type != target_type: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 270 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 271 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 272 | self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 273 | self.logger.info("\tSystem Type: %s" % target_type) |
| 274 | for k, v in config_dict[target_type].items(): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 275 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 276 | if self.remote_protocol not in working_protocol_list \ |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 277 | and self.remote_protocol != 'ALL': |
| 278 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 279 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 280 | protocol = config_dict[target_type][k]['PROTOCOL'][0] |
| 281 | |
| 282 | if protocol not in working_protocol_list: |
| 283 | continue |
| 284 | |
| 285 | if protocol == 'SSH' or protocol == 'SCP': |
| 286 | if 'SSH' in working_protocol_list or 'SCP' in working_protocol_list: |
| 287 | self.protocol_ssh(target_type, k) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 288 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 289 | 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] | 290 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 291 | if protocol == 'TELNET': |
| 292 | if protocol in working_protocol_list: |
| 293 | self.protocol_telnet(target_type, k) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 294 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 295 | 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] | 296 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 297 | if protocol == 'REDFISH': |
| 298 | if protocol in working_protocol_list: |
| 299 | self.protocol_redfish(target_type, k) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 300 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 301 | 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] | 302 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 303 | if protocol == 'IPMI': |
| 304 | if protocol in working_protocol_list: |
| 305 | self.protocol_ipmi(target_type, k) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 306 | else: |
Peter D Phan | d1fccd3 | 2021-07-21 06:45:54 -0500 | [diff] [blame] | 307 | 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] | 308 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 309 | if protocol == 'SHELL': |
| 310 | if protocol in working_protocol_list: |
| 311 | self.protocol_shell_script(target_type, k) |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 312 | else: |
| 313 | self.logger.error("\n\tERROR: can't execute SHELL script") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 314 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 315 | # Close network connection after collecting all files |
Peter D Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 316 | 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] | 317 | if self.ssh_remoteclient: |
| 318 | self.ssh_remoteclient.ssh_remoteclient_disconnect() |
| 319 | if self.telnet_remoteclient: |
| 320 | self.telnet_remoteclient.tn_remoteclient_disconnect() |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 321 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 322 | def protocol_ssh(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 323 | target_type, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 324 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 325 | r""" |
| 326 | Perform actions using SSH and SCP protocols. |
| 327 | |
| 328 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 329 | target_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 330 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 331 | """ |
| 332 | |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 333 | if sub_type == 'DUMP_LOGS': |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 334 | self.group_copy(self.ffdc_actions[target_type][sub_type]) |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 335 | else: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 336 | self.collect_and_copy_ffdc(self.ffdc_actions[target_type][sub_type]) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 337 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 338 | def protocol_telnet(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 339 | target_type, |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 340 | sub_type): |
| 341 | r""" |
| 342 | Perform actions using telnet protocol. |
| 343 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 344 | target_type OS Type of remote host. |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 345 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 346 | 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] | 347 | telnet_files_saved = [] |
| 348 | progress_counter = 0 |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 349 | list_of_commands = self.ffdc_actions[target_type][sub_type]['COMMANDS'] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 350 | for index, each_cmd in enumerate(list_of_commands, start=0): |
| 351 | command_txt, command_timeout = self.unpack_command(each_cmd) |
| 352 | result = self.telnet_remoteclient.execute_command(command_txt, command_timeout) |
| 353 | if result: |
| 354 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 355 | targ_file = self.ffdc_actions[target_type][sub_type]['FILES'][index] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 356 | except IndexError: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 357 | targ_file = command_txt |
| 358 | self.logger.warning( |
| 359 | "\n\t[WARN] Missing filename to store data from telnet %s." % each_cmd) |
| 360 | 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] | 361 | targ_file_with_path = (self.ffdc_dir_path |
| 362 | + self.ffdc_prefix |
| 363 | + targ_file) |
| 364 | # Creates a new file |
| 365 | with open(targ_file_with_path, 'wb') as fp: |
| 366 | fp.write(result) |
| 367 | fp.close |
| 368 | telnet_files_saved.append(targ_file) |
| 369 | progress_counter += 1 |
| 370 | self.print_progress(progress_counter) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 371 | 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] | 372 | for file in telnet_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 373 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 374 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 375 | def protocol_redfish(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 376 | target_type, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 377 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 378 | r""" |
| 379 | Perform actions using Redfish protocol. |
| 380 | |
| 381 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 382 | target_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 383 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 384 | """ |
| 385 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 386 | 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] | 387 | redfish_files_saved = [] |
| 388 | progress_counter = 0 |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 389 | list_of_URL = self.ffdc_actions[target_type][sub_type]['URL'] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 390 | for index, each_url in enumerate(list_of_URL, start=0): |
| 391 | redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \ |
| 392 | + self.hostname + ' -S Always raw GET ' + each_url |
| 393 | |
| 394 | result = self.run_redfishtool(redfish_parm) |
| 395 | if result: |
| 396 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 397 | targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 398 | except IndexError: |
| 399 | targ_file = each_url.split('/')[-1] |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 400 | self.logger.warning( |
| 401 | "\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url) |
| 402 | 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] | 403 | |
| 404 | targ_file_with_path = (self.ffdc_dir_path |
| 405 | + self.ffdc_prefix |
| 406 | + targ_file) |
| 407 | |
| 408 | # Creates a new file |
| 409 | with open(targ_file_with_path, 'w') as fp: |
| 410 | fp.write(result) |
| 411 | fp.close |
| 412 | redfish_files_saved.append(targ_file) |
| 413 | |
| 414 | progress_counter += 1 |
| 415 | self.print_progress(progress_counter) |
| 416 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 417 | 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] | 418 | |
| 419 | for file in redfish_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 420 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 421 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 422 | def protocol_ipmi(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 423 | target_type, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 424 | sub_type): |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 425 | r""" |
| 426 | Perform actions using ipmitool over LAN protocol. |
| 427 | |
| 428 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 429 | target_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 430 | sub_type Group type of commands. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 431 | """ |
| 432 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 433 | 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] | 434 | ipmi_files_saved = [] |
| 435 | progress_counter = 0 |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 436 | list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type]) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 437 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
| 438 | ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \ |
Peter D Phan | d1fccd3 | 2021-07-21 06:45:54 -0500 | [diff] [blame] | 439 | + self.hostname + ' -I lanplus ' + each_cmd |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 440 | |
| 441 | result = self.run_ipmitool(ipmi_parm) |
| 442 | if result: |
| 443 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 444 | targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 445 | except IndexError: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 446 | targ_file = each_cmd.split('/')[-1] |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 447 | self.logger.warning("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd) |
| 448 | 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] | 449 | |
| 450 | targ_file_with_path = (self.ffdc_dir_path |
| 451 | + self.ffdc_prefix |
| 452 | + targ_file) |
| 453 | |
| 454 | # Creates a new file |
| 455 | with open(targ_file_with_path, 'w') as fp: |
| 456 | fp.write(result) |
| 457 | fp.close |
| 458 | ipmi_files_saved.append(targ_file) |
| 459 | |
| 460 | progress_counter += 1 |
| 461 | self.print_progress(progress_counter) |
| 462 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 463 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 464 | |
| 465 | for file in ipmi_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 466 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 467 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 468 | def collect_and_copy_ffdc(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 469 | ffdc_actions_for_target_type, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 470 | form_filename=False): |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 471 | r""" |
| 472 | Send commands in ffdc_config file to targeted system. |
| 473 | |
| 474 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 475 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 476 | form_filename if true, pre-pend self.target_type to filename |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 477 | """ |
| 478 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 479 | # Executing commands, if any |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 480 | self.ssh_execute_ffdc_commands(ffdc_actions_for_target_type, |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 481 | form_filename) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 482 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 483 | # Copying files |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 484 | if self.ssh_remoteclient.scpclient: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 485 | 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] | 486 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 487 | # Retrieving files from target system |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 488 | list_of_files = self.get_file_list(ffdc_actions_for_target_type) |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 489 | 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] | 490 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 491 | 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] | 492 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 493 | def get_command_list(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 494 | ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 495 | r""" |
| 496 | Fetch list of commands from configuration file |
| 497 | |
| 498 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 499 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 500 | """ |
| 501 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 502 | list_of_commands = ffdc_actions_for_target_type['COMMANDS'] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 503 | except KeyError: |
| 504 | list_of_commands = [] |
| 505 | return list_of_commands |
| 506 | |
| 507 | def get_file_list(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 508 | ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 509 | r""" |
| 510 | Fetch list of commands from configuration file |
| 511 | |
| 512 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 513 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 514 | """ |
| 515 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 516 | list_of_files = ffdc_actions_for_target_type['FILES'] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 517 | except KeyError: |
| 518 | list_of_files = [] |
| 519 | return list_of_files |
| 520 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 521 | def unpack_command(self, |
| 522 | command): |
| 523 | r""" |
| 524 | Unpack command from config file |
| 525 | |
| 526 | Description of argument(s): |
| 527 | command Command from config file. |
| 528 | """ |
| 529 | if isinstance(command, dict): |
| 530 | command_txt = next(iter(command)) |
| 531 | command_timeout = next(iter(command.values())) |
| 532 | elif isinstance(command, str): |
| 533 | command_txt = command |
| 534 | # Default command timeout 60 seconds |
| 535 | command_timeout = 60 |
| 536 | |
| 537 | return command_txt, command_timeout |
| 538 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 539 | def ssh_execute_ffdc_commands(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 540 | ffdc_actions_for_target_type, |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 541 | form_filename=False): |
| 542 | r""" |
| 543 | Send commands in ffdc_config file to targeted system. |
| 544 | |
| 545 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 546 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 547 | form_filename if true, pre-pend self.target_type to filename |
| 548 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 549 | self.logger.info("\n\t[Run] Executing commands on %s using %s" |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 550 | % (self.hostname, ffdc_actions_for_target_type['PROTOCOL'][0])) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 551 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 552 | list_of_commands = self.get_command_list(ffdc_actions_for_target_type) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 553 | # If command list is empty, returns |
| 554 | if not list_of_commands: |
| 555 | return |
| 556 | |
| 557 | progress_counter = 0 |
| 558 | for command in list_of_commands: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 559 | command_txt, command_timeout = self.unpack_command(command) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 560 | |
| 561 | if form_filename: |
| 562 | command_txt = str(command_txt % self.target_type) |
| 563 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 564 | cmd_exit_code, err, response = \ |
| 565 | self.ssh_remoteclient.execute_command(command_txt, command_timeout) |
| 566 | |
| 567 | if cmd_exit_code: |
| 568 | self.logger.warning( |
| 569 | "\n\t\t[WARN] %s exits with code %s." % (command_txt, str(cmd_exit_code))) |
| 570 | self.logger.warning("\t\t[WARN] %s " % err) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 571 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 572 | progress_counter += 1 |
| 573 | self.print_progress(progress_counter) |
| 574 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 575 | 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] | 576 | |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 577 | def group_copy(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 578 | ffdc_actions_for_target_type): |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 579 | r""" |
| 580 | scp group of files (wild card) from remote host. |
| 581 | |
| 582 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 583 | fdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 584 | """ |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 585 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 586 | if self.ssh_remoteclient.scpclient: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 587 | 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] | 588 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 589 | list_of_commands = self.get_command_list(ffdc_actions_for_target_type) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 590 | # If command list is empty, returns |
| 591 | if not list_of_commands: |
| 592 | return |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 593 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 594 | for command in list_of_commands: |
| 595 | try: |
| 596 | filename = command.split(' ')[2] |
| 597 | except IndexError: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 598 | 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] | 599 | continue |
| 600 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 601 | cmd_exit_code, err, response = \ |
| 602 | self.ssh_remoteclient.execute_command(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 603 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 604 | # If file does not exist, code take no action. |
| 605 | # cmd_exit_code is ignored for this scenario. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 606 | if response: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 607 | 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] | 608 | if scp_result: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 609 | self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + filename) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 610 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 611 | self.logger.info("\t\tThere is no " + filename) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 612 | |
| 613 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 614 | 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] | 615 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 616 | def scp_ffdc(self, |
| 617 | targ_dir_path, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 618 | targ_file_prefix, |
| 619 | form_filename, |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 620 | file_list=None, |
| 621 | quiet=None): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 622 | r""" |
| 623 | SCP all files in file_dict to the indicated directory on the local system. |
| 624 | |
| 625 | Description of argument(s): |
| 626 | targ_dir_path The path of the directory to receive the files. |
| 627 | targ_file_prefix Prefix which will be pre-pended to each |
| 628 | target file's name. |
| 629 | file_dict A dictionary of files to scp from targeted system to this system |
| 630 | |
| 631 | """ |
| 632 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 633 | progress_counter = 0 |
| 634 | for filename in file_list: |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 635 | if form_filename: |
| 636 | filename = str(filename % self.target_type) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 637 | source_file_path = filename |
| 638 | targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1] |
| 639 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 640 | # If source file name contains wild card, copy filename as is. |
| 641 | if '*' in source_file_path: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 642 | 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] | 643 | else: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 644 | 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] | 645 | |
| 646 | if not quiet: |
| 647 | if scp_result: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 648 | self.logger.info( |
| 649 | "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 650 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 651 | self.logger.info( |
| 652 | "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 653 | else: |
| 654 | progress_counter += 1 |
| 655 | self.print_progress(progress_counter) |
| 656 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 657 | def set_ffdc_defaults(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 658 | r""" |
| 659 | Set a default value for self.ffdc_dir_path and self.ffdc_prefix. |
| 660 | Collected ffdc file will be stored in dir /self.location/hostname_timestr/. |
| 661 | Individual ffdc file will have timestr_filename. |
| 662 | |
| 663 | Description of class variables: |
| 664 | self.ffdc_dir_path The dir path where collected ffdc data files should be put. |
| 665 | |
| 666 | self.ffdc_prefix The prefix to be given to each ffdc file name. |
| 667 | |
| 668 | """ |
| 669 | |
| 670 | timestr = time.strftime("%Y%m%d-%H%M%S") |
| 671 | self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/" |
| 672 | self.ffdc_prefix = timestr + "_" |
| 673 | self.validate_local_store(self.ffdc_dir_path) |
| 674 | |
| 675 | def validate_local_store(self, dir_path): |
| 676 | r""" |
| 677 | Ensure path exists to store FFDC files locally. |
| 678 | |
| 679 | Description of variable: |
| 680 | dir_path The dir path where collected ffdc data files will be stored. |
| 681 | |
| 682 | """ |
| 683 | |
| 684 | if not os.path.exists(dir_path): |
| 685 | try: |
George Keishing | 7b3a513 | 2021-07-13 09:24:02 -0500 | [diff] [blame] | 686 | os.makedirs(dir_path, 0o755) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 687 | except (IOError, OSError) as e: |
| 688 | # PermissionError |
| 689 | if e.errno == EPERM or e.errno == EACCES: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 690 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 691 | '\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 692 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 693 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 694 | '\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] | 695 | sys.exit(-1) |
| 696 | |
| 697 | def print_progress(self, progress): |
| 698 | r""" |
| 699 | Print activity progress + |
| 700 | |
| 701 | Description of variable: |
| 702 | progress Progress counter. |
| 703 | |
| 704 | """ |
| 705 | |
| 706 | sys.stdout.write("\r\t" + "+" * progress) |
| 707 | sys.stdout.flush() |
| 708 | time.sleep(.1) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 709 | |
| 710 | def verify_redfish(self): |
| 711 | r""" |
| 712 | Verify remote host has redfish service active |
| 713 | |
| 714 | """ |
| 715 | redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \ |
| 716 | + self.hostname + ' -S Always raw GET /redfish/v1/' |
| 717 | return(self.run_redfishtool(redfish_parm, True)) |
| 718 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 719 | def verify_ipmi(self): |
| 720 | r""" |
| 721 | Verify remote host has IPMI LAN service active |
| 722 | |
| 723 | """ |
| 724 | ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \ |
Peter D Phan | d1fccd3 | 2021-07-21 06:45:54 -0500 | [diff] [blame] | 725 | + self.hostname + ' power status -I lanplus' |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 726 | return(self.run_ipmitool(ipmi_parm, True)) |
| 727 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 728 | def run_redfishtool(self, |
| 729 | parms_string, |
| 730 | quiet=False): |
| 731 | r""" |
| 732 | Run CLI redfishtool |
| 733 | |
| 734 | Description of variable: |
| 735 | parms_string redfishtool subcommand and options. |
| 736 | quiet do not print redfishtool error message if True |
| 737 | """ |
| 738 | |
| 739 | result = subprocess.run(['redfishtool ' + parms_string], |
| 740 | stdout=subprocess.PIPE, |
| 741 | stderr=subprocess.PIPE, |
| 742 | shell=True, |
| 743 | universal_newlines=True) |
| 744 | |
| 745 | if result.stderr and not quiet: |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 746 | self.logger.error('\n\tERROR with redfishtool ' + parms_string) |
| 747 | self.logger.error('\n\t%s' % result.stderr.split('\n')) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 748 | |
| 749 | return result.stdout |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 750 | |
| 751 | def run_ipmitool(self, |
| 752 | parms_string, |
| 753 | quiet=False): |
| 754 | r""" |
| 755 | Run CLI IPMI tool. |
| 756 | |
| 757 | Description of variable: |
| 758 | parms_string ipmitool subcommand and options. |
| 759 | quiet do not print redfishtool error message if True |
| 760 | """ |
| 761 | |
| 762 | result = subprocess.run(['ipmitool -I lanplus -C 17 ' + parms_string], |
| 763 | stdout=subprocess.PIPE, |
| 764 | stderr=subprocess.PIPE, |
| 765 | shell=True, |
| 766 | universal_newlines=True) |
| 767 | |
| 768 | if result.stderr and not quiet: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 769 | self.logger.error('\n\t\tERROR with ipmitool -I lanplus -C 17 ' + parms_string) |
| 770 | self.logger.error('\t\t' + result.stderr) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 771 | |
| 772 | return result.stdout |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 773 | |
| 774 | def run_shell_script(self, |
| 775 | parms_string, |
| 776 | quiet=False): |
| 777 | r""" |
| 778 | Run CLI shell script tool. |
| 779 | |
| 780 | Description of variable: |
| 781 | parms_string script command options. |
| 782 | quiet do not print redfishtool error message if True |
| 783 | """ |
| 784 | |
| 785 | result = subprocess.run([parms_string], |
| 786 | stdout=subprocess.PIPE, |
| 787 | stderr=subprocess.PIPE, |
| 788 | shell=True, |
| 789 | universal_newlines=True) |
| 790 | |
| 791 | if result.stderr and not quiet: |
| 792 | self.logger.error('\n\t\tERROR executing %s' % parms_string) |
| 793 | self.logger.error('\t\t' + result.stderr) |
| 794 | |
| 795 | return result.stdout |
| 796 | |
| 797 | def protocol_shell_script(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 798 | target_type, |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 799 | sub_type): |
| 800 | r""" |
| 801 | Perform SHELL script execution locally. |
| 802 | |
| 803 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 804 | target_type OS Type of remote host. |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 805 | sub_type Group type of commands. |
| 806 | """ |
| 807 | |
| 808 | self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'SHELL')) |
| 809 | shell_files_saved = [] |
| 810 | progress_counter = 0 |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 811 | list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type]) |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 812 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
| 813 | |
| 814 | result = self.run_shell_script(each_cmd) |
| 815 | if result: |
| 816 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 817 | targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index] |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 818 | except IndexError: |
| 819 | targ_file = each_cmd.split('/')[-1] |
| 820 | self.logger.warning("\n\t[WARN] Missing filename to store data %s." % each_cmd) |
| 821 | self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file) |
| 822 | |
| 823 | targ_file_with_path = (self.ffdc_dir_path |
| 824 | + self.ffdc_prefix |
| 825 | + targ_file) |
| 826 | |
| 827 | # Creates a new file |
| 828 | with open(targ_file_with_path, 'w') as fp: |
| 829 | fp.write(result) |
| 830 | fp.close |
| 831 | shell_files_saved.append(targ_file) |
| 832 | |
| 833 | progress_counter += 1 |
| 834 | self.print_progress(progress_counter) |
| 835 | |
| 836 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
| 837 | |
| 838 | for file in shell_files_saved: |
| 839 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 840 | |
| 841 | def verify_protocol(self, protocol_list): |
| 842 | r""" |
| 843 | Perform protocol working check. |
| 844 | |
| 845 | Description of argument(s): |
| 846 | protocol_list List of protocol. |
| 847 | """ |
| 848 | |
| 849 | tmp_list = [] |
| 850 | if self.target_is_pingable(): |
| 851 | tmp_list.append("SHELL") |
| 852 | |
| 853 | for protocol in protocol_list: |
| 854 | if self.remote_protocol != 'ALL': |
| 855 | if self.remote_protocol != protocol: |
| 856 | continue |
| 857 | |
| 858 | # Only check SSH/SCP once for both protocols |
| 859 | if protocol == 'SSH' or protocol == 'SCP' and protocol not in tmp_list: |
| 860 | if self.ssh_to_target_system(): |
| 861 | tmp_list.append('SSH') |
| 862 | tmp_list.append('SCP') |
| 863 | |
| 864 | if protocol == 'TELNET': |
| 865 | if self.telnet_to_target_system(): |
| 866 | tmp_list.append(protocol) |
| 867 | |
| 868 | if protocol == 'REDFISH': |
| 869 | if self.verify_redfish(): |
| 870 | tmp_list.append(protocol) |
| 871 | self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname) |
| 872 | else: |
| 873 | self.logger.info("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname) |
| 874 | |
| 875 | if protocol == 'IPMI': |
| 876 | if self.verify_ipmi(): |
| 877 | tmp_list.append(protocol) |
| 878 | self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname) |
| 879 | else: |
| 880 | self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname) |
| 881 | |
| 882 | return tmp_list |