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