blob: 53fe9dd073ad091966373dd84e1bb58b8e2fd631 [file] [log] [blame]
Peter D Phan72ce6b82021-06-03 06:18:26 -05001#!/usr/bin/env python
2
3r"""
4See class prolog below for details.
5"""
6
7import os
George Keishing0813e712021-07-26 08:29:20 -05008import re
Peter D Phan72ce6b82021-06-03 06:18:26 -05009import sys
10import yaml
George Keishing4885b2f2021-07-21 15:22:45 -050011import json
Peter D Phan72ce6b82021-06-03 06:18:26 -050012import time
Peter D Phane86d9a52021-07-15 10:42:25 -050013import logging
Peter D Phan72ce6b82021-06-03 06:18:26 -050014import platform
15from errno import EACCES, EPERM
Peter D Phan0c669772021-06-24 13:52:42 -050016import subprocess
Peter D Phan72ce6b82021-06-03 06:18:26 -050017from ssh_utility import SSHRemoteclient
Peter D Phan5963d632021-07-12 09:58:55 -050018from telnet_utility import TelnetRemoteclient
Peter D Phan72ce6b82021-06-03 06:18:26 -050019
20
21class 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 Phan0c669772021-06-24 13:52:42 -050029 def __init__(self,
30 hostname,
31 username,
32 password,
33 ffdc_config,
34 location,
35 remote_type,
Peter D Phane86d9a52021-07-15 10:42:25 -050036 remote_protocol,
George Keishing4885b2f2021-07-21 15:22:45 -050037 env_vars,
George Keishing8e94f8c2021-07-23 15:06:32 -050038 econfig,
Peter D Phane86d9a52021-07-15 10:42:25 -050039 log_level):
Peter D Phan72ce6b82021-06-03 06:18:26 -050040 r"""
41 Description of argument(s):
42
George Keishing8e94f8c2021-07-23 15:06:32 -050043 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 Phan72ce6b82021-06-03 06:18:26 -050052
53 """
Peter D Phane86d9a52021-07-15 10:42:25 -050054
55 self.hostname = hostname
56 self.username = username
57 self.password = password
George Keishing04d29102021-07-16 02:05:57 -050058 # 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 Phane86d9a52021-07-15 10:42:25 -050066 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 Phan72ce6b82021-06-03 06:18:26 -050091 if self.verify_script_env():
Peter D Phane86d9a52021-07-15 10:42:25 -050092 # 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 Phan72ce6b82021-06-03 06:18:26 -0500100 else:
Peter D Phan8462faf2021-06-16 12:24:15 -0500101 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500102
George Keishing4885b2f2021-07-21 15:22:45 -0500103 # Load ENV vars from user.
George Keishingaa1f8482021-07-22 00:54:55 -0500104 self.logger.info("\n\tENV: User define input YAML variables")
105 self.env_dict = {}
106
George Keishing4885b2f2021-07-21 15:22:45 -0500107 try:
108 if env_vars:
George Keishingaa1f8482021-07-22 00:54:55 -0500109 self.env_dict = json.loads(env_vars)
George Keishing4885b2f2021-07-21 15:22:45 -0500110
111 # Export ENV vars default.
George Keishingaa1f8482021-07-22 00:54:55 -0500112 for key, value in self.env_dict.items():
George Keishing4885b2f2021-07-21 15:22:45 -0500113 os.environ[key] = value
George Keishingaa1f8482021-07-22 00:54:55 -0500114
George Keishing8e94f8c2021-07-23 15:06:32 -0500115 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 Keishing4885b2f2021-07-21 15:22:45 -0500123 except json.decoder.JSONDecodeError as e:
124 self.logger.error("\n\tERROR: %s " % e)
125 sys.exit(-1)
126
George Keishingaa1f8482021-07-22 00:54:55 -0500127 # 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 Keishing0813e712021-07-26 08:29:20 -0500131 # 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 Keishingaa1f8482021-07-22 00:54:55 -0500141
Peter D Phan72ce6b82021-06-03 06:18:26 -0500142 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 Phan0c669772021-06-24 13:52:42 -0500149
George Keishingeafba182021-06-29 13:44:58 -0500150 redfishtool_version = self.run_redfishtool('-V').split(' ')[2].strip('\n')
George Keishing484f8242021-07-27 01:42:02 -0500151 ipmitool_version = self.run_ipmitool('ipmitool -V').split(' ')[2]
Peter D Phan0c669772021-06-24 13:52:42 -0500152
Peter D Phane86d9a52021-07-15 10:42:25 -0500153 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 Phan72ce6b82021-06-03 06:18:26 -0500162
Peter D Phan8462faf2021-06-16 12:24:15 -0500163 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
Peter D Phane86d9a52021-07-15 10:42:25 -0500164 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 Phan72ce6b82021-06-03 06:18:26 -0500166 run_env_ok = False
167
Peter D Phane86d9a52021-07-15 10:42:25 -0500168 self.logger.info("\t---- End script host environment ----")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500169 return run_env_ok
170
Peter D Phane86d9a52021-07-15 10:42:25 -0500171 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 Phan72ce6b82021-06-03 06:18:26 -0500188 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500189 r"""
190 Check if target system is ping-able.
191
192 """
George Keishing0662e942021-07-13 05:12:20 -0500193 response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500194 if response == 0:
Peter D Phane86d9a52021-07-15 10:42:25 -0500195 self.logger.info("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500196 return True
197 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500198 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500199 "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500200 sys.exit(-1)
201
Peter D Phan72ce6b82021-06-03 06:18:26 -0500202 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500203 r"""
204 Initiate FFDC Collection depending on requested protocol.
205
206 """
207
Peter D Phane86d9a52021-07-15 10:42:25 -0500208 self.logger.info("\n\t---- Start communicating with %s ----" % self.hostname)
Peter D Phan7610bc42021-07-06 06:31:05 -0500209 self.start_time = time.time()
Peter D Phan0c669772021-06-24 13:52:42 -0500210
George Keishingf5a57502021-07-22 16:43:47 -0500211 # Find the list of target and protocol supported.
212 check_protocol_list = []
213 config_dict = self.ffdc_actions
Peter D Phan0c669772021-06-24 13:52:42 -0500214
George Keishingf5a57502021-07-22 16:43:47 -0500215 for target_type in config_dict.keys():
216 if self.target_type != target_type:
217 continue
George Keishingeafba182021-06-29 13:44:58 -0500218
George Keishingf5a57502021-07-22 16:43:47 -0500219 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 Phanbff617a2021-07-22 08:41:35 -0500222
George Keishingf5a57502021-07-22 16:43:47 -0500223 self.logger.info("\n\t %s protocol type: %s" % (self.target_type, check_protocol_list))
Peter D Phanbff617a2021-07-22 08:41:35 -0500224
George Keishingf5a57502021-07-22 16:43:47 -0500225 verified_working_protocol = self.verify_protocol(check_protocol_list)
Peter D Phanbff617a2021-07-22 08:41:35 -0500226
George Keishingf5a57502021-07-22 16:43:47 -0500227 if verified_working_protocol:
Peter D Phane86d9a52021-07-15 10:42:25 -0500228 self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500229
George Keishingf5a57502021-07-22 16:43:47 -0500230 # 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 Phan72ce6b82021-06-03 06:18:26 -0500241
242 def ssh_to_target_system(self):
243 r"""
244 Open a ssh connection to targeted system.
245
246 """
247
Peter D Phan5963d632021-07-12 09:58:55 -0500248 self.ssh_remoteclient = SSHRemoteclient(self.hostname,
249 self.username,
250 self.password)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500251
Peter D Phan5963d632021-07-12 09:58:55 -0500252 if self.ssh_remoteclient.ssh_remoteclient_login():
Peter D Phane86d9a52021-07-15 10:42:25 -0500253 self.logger.info("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500254
Peter D Phan5963d632021-07-12 09:58:55 -0500255 # 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 Phane86d9a52021-07-15 10:42:25 -0500261 self.logger.info("\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500262 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 Phane86d9a52021-07-15 10:42:25 -0500272 self.logger.info("\n\t[Check] %s Telnet connection established.\t [OK]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500273 return True
274 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500275 self.logger.info("\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500276 return False
Peter D Phan72ce6b82021-06-03 06:18:26 -0500277
George Keishing772c9772021-06-16 23:23:42 -0500278 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500279 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500280 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500281
Peter D Phan04aca3b2021-06-21 10:37:18 -0500282 Description of argument(s):
283 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500284 """
285
Peter D Phane86d9a52021-07-15 10:42:25 -0500286 self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----")
287 self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500288
George Keishingf5a57502021-07-22 16:43:47 -0500289 config_dict = self.ffdc_actions
290 for target_type in config_dict.keys():
291 if self.target_type != target_type:
George Keishing6ea92b02021-07-01 11:20:50 -0500292 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500293
Peter D Phane86d9a52021-07-15 10:42:25 -0500294 self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path)
George Keishingf5a57502021-07-22 16:43:47 -0500295 self.logger.info("\tSystem Type: %s" % target_type)
296 for k, v in config_dict[target_type].items():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500297
George Keishingf5a57502021-07-22 16:43:47 -0500298 if self.remote_protocol not in working_protocol_list \
George Keishing6ea92b02021-07-01 11:20:50 -0500299 and self.remote_protocol != 'ALL':
300 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500301
George Keishingf5a57502021-07-22 16:43:47 -0500302 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 Phan3beb02e2021-07-06 13:25:17 -0500310 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500311 self.logger.error("\n\tERROR: SSH or SCP is not available for %s." % self.hostname)
George Keishing6ea92b02021-07-01 11:20:50 -0500312
George Keishingf5a57502021-07-22 16:43:47 -0500313 if protocol == 'TELNET':
314 if protocol in working_protocol_list:
315 self.protocol_telnet(target_type, k)
Peter D Phan5963d632021-07-12 09:58:55 -0500316 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500317 self.logger.error("\n\tERROR: TELNET is not available for %s." % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500318
George Keishingf5a57502021-07-22 16:43:47 -0500319 if protocol == 'REDFISH':
320 if protocol in working_protocol_list:
321 self.protocol_redfish(target_type, k)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500322 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500323 self.logger.error("\n\tERROR: REDFISH is not available for %s." % self.hostname)
George Keishing6ea92b02021-07-01 11:20:50 -0500324
George Keishingf5a57502021-07-22 16:43:47 -0500325 if protocol == 'IPMI':
326 if protocol in working_protocol_list:
327 self.protocol_ipmi(target_type, k)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500328 else:
Peter D Phand1fccd32021-07-21 06:45:54 -0500329 self.logger.error("\n\tERROR: IPMI is not available for %s." % self.hostname)
George Keishing04d29102021-07-16 02:05:57 -0500330
George Keishingf5a57502021-07-22 16:43:47 -0500331 if protocol == 'SHELL':
332 if protocol in working_protocol_list:
333 self.protocol_shell_script(target_type, k)
George Keishing04d29102021-07-16 02:05:57 -0500334 else:
335 self.logger.error("\n\tERROR: can't execute SHELL script")
George Keishingeafba182021-06-29 13:44:58 -0500336
Peter D Phan04aca3b2021-06-21 10:37:18 -0500337 # Close network connection after collecting all files
Peter D Phan7610bc42021-07-06 06:31:05 -0500338 self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time))
Peter D Phanbff617a2021-07-22 08:41:35 -0500339 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 Phan04aca3b2021-06-21 10:37:18 -0500343
Peter D Phan0c669772021-06-24 13:52:42 -0500344 def protocol_ssh(self,
George Keishingf5a57502021-07-22 16:43:47 -0500345 target_type,
George Keishing6ea92b02021-07-01 11:20:50 -0500346 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500347 r"""
348 Perform actions using SSH and SCP protocols.
349
350 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500351 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500352 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500353 """
354
George Keishing6ea92b02021-07-01 11:20:50 -0500355 if sub_type == 'DUMP_LOGS':
George Keishingf5a57502021-07-22 16:43:47 -0500356 self.group_copy(self.ffdc_actions[target_type][sub_type])
George Keishing6ea92b02021-07-01 11:20:50 -0500357 else:
George Keishingf5a57502021-07-22 16:43:47 -0500358 self.collect_and_copy_ffdc(self.ffdc_actions[target_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500359
Peter D Phan5963d632021-07-12 09:58:55 -0500360 def protocol_telnet(self,
George Keishingf5a57502021-07-22 16:43:47 -0500361 target_type,
Peter D Phan5963d632021-07-12 09:58:55 -0500362 sub_type):
363 r"""
364 Perform actions using telnet protocol.
365 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500366 target_type OS Type of remote host.
Peter D Phan5963d632021-07-12 09:58:55 -0500367 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500368 self.logger.info("\n\t[Run] Executing commands on %s using %s" % (self.hostname, 'TELNET'))
Peter D Phan5963d632021-07-12 09:58:55 -0500369 telnet_files_saved = []
370 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500371 list_of_commands = self.ffdc_actions[target_type][sub_type]['COMMANDS']
Peter D Phan5963d632021-07-12 09:58:55 -0500372 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 Keishingf5a57502021-07-22 16:43:47 -0500377 targ_file = self.ffdc_actions[target_type][sub_type]['FILES'][index]
Peter D Phan5963d632021-07-12 09:58:55 -0500378 except IndexError:
Peter D Phane86d9a52021-07-15 10:42:25 -0500379 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 Phan5963d632021-07-12 09:58:55 -0500383 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 Phane86d9a52021-07-15 10:42:25 -0500393 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan5963d632021-07-12 09:58:55 -0500394 for file in telnet_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500395 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
Peter D Phan5963d632021-07-12 09:58:55 -0500396
Peter D Phan0c669772021-06-24 13:52:42 -0500397 def protocol_redfish(self,
George Keishingf5a57502021-07-22 16:43:47 -0500398 target_type,
George Keishing6ea92b02021-07-01 11:20:50 -0500399 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500400 r"""
401 Perform actions using Redfish protocol.
402
403 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500404 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500405 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500406 """
407
Peter D Phane86d9a52021-07-15 10:42:25 -0500408 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH'))
Peter D Phan0c669772021-06-24 13:52:42 -0500409 redfish_files_saved = []
410 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500411 list_of_URL = self.ffdc_actions[target_type][sub_type]['URL']
Peter D Phan0c669772021-06-24 13:52:42 -0500412 for index, each_url in enumerate(list_of_URL, start=0):
413 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
414 + self.hostname + ' -S Always raw GET ' + each_url
415
416 result = self.run_redfishtool(redfish_parm)
417 if result:
418 try:
George Keishingf5a57502021-07-22 16:43:47 -0500419 targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
Peter D Phan0c669772021-06-24 13:52:42 -0500420 except IndexError:
421 targ_file = each_url.split('/')[-1]
Peter D Phane86d9a52021-07-15 10:42:25 -0500422 self.logger.warning(
423 "\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url)
424 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
Peter D Phan0c669772021-06-24 13:52:42 -0500425
426 targ_file_with_path = (self.ffdc_dir_path
427 + self.ffdc_prefix
428 + targ_file)
429
430 # Creates a new file
431 with open(targ_file_with_path, 'w') as fp:
432 fp.write(result)
433 fp.close
434 redfish_files_saved.append(targ_file)
435
436 progress_counter += 1
437 self.print_progress(progress_counter)
438
Peter D Phane86d9a52021-07-15 10:42:25 -0500439 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan0c669772021-06-24 13:52:42 -0500440
441 for file in redfish_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500442 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
Peter D Phan0c669772021-06-24 13:52:42 -0500443
George Keishingeafba182021-06-29 13:44:58 -0500444 def protocol_ipmi(self,
George Keishingf5a57502021-07-22 16:43:47 -0500445 target_type,
George Keishing6ea92b02021-07-01 11:20:50 -0500446 sub_type):
George Keishingeafba182021-06-29 13:44:58 -0500447 r"""
448 Perform actions using ipmitool over LAN protocol.
449
450 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500451 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500452 sub_type Group type of commands.
George Keishingeafba182021-06-29 13:44:58 -0500453 """
454
Peter D Phane86d9a52021-07-15 10:42:25 -0500455 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI'))
George Keishingeafba182021-06-29 13:44:58 -0500456 ipmi_files_saved = []
457 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500458 list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type])
George Keishingeafba182021-06-29 13:44:58 -0500459 for index, each_cmd in enumerate(list_of_cmd, start=0):
George Keishing484f8242021-07-27 01:42:02 -0500460 result = self.run_ipmitool(each_cmd)
George Keishingeafba182021-06-29 13:44:58 -0500461 if result:
462 try:
George Keishingf5a57502021-07-22 16:43:47 -0500463 targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
George Keishingeafba182021-06-29 13:44:58 -0500464 except IndexError:
George Keishing6ea92b02021-07-01 11:20:50 -0500465 targ_file = each_cmd.split('/')[-1]
Peter D Phane86d9a52021-07-15 10:42:25 -0500466 self.logger.warning("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd)
467 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500468
469 targ_file_with_path = (self.ffdc_dir_path
470 + self.ffdc_prefix
471 + targ_file)
472
473 # Creates a new file
474 with open(targ_file_with_path, 'w') as fp:
475 fp.write(result)
476 fp.close
477 ipmi_files_saved.append(targ_file)
478
479 progress_counter += 1
480 self.print_progress(progress_counter)
481
Peter D Phane86d9a52021-07-15 10:42:25 -0500482 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
George Keishingeafba182021-06-29 13:44:58 -0500483
484 for file in ipmi_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500485 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
George Keishingeafba182021-06-29 13:44:58 -0500486
Peter D Phan04aca3b2021-06-21 10:37:18 -0500487 def collect_and_copy_ffdc(self,
George Keishingf5a57502021-07-22 16:43:47 -0500488 ffdc_actions_for_target_type,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500489 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500490 r"""
491 Send commands in ffdc_config file to targeted system.
492
493 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500494 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500495 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500496 """
497
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500498 # Executing commands, if any
George Keishingf5a57502021-07-22 16:43:47 -0500499 self.ssh_execute_ffdc_commands(ffdc_actions_for_target_type,
Peter D Phan3beb02e2021-07-06 13:25:17 -0500500 form_filename)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500501
Peter D Phan3beb02e2021-07-06 13:25:17 -0500502 # Copying files
Peter D Phan5963d632021-07-12 09:58:55 -0500503 if self.ssh_remoteclient.scpclient:
Peter D Phane86d9a52021-07-15 10:42:25 -0500504 self.logger.info("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500505
Peter D Phan04aca3b2021-06-21 10:37:18 -0500506 # Retrieving files from target system
George Keishingf5a57502021-07-22 16:43:47 -0500507 list_of_files = self.get_file_list(ffdc_actions_for_target_type)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500508 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500509 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500510 self.logger.info("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500511
Peter D Phanbabf2962021-07-07 11:24:40 -0500512 def get_command_list(self,
George Keishingf5a57502021-07-22 16:43:47 -0500513 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500514 r"""
515 Fetch list of commands from configuration file
516
517 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500518 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500519 """
520 try:
George Keishingf5a57502021-07-22 16:43:47 -0500521 list_of_commands = ffdc_actions_for_target_type['COMMANDS']
Peter D Phanbabf2962021-07-07 11:24:40 -0500522 except KeyError:
523 list_of_commands = []
524 return list_of_commands
525
526 def get_file_list(self,
George Keishingf5a57502021-07-22 16:43:47 -0500527 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500528 r"""
529 Fetch list of commands from configuration file
530
531 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500532 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500533 """
534 try:
George Keishingf5a57502021-07-22 16:43:47 -0500535 list_of_files = ffdc_actions_for_target_type['FILES']
Peter D Phanbabf2962021-07-07 11:24:40 -0500536 except KeyError:
537 list_of_files = []
538 return list_of_files
539
Peter D Phan5963d632021-07-12 09:58:55 -0500540 def unpack_command(self,
541 command):
542 r"""
543 Unpack command from config file
544
545 Description of argument(s):
546 command Command from config file.
547 """
548 if isinstance(command, dict):
549 command_txt = next(iter(command))
550 command_timeout = next(iter(command.values()))
551 elif isinstance(command, str):
552 command_txt = command
553 # Default command timeout 60 seconds
554 command_timeout = 60
555
556 return command_txt, command_timeout
557
Peter D Phan3beb02e2021-07-06 13:25:17 -0500558 def ssh_execute_ffdc_commands(self,
George Keishingf5a57502021-07-22 16:43:47 -0500559 ffdc_actions_for_target_type,
Peter D Phan3beb02e2021-07-06 13:25:17 -0500560 form_filename=False):
561 r"""
562 Send commands in ffdc_config file to targeted system.
563
564 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500565 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan3beb02e2021-07-06 13:25:17 -0500566 form_filename if true, pre-pend self.target_type to filename
567 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500568 self.logger.info("\n\t[Run] Executing commands on %s using %s"
George Keishingf5a57502021-07-22 16:43:47 -0500569 % (self.hostname, ffdc_actions_for_target_type['PROTOCOL'][0]))
Peter D Phan3beb02e2021-07-06 13:25:17 -0500570
George Keishingf5a57502021-07-22 16:43:47 -0500571 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500572 # If command list is empty, returns
573 if not list_of_commands:
574 return
575
576 progress_counter = 0
577 for command in list_of_commands:
Peter D Phan5963d632021-07-12 09:58:55 -0500578 command_txt, command_timeout = self.unpack_command(command)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500579
580 if form_filename:
581 command_txt = str(command_txt % self.target_type)
582
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500583 cmd_exit_code, err, response = \
584 self.ssh_remoteclient.execute_command(command_txt, command_timeout)
585
586 if cmd_exit_code:
587 self.logger.warning(
588 "\n\t\t[WARN] %s exits with code %s." % (command_txt, str(cmd_exit_code)))
589 self.logger.warning("\t\t[WARN] %s " % err)
Peter D Phanbabf2962021-07-07 11:24:40 -0500590
Peter D Phan3beb02e2021-07-06 13:25:17 -0500591 progress_counter += 1
592 self.print_progress(progress_counter)
593
Peter D Phane86d9a52021-07-15 10:42:25 -0500594 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan3beb02e2021-07-06 13:25:17 -0500595
Peter D Phan56429a62021-06-23 08:38:29 -0500596 def group_copy(self,
George Keishingf5a57502021-07-22 16:43:47 -0500597 ffdc_actions_for_target_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500598 r"""
599 scp group of files (wild card) from remote host.
600
601 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500602 fdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan56429a62021-06-23 08:38:29 -0500603 """
Peter D Phan3beb02e2021-07-06 13:25:17 -0500604
Peter D Phan5963d632021-07-12 09:58:55 -0500605 if self.ssh_remoteclient.scpclient:
Peter D Phane86d9a52021-07-15 10:42:25 -0500606 self.logger.info("\n\tCopying DUMP files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500607
George Keishingf5a57502021-07-22 16:43:47 -0500608 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phanbabf2962021-07-07 11:24:40 -0500609 # If command list is empty, returns
610 if not list_of_commands:
611 return
Peter D Phan56429a62021-06-23 08:38:29 -0500612
Peter D Phanbabf2962021-07-07 11:24:40 -0500613 for command in list_of_commands:
614 try:
615 filename = command.split(' ')[2]
616 except IndexError:
Peter D Phane86d9a52021-07-15 10:42:25 -0500617 self.logger.info("\t\tInvalid command %s for DUMP_LOGS block." % command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500618 continue
619
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500620 cmd_exit_code, err, response = \
621 self.ssh_remoteclient.execute_command(command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500622
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500623 # If file does not exist, code take no action.
624 # cmd_exit_code is ignored for this scenario.
Peter D Phan56429a62021-06-23 08:38:29 -0500625 if response:
Peter D Phan5963d632021-07-12 09:58:55 -0500626 scp_result = self.ssh_remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
Peter D Phan56429a62021-06-23 08:38:29 -0500627 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500628 self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
Peter D Phan56429a62021-06-23 08:38:29 -0500629 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500630 self.logger.info("\t\tThere is no " + filename)
Peter D Phan56429a62021-06-23 08:38:29 -0500631
632 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500633 self.logger.info("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500634
Peter D Phan72ce6b82021-06-03 06:18:26 -0500635 def scp_ffdc(self,
636 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500637 targ_file_prefix,
638 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500639 file_list=None,
640 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500641 r"""
642 SCP all files in file_dict to the indicated directory on the local system.
643
644 Description of argument(s):
645 targ_dir_path The path of the directory to receive the files.
646 targ_file_prefix Prefix which will be pre-pended to each
647 target file's name.
648 file_dict A dictionary of files to scp from targeted system to this system
649
650 """
651
Peter D Phan72ce6b82021-06-03 06:18:26 -0500652 progress_counter = 0
653 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500654 if form_filename:
655 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500656 source_file_path = filename
657 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
658
Peter D Phanbabf2962021-07-07 11:24:40 -0500659 # If source file name contains wild card, copy filename as is.
660 if '*' in source_file_path:
Peter D Phan5963d632021-07-12 09:58:55 -0500661 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, self.ffdc_dir_path)
Peter D Phanbabf2962021-07-07 11:24:40 -0500662 else:
Peter D Phan5963d632021-07-12 09:58:55 -0500663 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500664
665 if not quiet:
666 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500667 self.logger.info(
668 "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500669 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500670 self.logger.info(
671 "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500672 else:
673 progress_counter += 1
674 self.print_progress(progress_counter)
675
Peter D Phan72ce6b82021-06-03 06:18:26 -0500676 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500677 r"""
678 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
679 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
680 Individual ffdc file will have timestr_filename.
681
682 Description of class variables:
683 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
684
685 self.ffdc_prefix The prefix to be given to each ffdc file name.
686
687 """
688
689 timestr = time.strftime("%Y%m%d-%H%M%S")
690 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
691 self.ffdc_prefix = timestr + "_"
692 self.validate_local_store(self.ffdc_dir_path)
693
694 def validate_local_store(self, dir_path):
695 r"""
696 Ensure path exists to store FFDC files locally.
697
698 Description of variable:
699 dir_path The dir path where collected ffdc data files will be stored.
700
701 """
702
703 if not os.path.exists(dir_path):
704 try:
George Keishing7b3a5132021-07-13 09:24:02 -0500705 os.makedirs(dir_path, 0o755)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500706 except (IOError, OSError) as e:
707 # PermissionError
708 if e.errno == EPERM or e.errno == EACCES:
Peter D Phane86d9a52021-07-15 10:42:25 -0500709 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500710 '\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500711 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500712 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500713 '\tERROR: os.makedirs %s failed with %s.\n' % (dir_path, e.strerror))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500714 sys.exit(-1)
715
716 def print_progress(self, progress):
717 r"""
718 Print activity progress +
719
720 Description of variable:
721 progress Progress counter.
722
723 """
724
725 sys.stdout.write("\r\t" + "+" * progress)
726 sys.stdout.flush()
727 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500728
729 def verify_redfish(self):
730 r"""
731 Verify remote host has redfish service active
732
733 """
734 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
735 + self.hostname + ' -S Always raw GET /redfish/v1/'
736 return(self.run_redfishtool(redfish_parm, True))
737
George Keishingeafba182021-06-29 13:44:58 -0500738 def verify_ipmi(self):
739 r"""
740 Verify remote host has IPMI LAN service active
741
742 """
George Keishing484f8242021-07-27 01:42:02 -0500743 if self.target_type == 'OPENBMC':
744 ipmi_parm = 'ipmitool -I lanplus -C 17 -U ' + self.username + ' -P ' \
745 + self.password + ' -H ' + self.hostname + ' power status'
746 else:
747 ipmi_parm = 'ipmitool -I lanplus -P ' \
748 + self.password + ' -H ' + self.hostname + ' power status'
749
George Keishingeafba182021-06-29 13:44:58 -0500750 return(self.run_ipmitool(ipmi_parm, True))
751
Peter D Phan0c669772021-06-24 13:52:42 -0500752 def run_redfishtool(self,
753 parms_string,
754 quiet=False):
755 r"""
756 Run CLI redfishtool
757
758 Description of variable:
759 parms_string redfishtool subcommand and options.
760 quiet do not print redfishtool error message if True
761 """
762
763 result = subprocess.run(['redfishtool ' + parms_string],
764 stdout=subprocess.PIPE,
765 stderr=subprocess.PIPE,
766 shell=True,
767 universal_newlines=True)
768
769 if result.stderr and not quiet:
George Keishing7bf55092021-07-22 12:33:34 -0500770 self.logger.error('\n\tERROR with redfishtool ' + parms_string)
771 self.logger.error('\n\t%s' % result.stderr.split('\n'))
Peter D Phan0c669772021-06-24 13:52:42 -0500772
773 return result.stdout
George Keishingeafba182021-06-29 13:44:58 -0500774
775 def run_ipmitool(self,
776 parms_string,
777 quiet=False):
778 r"""
779 Run CLI IPMI tool.
780
781 Description of variable:
782 parms_string ipmitool subcommand and options.
783 quiet do not print redfishtool error message if True
784 """
785
George Keishing484f8242021-07-27 01:42:02 -0500786 result = subprocess.run([parms_string],
George Keishingeafba182021-06-29 13:44:58 -0500787 stdout=subprocess.PIPE,
788 stderr=subprocess.PIPE,
789 shell=True,
790 universal_newlines=True)
791
792 if result.stderr and not quiet:
George Keishing484f8242021-07-27 01:42:02 -0500793 self.logger.error('\n\t\tERROR with %s ' % parms_string)
Peter D Phane86d9a52021-07-15 10:42:25 -0500794 self.logger.error('\t\t' + result.stderr)
George Keishingeafba182021-06-29 13:44:58 -0500795
796 return result.stdout
George Keishing04d29102021-07-16 02:05:57 -0500797
798 def run_shell_script(self,
799 parms_string,
800 quiet=False):
801 r"""
802 Run CLI shell script tool.
803
804 Description of variable:
805 parms_string script command options.
806 quiet do not print redfishtool error message if True
807 """
808
809 result = subprocess.run([parms_string],
810 stdout=subprocess.PIPE,
811 stderr=subprocess.PIPE,
812 shell=True,
813 universal_newlines=True)
814
815 if result.stderr and not quiet:
816 self.logger.error('\n\t\tERROR executing %s' % parms_string)
817 self.logger.error('\t\t' + result.stderr)
818
819 return result.stdout
820
821 def protocol_shell_script(self,
George Keishingf5a57502021-07-22 16:43:47 -0500822 target_type,
George Keishing04d29102021-07-16 02:05:57 -0500823 sub_type):
824 r"""
825 Perform SHELL script execution locally.
826
827 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500828 target_type OS Type of remote host.
George Keishing04d29102021-07-16 02:05:57 -0500829 sub_type Group type of commands.
830 """
831
832 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'SHELL'))
833 shell_files_saved = []
834 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500835 list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type])
George Keishing04d29102021-07-16 02:05:57 -0500836 for index, each_cmd in enumerate(list_of_cmd, start=0):
837
838 result = self.run_shell_script(each_cmd)
839 if result:
840 try:
George Keishingf5a57502021-07-22 16:43:47 -0500841 targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
George Keishing04d29102021-07-16 02:05:57 -0500842 except IndexError:
843 targ_file = each_cmd.split('/')[-1]
844 self.logger.warning("\n\t[WARN] Missing filename to store data %s." % each_cmd)
845 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
846
847 targ_file_with_path = (self.ffdc_dir_path
848 + self.ffdc_prefix
849 + targ_file)
850
851 # Creates a new file
852 with open(targ_file_with_path, 'w') as fp:
853 fp.write(result)
854 fp.close
855 shell_files_saved.append(targ_file)
856
857 progress_counter += 1
858 self.print_progress(progress_counter)
859
860 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
861
862 for file in shell_files_saved:
863 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
George Keishingf5a57502021-07-22 16:43:47 -0500864
865 def verify_protocol(self, protocol_list):
866 r"""
867 Perform protocol working check.
868
869 Description of argument(s):
870 protocol_list List of protocol.
871 """
872
873 tmp_list = []
874 if self.target_is_pingable():
875 tmp_list.append("SHELL")
876
877 for protocol in protocol_list:
878 if self.remote_protocol != 'ALL':
879 if self.remote_protocol != protocol:
880 continue
881
882 # Only check SSH/SCP once for both protocols
883 if protocol == 'SSH' or protocol == 'SCP' and protocol not in tmp_list:
884 if self.ssh_to_target_system():
George Keishingaa638702021-07-26 11:48:28 -0500885 # Add only what user asked.
886 if self.remote_protocol != 'ALL':
887 tmp_list.append(self.remote_protocol)
888 else:
889 tmp_list.append('SSH')
890 tmp_list.append('SCP')
George Keishingf5a57502021-07-22 16:43:47 -0500891
892 if protocol == 'TELNET':
893 if self.telnet_to_target_system():
894 tmp_list.append(protocol)
895
896 if protocol == 'REDFISH':
897 if self.verify_redfish():
898 tmp_list.append(protocol)
899 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
900 else:
901 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname)
902
903 if protocol == 'IPMI':
904 if self.verify_ipmi():
905 tmp_list.append(protocol)
906 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
907 else:
908 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname)
909
910 return tmp_list