blob: f6a5a9c6d739709f5cd823b46a746cf9e61c196b [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Peter D Phan72ce6b82021-06-03 06:18:26 -05002
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 Phan5e56f522021-12-20 13:19:41 -060017
Peter D Phancb791d72022-02-08 12:23:03 -060018script_dir = os.path.dirname(os.path.abspath(__file__))
19sys.path.append(script_dir)
20# Walk path and append to sys.path
21for root, dirs, files in os.walk(script_dir):
22 for dir in dirs:
23 sys.path.append(os.path.join(root, dir))
24
Peter D Phan72ce6b82021-06-03 06:18:26 -050025from ssh_utility import SSHRemoteclient
Peter D Phan5963d632021-07-12 09:58:55 -050026from telnet_utility import TelnetRemoteclient
Peter D Phan72ce6b82021-06-03 06:18:26 -050027
George Keishingb97a9042021-07-29 07:41:20 -050028r"""
29User define plugins python functions.
30
31It will imports files from directory plugins
32
33plugins
34├── file1.py
35└── file2.py
36
37Example how to define in YAML:
38 - plugin:
39 - plugin_name: plugin.foo_func.foo_func_yaml
40 - plugin_args:
41 - arg1
42 - arg2
43"""
Peter D Phan5e56f522021-12-20 13:19:41 -060044plugin_dir = __file__.split(__file__.split("/")[-1])[0] + '/plugins'
45sys.path.append(plugin_dir)
George Keishingb97a9042021-07-29 07:41:20 -050046try:
47 for module in os.listdir(plugin_dir):
48 if module == '__init__.py' or module[-3:] != '.py':
49 continue
50 plugin_module = "plugins." + module[:-3]
51 # To access the module plugin.<module name>.<function>
52 # Example: plugin.foo_func.foo_func_yaml()
53 try:
54 plugin = __import__(plugin_module, globals(), locals(), [], 0)
55 except Exception as e:
56 print("PLUGIN: Module import failed: %s" % module)
57 pass
58except FileNotFoundError as e:
59 print("PLUGIN: %s" % e)
60 pass
61
62r"""
63This is for plugin functions returning data or responses to the caller
64in YAML plugin setup.
65
66Example:
67
68 - plugin:
69 - plugin_name: version = plugin.ssh_execution.ssh_execute_cmd
70 - plugin_args:
71 - ${hostname}
72 - ${username}
73 - ${password}
74 - "cat /etc/os-release | grep VERSION_ID | awk -F'=' '{print $2}'"
75 - plugin:
76 - plugin_name: plugin.print_vars.print_vars
77 - plugin_args:
78 - version
79
80where first plugin "version" var is used by another plugin in the YAML
81block or plugin
82
83"""
84global global_log_store_path
85global global_plugin_dict
86global global_plugin_list
George Keishing9348b402021-08-13 12:22:35 -050087
George Keishing0581cb02021-08-05 15:08:58 -050088# Hold the plugin return values in dict and plugin return vars in list.
George Keishing9348b402021-08-13 12:22:35 -050089# Dict is to reference and update vars processing in parser where as
90# list is for current vars from the plugin block which needs processing.
George Keishingb97a9042021-07-29 07:41:20 -050091global_plugin_dict = {}
92global_plugin_list = []
George Keishing9348b402021-08-13 12:22:35 -050093
George Keishing0581cb02021-08-05 15:08:58 -050094# Hold the plugin return named declared if function returned values are list,dict.
95# Refer this name list to look up the plugin dict for eval() args function
George Keishing9348b402021-08-13 12:22:35 -050096# Example ['version']
George Keishing0581cb02021-08-05 15:08:58 -050097global_plugin_type_list = []
George Keishing9348b402021-08-13 12:22:35 -050098
99# Path where logs are to be stored or written.
George Keishingb97a9042021-07-29 07:41:20 -0500100global_log_store_path = ''
101
George Keishing1e7b0182021-08-06 14:05:54 -0500102# Plugin error state defaults.
103plugin_error_dict = {
104 'exit_on_error': False,
105 'continue_on_error': False,
106}
107
Peter D Phan72ce6b82021-06-03 06:18:26 -0500108
Peter D Phan5e56f522021-12-20 13:19:41 -0600109class ffdc_collector:
Peter D Phan72ce6b82021-06-03 06:18:26 -0500110
111 r"""
George Keishing1e7b0182021-08-06 14:05:54 -0500112 Execute commands from configuration file to collect log files.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500113 Fetch and store generated files at the specified location.
114
115 """
116
Peter D Phan0c669772021-06-24 13:52:42 -0500117 def __init__(self,
118 hostname,
119 username,
120 password,
121 ffdc_config,
122 location,
123 remote_type,
Peter D Phane86d9a52021-07-15 10:42:25 -0500124 remote_protocol,
George Keishing4885b2f2021-07-21 15:22:45 -0500125 env_vars,
George Keishing8e94f8c2021-07-23 15:06:32 -0500126 econfig,
Peter D Phane86d9a52021-07-15 10:42:25 -0500127 log_level):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500128 r"""
129 Description of argument(s):
130
George Keishing8e94f8c2021-07-23 15:06:32 -0500131 hostname name/ip of the targeted (remote) system
132 username user on the targeted system with access to FFDC files
133 password password for user on targeted system
134 ffdc_config configuration file listing commands and files for FFDC
135 location where to store collected FFDC
136 remote_type os type of the remote host
137 remote_protocol Protocol to use to collect data
138 env_vars User define CLI env vars '{"key : "value"}'
139 econfig User define env vars YAML file
Peter D Phan72ce6b82021-06-03 06:18:26 -0500140
141 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500142
143 self.hostname = hostname
144 self.username = username
145 self.password = password
146 self.ffdc_config = ffdc_config
147 self.location = location + "/" + remote_type.upper()
148 self.ssh_remoteclient = None
149 self.telnet_remoteclient = None
150 self.ffdc_dir_path = ""
151 self.ffdc_prefix = ""
152 self.target_type = remote_type.upper()
153 self.remote_protocol = remote_protocol.upper()
George Keishinge1686752021-07-27 12:55:28 -0500154 self.env_vars = env_vars
155 self.econfig = econfig
Peter D Phane86d9a52021-07-15 10:42:25 -0500156 self.start_time = 0
157 self.elapsed_time = ''
158 self.logger = None
159
160 # Set prefix values for scp files and directory.
161 # Since the time stamp is at second granularity, these values are set here
162 # to be sure that all files for this run will have same timestamps
163 # and they will be saved in the same directory.
164 # self.location == local system for now
Peter D Phan5e56f522021-12-20 13:19:41 -0600165 self.set_ffdc_default_store_path()
Peter D Phane86d9a52021-07-15 10:42:25 -0500166
Peter D Phan5e56f522021-12-20 13:19:41 -0600167 # Logger for this run. Need to be after set_ffdc_default_store_path()
Peter D Phane86d9a52021-07-15 10:42:25 -0500168 self.script_logging(getattr(logging, log_level.upper()))
169
170 # Verify top level directory exists for storage
171 self.validate_local_store(self.location)
172
Peter D Phan72ce6b82021-06-03 06:18:26 -0500173 if self.verify_script_env():
Peter D Phane86d9a52021-07-15 10:42:25 -0500174 # Load default or user define YAML configuration file.
175 with open(self.ffdc_config, 'r') as file:
George Keishinge9b23d32021-08-13 12:57:58 -0500176 try:
177 self.ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
178 except yaml.YAMLError as e:
179 self.logger.error(e)
180 sys.exit(-1)
Peter D Phane86d9a52021-07-15 10:42:25 -0500181
182 if self.target_type not in self.ffdc_actions.keys():
183 self.logger.error(
184 "\n\tERROR: %s is not listed in %s.\n\n" % (self.target_type, self.ffdc_config))
185 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500186 else:
Peter D Phan8462faf2021-06-16 12:24:15 -0500187 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500188
George Keishing4885b2f2021-07-21 15:22:45 -0500189 # Load ENV vars from user.
George Keishingaa1f8482021-07-22 00:54:55 -0500190 self.logger.info("\n\tENV: User define input YAML variables")
191 self.env_dict = {}
Peter D Phan5e56f522021-12-20 13:19:41 -0600192 self.load_env()
George Keishingaa1f8482021-07-22 00:54:55 -0500193
Peter D Phan72ce6b82021-06-03 06:18:26 -0500194 def verify_script_env(self):
195
196 # Import to log version
197 import click
198 import paramiko
199
200 run_env_ok = True
Peter D Phan0c669772021-06-24 13:52:42 -0500201
George Keishing506b0582021-07-27 09:31:22 -0500202 redfishtool_version = self.run_tool_cmd('redfishtool -V').split(' ')[2].strip('\n')
203 ipmitool_version = self.run_tool_cmd('ipmitool -V').split(' ')[2]
Peter D Phan0c669772021-06-24 13:52:42 -0500204
Peter D Phane86d9a52021-07-15 10:42:25 -0500205 self.logger.info("\n\t---- Script host environment ----")
206 self.logger.info("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
207 self.logger.info("\t{:<10} {:<10}".format('Script host os', platform.platform()))
208 self.logger.info("\t{:<10} {:>10}".format('Python', platform.python_version()))
209 self.logger.info("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
210 self.logger.info("\t{:<10} {:>10}".format('click', click.__version__))
211 self.logger.info("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
212 self.logger.info("\t{:<10} {:>9}".format('redfishtool', redfishtool_version))
213 self.logger.info("\t{:<10} {:>12}".format('ipmitool', ipmitool_version))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500214
Peter D Phanb2890812022-01-25 05:39:12 -0600215 if eval(yaml.__version__.replace('.', ',')) < (5, 3, 0):
Peter D Phane86d9a52021-07-15 10:42:25 -0500216 self.logger.error("\n\tERROR: Python or python packages do not meet minimum version requirement.")
Peter D Phanb2890812022-01-25 05:39:12 -0600217 self.logger.error("\tERROR: PyYAML version 5.3.0 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500218 run_env_ok = False
219
Peter D Phane86d9a52021-07-15 10:42:25 -0500220 self.logger.info("\t---- End script host environment ----")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500221 return run_env_ok
222
Peter D Phane86d9a52021-07-15 10:42:25 -0500223 def script_logging(self,
224 log_level_attr):
225 r"""
226 Create logger
227
228 """
229 self.logger = logging.getLogger()
230 self.logger.setLevel(log_level_attr)
231 log_file_handler = logging.FileHandler(self.ffdc_dir_path + "collector.log")
232
233 stdout_handler = logging.StreamHandler(sys.stdout)
234 self.logger.addHandler(log_file_handler)
235 self.logger.addHandler(stdout_handler)
236
237 # Turn off paramiko INFO logging
238 logging.getLogger("paramiko").setLevel(logging.WARNING)
239
Peter D Phan72ce6b82021-06-03 06:18:26 -0500240 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500241 r"""
242 Check if target system is ping-able.
243
244 """
George Keishing0662e942021-07-13 05:12:20 -0500245 response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500246 if response == 0:
Peter D Phane86d9a52021-07-15 10:42:25 -0500247 self.logger.info("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500248 return True
249 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500250 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500251 "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500252 sys.exit(-1)
253
Peter D Phan72ce6b82021-06-03 06:18:26 -0500254 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500255 r"""
256 Initiate FFDC Collection depending on requested protocol.
257
258 """
259
Peter D Phane86d9a52021-07-15 10:42:25 -0500260 self.logger.info("\n\t---- Start communicating with %s ----" % self.hostname)
Peter D Phan7610bc42021-07-06 06:31:05 -0500261 self.start_time = time.time()
Peter D Phan0c669772021-06-24 13:52:42 -0500262
George Keishingf5a57502021-07-22 16:43:47 -0500263 # Find the list of target and protocol supported.
264 check_protocol_list = []
265 config_dict = self.ffdc_actions
Peter D Phan0c669772021-06-24 13:52:42 -0500266
George Keishingf5a57502021-07-22 16:43:47 -0500267 for target_type in config_dict.keys():
268 if self.target_type != target_type:
269 continue
George Keishingeafba182021-06-29 13:44:58 -0500270
George Keishingf5a57502021-07-22 16:43:47 -0500271 for k, v in config_dict[target_type].items():
272 if config_dict[target_type][k]['PROTOCOL'][0] not in check_protocol_list:
273 check_protocol_list.append(config_dict[target_type][k]['PROTOCOL'][0])
Peter D Phanbff617a2021-07-22 08:41:35 -0500274
George Keishingf5a57502021-07-22 16:43:47 -0500275 self.logger.info("\n\t %s protocol type: %s" % (self.target_type, check_protocol_list))
Peter D Phanbff617a2021-07-22 08:41:35 -0500276
George Keishingf5a57502021-07-22 16:43:47 -0500277 verified_working_protocol = self.verify_protocol(check_protocol_list)
Peter D Phanbff617a2021-07-22 08:41:35 -0500278
George Keishingf5a57502021-07-22 16:43:47 -0500279 if verified_working_protocol:
Peter D Phane86d9a52021-07-15 10:42:25 -0500280 self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500281
George Keishingf5a57502021-07-22 16:43:47 -0500282 # Verify top level directory exists for storage
283 self.validate_local_store(self.location)
284
285 if ((self.remote_protocol not in verified_working_protocol) and (self.remote_protocol != 'ALL')):
286 self.logger.info("\n\tWorking protocol list: %s" % verified_working_protocol)
287 self.logger.error(
288 '\tERROR: Requested protocol %s is not in working protocol list.\n'
289 % self.remote_protocol)
290 sys.exit(-1)
291 else:
292 self.generate_ffdc(verified_working_protocol)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500293
294 def ssh_to_target_system(self):
295 r"""
296 Open a ssh connection to targeted system.
297
298 """
299
Peter D Phan5963d632021-07-12 09:58:55 -0500300 self.ssh_remoteclient = SSHRemoteclient(self.hostname,
301 self.username,
302 self.password)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500303
Peter D Phan5963d632021-07-12 09:58:55 -0500304 if self.ssh_remoteclient.ssh_remoteclient_login():
Peter D Phane86d9a52021-07-15 10:42:25 -0500305 self.logger.info("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500306
Peter D Phan5963d632021-07-12 09:58:55 -0500307 # Check scp connection.
308 # If scp connection fails,
309 # continue with FFDC generation but skip scp files to local host.
310 self.ssh_remoteclient.scp_connection()
311 return True
312 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500313 self.logger.info("\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500314 return False
315
316 def telnet_to_target_system(self):
317 r"""
318 Open a telnet connection to targeted system.
319 """
320 self.telnet_remoteclient = TelnetRemoteclient(self.hostname,
321 self.username,
322 self.password)
323 if self.telnet_remoteclient.tn_remoteclient_login():
Peter D Phane86d9a52021-07-15 10:42:25 -0500324 self.logger.info("\n\t[Check] %s Telnet connection established.\t [OK]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500325 return True
326 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500327 self.logger.info("\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500328 return False
Peter D Phan72ce6b82021-06-03 06:18:26 -0500329
George Keishing772c9772021-06-16 23:23:42 -0500330 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500331 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500332 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500333
Peter D Phan04aca3b2021-06-21 10:37:18 -0500334 Description of argument(s):
335 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500336 """
337
Peter D Phane86d9a52021-07-15 10:42:25 -0500338 self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----")
339 self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500340
George Keishingf5a57502021-07-22 16:43:47 -0500341 config_dict = self.ffdc_actions
342 for target_type in config_dict.keys():
343 if self.target_type != target_type:
George Keishing6ea92b02021-07-01 11:20:50 -0500344 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500345
Peter D Phane86d9a52021-07-15 10:42:25 -0500346 self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path)
George Keishingb97a9042021-07-29 07:41:20 -0500347 global_plugin_dict['global_log_store_path'] = self.ffdc_dir_path
George Keishingf5a57502021-07-22 16:43:47 -0500348 self.logger.info("\tSystem Type: %s" % target_type)
349 for k, v in config_dict[target_type].items():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500350
George Keishingf5a57502021-07-22 16:43:47 -0500351 if self.remote_protocol not in working_protocol_list \
George Keishing6ea92b02021-07-01 11:20:50 -0500352 and self.remote_protocol != 'ALL':
353 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500354
George Keishingf5a57502021-07-22 16:43:47 -0500355 protocol = config_dict[target_type][k]['PROTOCOL'][0]
356
357 if protocol not in working_protocol_list:
358 continue
359
George Keishingb7607612021-07-27 13:31:23 -0500360 if protocol in working_protocol_list:
361 if protocol == 'SSH' or protocol == 'SCP':
George Keishing12fd0652021-07-27 13:57:11 -0500362 self.protocol_ssh(protocol, target_type, k)
George Keishingb7607612021-07-27 13:31:23 -0500363 elif protocol == 'TELNET':
George Keishingf5a57502021-07-22 16:43:47 -0500364 self.protocol_telnet(target_type, k)
George Keishingb7607612021-07-27 13:31:23 -0500365 elif protocol == 'REDFISH' or protocol == 'IPMI' or protocol == 'SHELL':
George Keishing506b0582021-07-27 09:31:22 -0500366 self.protocol_execute(protocol, target_type, k)
George Keishingb7607612021-07-27 13:31:23 -0500367 else:
368 self.logger.error("\n\tERROR: %s is not available for %s." % (protocol, self.hostname))
George Keishingeafba182021-06-29 13:44:58 -0500369
Peter D Phan04aca3b2021-06-21 10:37:18 -0500370 # Close network connection after collecting all files
Peter D Phan7610bc42021-07-06 06:31:05 -0500371 self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time))
Peter D Phanbff617a2021-07-22 08:41:35 -0500372 if self.ssh_remoteclient:
373 self.ssh_remoteclient.ssh_remoteclient_disconnect()
374 if self.telnet_remoteclient:
375 self.telnet_remoteclient.tn_remoteclient_disconnect()
Peter D Phan04aca3b2021-06-21 10:37:18 -0500376
Peter D Phan0c669772021-06-24 13:52:42 -0500377 def protocol_ssh(self,
George Keishing12fd0652021-07-27 13:57:11 -0500378 protocol,
George Keishingf5a57502021-07-22 16:43:47 -0500379 target_type,
George Keishing6ea92b02021-07-01 11:20:50 -0500380 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500381 r"""
382 Perform actions using SSH and SCP protocols.
383
384 Description of argument(s):
George Keishing12fd0652021-07-27 13:57:11 -0500385 protocol Protocol to execute.
George Keishingf5a57502021-07-22 16:43:47 -0500386 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500387 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500388 """
389
George Keishing12fd0652021-07-27 13:57:11 -0500390 if protocol == 'SCP':
George Keishingf5a57502021-07-22 16:43:47 -0500391 self.group_copy(self.ffdc_actions[target_type][sub_type])
George Keishing6ea92b02021-07-01 11:20:50 -0500392 else:
George Keishingf5a57502021-07-22 16:43:47 -0500393 self.collect_and_copy_ffdc(self.ffdc_actions[target_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500394
Peter D Phan5963d632021-07-12 09:58:55 -0500395 def protocol_telnet(self,
George Keishingf5a57502021-07-22 16:43:47 -0500396 target_type,
Peter D Phan5963d632021-07-12 09:58:55 -0500397 sub_type):
398 r"""
399 Perform actions using telnet protocol.
400 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500401 target_type OS Type of remote host.
Peter D Phan5963d632021-07-12 09:58:55 -0500402 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500403 self.logger.info("\n\t[Run] Executing commands on %s using %s" % (self.hostname, 'TELNET'))
Peter D Phan5963d632021-07-12 09:58:55 -0500404 telnet_files_saved = []
405 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500406 list_of_commands = self.ffdc_actions[target_type][sub_type]['COMMANDS']
Peter D Phan5963d632021-07-12 09:58:55 -0500407 for index, each_cmd in enumerate(list_of_commands, start=0):
408 command_txt, command_timeout = self.unpack_command(each_cmd)
409 result = self.telnet_remoteclient.execute_command(command_txt, command_timeout)
410 if result:
411 try:
George Keishingf5a57502021-07-22 16:43:47 -0500412 targ_file = self.ffdc_actions[target_type][sub_type]['FILES'][index]
Peter D Phan5963d632021-07-12 09:58:55 -0500413 except IndexError:
Peter D Phane86d9a52021-07-15 10:42:25 -0500414 targ_file = command_txt
415 self.logger.warning(
416 "\n\t[WARN] Missing filename to store data from telnet %s." % each_cmd)
417 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
Peter D Phan5963d632021-07-12 09:58:55 -0500418 targ_file_with_path = (self.ffdc_dir_path
419 + self.ffdc_prefix
420 + targ_file)
421 # Creates a new file
Peter D Phanb76e1752021-08-03 12:50:05 -0500422 with open(targ_file_with_path, 'w') as fp:
Peter D Phan5963d632021-07-12 09:58:55 -0500423 fp.write(result)
424 fp.close
425 telnet_files_saved.append(targ_file)
426 progress_counter += 1
427 self.print_progress(progress_counter)
Peter D Phane86d9a52021-07-15 10:42:25 -0500428 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan5963d632021-07-12 09:58:55 -0500429 for file in telnet_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500430 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
Peter D Phan5963d632021-07-12 09:58:55 -0500431
George Keishing506b0582021-07-27 09:31:22 -0500432 def protocol_execute(self,
433 protocol,
George Keishingf5a57502021-07-22 16:43:47 -0500434 target_type,
George Keishing6ea92b02021-07-01 11:20:50 -0500435 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500436 r"""
George Keishing506b0582021-07-27 09:31:22 -0500437 Perform actions for a given protocol.
Peter D Phan0c669772021-06-24 13:52:42 -0500438
439 Description of argument(s):
George Keishing506b0582021-07-27 09:31:22 -0500440 protocol Protocol to execute.
George Keishingf5a57502021-07-22 16:43:47 -0500441 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500442 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500443 """
444
George Keishing506b0582021-07-27 09:31:22 -0500445 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, protocol))
446 executed_files_saved = []
George Keishingeafba182021-06-29 13:44:58 -0500447 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500448 list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type])
George Keishingeafba182021-06-29 13:44:58 -0500449 for index, each_cmd in enumerate(list_of_cmd, start=0):
George Keishingcaa97e62021-08-03 14:00:09 -0500450 plugin_call = False
George Keishingb97a9042021-07-29 07:41:20 -0500451 if isinstance(each_cmd, dict):
452 if 'plugin' in each_cmd:
George Keishing1e7b0182021-08-06 14:05:54 -0500453 # If the error is set and plugin explicitly
454 # requested to skip execution on error..
455 if plugin_error_dict['exit_on_error'] and \
456 self.plugin_error_check(each_cmd['plugin']):
457 self.logger.info("\n\t[PLUGIN-ERROR] exit_on_error: %s" %
458 plugin_error_dict['exit_on_error'])
459 self.logger.info("\t[PLUGIN-SKIP] %s" %
460 each_cmd['plugin'][0])
461 continue
George Keishingcaa97e62021-08-03 14:00:09 -0500462 plugin_call = True
George Keishingb97a9042021-07-29 07:41:20 -0500463 # call the plugin
464 self.logger.info("\n\t[PLUGIN-START]")
George Keishingcaa97e62021-08-03 14:00:09 -0500465 result = self.execute_plugin_block(each_cmd['plugin'])
George Keishingb97a9042021-07-29 07:41:20 -0500466 self.logger.info("\t[PLUGIN-END]\n")
George Keishingb97a9042021-07-29 07:41:20 -0500467 else:
George Keishing2b83e042021-08-03 12:56:11 -0500468 each_cmd = self.yaml_env_and_plugin_vars_populate(each_cmd)
George Keishingb97a9042021-07-29 07:41:20 -0500469
George Keishingcaa97e62021-08-03 14:00:09 -0500470 if not plugin_call:
471 result = self.run_tool_cmd(each_cmd)
George Keishingeafba182021-06-29 13:44:58 -0500472 if result:
473 try:
George Keishingcaa97e62021-08-03 14:00:09 -0500474 file_name = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
George Keishingb97a9042021-07-29 07:41:20 -0500475 # If file is specified as None.
George Keishing0581cb02021-08-05 15:08:58 -0500476 if file_name == "None":
George Keishingb97a9042021-07-29 07:41:20 -0500477 continue
George Keishing0581cb02021-08-05 15:08:58 -0500478 targ_file = self.yaml_env_and_plugin_vars_populate(file_name)
George Keishingeafba182021-06-29 13:44:58 -0500479 except IndexError:
George Keishing6ea92b02021-07-01 11:20:50 -0500480 targ_file = each_cmd.split('/')[-1]
George Keishing506b0582021-07-27 09:31:22 -0500481 self.logger.warning(
482 "\n\t[WARN] Missing filename to store data from %s." % each_cmd)
Peter D Phane86d9a52021-07-15 10:42:25 -0500483 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500484
485 targ_file_with_path = (self.ffdc_dir_path
486 + self.ffdc_prefix
487 + targ_file)
488
489 # Creates a new file
490 with open(targ_file_with_path, 'w') as fp:
George Keishing91308ea2021-08-10 14:43:15 -0500491 if isinstance(result, dict):
492 fp.write(json.dumps(result))
493 else:
494 fp.write(result)
George Keishingeafba182021-06-29 13:44:58 -0500495 fp.close
George Keishing506b0582021-07-27 09:31:22 -0500496 executed_files_saved.append(targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500497
498 progress_counter += 1
499 self.print_progress(progress_counter)
500
Peter D Phane86d9a52021-07-15 10:42:25 -0500501 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
George Keishingeafba182021-06-29 13:44:58 -0500502
George Keishing506b0582021-07-27 09:31:22 -0500503 for file in executed_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500504 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
George Keishingeafba182021-06-29 13:44:58 -0500505
Peter D Phan04aca3b2021-06-21 10:37:18 -0500506 def collect_and_copy_ffdc(self,
George Keishingf5a57502021-07-22 16:43:47 -0500507 ffdc_actions_for_target_type,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500508 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500509 r"""
510 Send commands in ffdc_config file to targeted system.
511
512 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500513 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500514 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500515 """
516
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500517 # Executing commands, if any
George Keishingf5a57502021-07-22 16:43:47 -0500518 self.ssh_execute_ffdc_commands(ffdc_actions_for_target_type,
Peter D Phan3beb02e2021-07-06 13:25:17 -0500519 form_filename)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500520
Peter D Phan3beb02e2021-07-06 13:25:17 -0500521 # Copying files
Peter D Phan5963d632021-07-12 09:58:55 -0500522 if self.ssh_remoteclient.scpclient:
Peter D Phane86d9a52021-07-15 10:42:25 -0500523 self.logger.info("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500524
Peter D Phan04aca3b2021-06-21 10:37:18 -0500525 # Retrieving files from target system
George Keishingf5a57502021-07-22 16:43:47 -0500526 list_of_files = self.get_file_list(ffdc_actions_for_target_type)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500527 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500528 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500529 self.logger.info("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500530
Peter D Phanbabf2962021-07-07 11:24:40 -0500531 def get_command_list(self,
George Keishingf5a57502021-07-22 16:43:47 -0500532 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500533 r"""
534 Fetch list of commands from configuration file
535
536 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500537 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500538 """
539 try:
George Keishingf5a57502021-07-22 16:43:47 -0500540 list_of_commands = ffdc_actions_for_target_type['COMMANDS']
Peter D Phanbabf2962021-07-07 11:24:40 -0500541 except KeyError:
542 list_of_commands = []
543 return list_of_commands
544
545 def get_file_list(self,
George Keishingf5a57502021-07-22 16:43:47 -0500546 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500547 r"""
548 Fetch list of commands from configuration file
549
550 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500551 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500552 """
553 try:
George Keishingf5a57502021-07-22 16:43:47 -0500554 list_of_files = ffdc_actions_for_target_type['FILES']
Peter D Phanbabf2962021-07-07 11:24:40 -0500555 except KeyError:
556 list_of_files = []
557 return list_of_files
558
Peter D Phan5963d632021-07-12 09:58:55 -0500559 def unpack_command(self,
560 command):
561 r"""
562 Unpack command from config file
563
564 Description of argument(s):
565 command Command from config file.
566 """
567 if isinstance(command, dict):
568 command_txt = next(iter(command))
569 command_timeout = next(iter(command.values()))
570 elif isinstance(command, str):
571 command_txt = command
572 # Default command timeout 60 seconds
573 command_timeout = 60
574
575 return command_txt, command_timeout
576
Peter D Phan3beb02e2021-07-06 13:25:17 -0500577 def ssh_execute_ffdc_commands(self,
George Keishingf5a57502021-07-22 16:43:47 -0500578 ffdc_actions_for_target_type,
Peter D Phan3beb02e2021-07-06 13:25:17 -0500579 form_filename=False):
580 r"""
581 Send commands in ffdc_config file to targeted system.
582
583 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500584 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan3beb02e2021-07-06 13:25:17 -0500585 form_filename if true, pre-pend self.target_type to filename
586 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500587 self.logger.info("\n\t[Run] Executing commands on %s using %s"
George Keishingf5a57502021-07-22 16:43:47 -0500588 % (self.hostname, ffdc_actions_for_target_type['PROTOCOL'][0]))
Peter D Phan3beb02e2021-07-06 13:25:17 -0500589
George Keishingf5a57502021-07-22 16:43:47 -0500590 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500591 # If command list is empty, returns
592 if not list_of_commands:
593 return
594
595 progress_counter = 0
596 for command in list_of_commands:
Peter D Phan5963d632021-07-12 09:58:55 -0500597 command_txt, command_timeout = self.unpack_command(command)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500598
599 if form_filename:
600 command_txt = str(command_txt % self.target_type)
601
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500602 cmd_exit_code, err, response = \
603 self.ssh_remoteclient.execute_command(command_txt, command_timeout)
604
605 if cmd_exit_code:
606 self.logger.warning(
607 "\n\t\t[WARN] %s exits with code %s." % (command_txt, str(cmd_exit_code)))
608 self.logger.warning("\t\t[WARN] %s " % err)
Peter D Phanbabf2962021-07-07 11:24:40 -0500609
Peter D Phan3beb02e2021-07-06 13:25:17 -0500610 progress_counter += 1
611 self.print_progress(progress_counter)
612
Peter D Phane86d9a52021-07-15 10:42:25 -0500613 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan3beb02e2021-07-06 13:25:17 -0500614
Peter D Phan56429a62021-06-23 08:38:29 -0500615 def group_copy(self,
George Keishingf5a57502021-07-22 16:43:47 -0500616 ffdc_actions_for_target_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500617 r"""
618 scp group of files (wild card) from remote host.
619
620 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500621 fdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan56429a62021-06-23 08:38:29 -0500622 """
Peter D Phan3beb02e2021-07-06 13:25:17 -0500623
Peter D Phan5963d632021-07-12 09:58:55 -0500624 if self.ssh_remoteclient.scpclient:
George Keishing12fd0652021-07-27 13:57:11 -0500625 self.logger.info("\n\tCopying files from remote system %s via SCP.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500626
George Keishingf5a57502021-07-22 16:43:47 -0500627 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phanbabf2962021-07-07 11:24:40 -0500628 # If command list is empty, returns
629 if not list_of_commands:
630 return
Peter D Phan56429a62021-06-23 08:38:29 -0500631
Peter D Phanbabf2962021-07-07 11:24:40 -0500632 for command in list_of_commands:
633 try:
George Keishingb4540e72021-08-02 13:48:46 -0500634 command = self.yaml_env_and_plugin_vars_populate(command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500635 except IndexError:
George Keishingb4540e72021-08-02 13:48:46 -0500636 self.logger.error("\t\tInvalid command %s" % command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500637 continue
638
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500639 cmd_exit_code, err, response = \
640 self.ssh_remoteclient.execute_command(command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500641
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500642 # If file does not exist, code take no action.
643 # cmd_exit_code is ignored for this scenario.
Peter D Phan56429a62021-06-23 08:38:29 -0500644 if response:
George Keishinga56e87b2021-08-06 00:24:19 -0500645 scp_result = \
646 self.ssh_remoteclient.scp_file_from_remote(response.split('\n'),
647 self.ffdc_dir_path)
Peter D Phan56429a62021-06-23 08:38:29 -0500648 if scp_result:
George Keishinga56e87b2021-08-06 00:24:19 -0500649 self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + command)
Peter D Phan56429a62021-06-23 08:38:29 -0500650 else:
George Keishinga56e87b2021-08-06 00:24:19 -0500651 self.logger.info("\t\t%s has no result" % command)
Peter D Phan56429a62021-06-23 08:38:29 -0500652
653 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500654 self.logger.info("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500655
Peter D Phan72ce6b82021-06-03 06:18:26 -0500656 def scp_ffdc(self,
657 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500658 targ_file_prefix,
659 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500660 file_list=None,
661 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500662 r"""
663 SCP all files in file_dict to the indicated directory on the local system.
664
665 Description of argument(s):
666 targ_dir_path The path of the directory to receive the files.
667 targ_file_prefix Prefix which will be pre-pended to each
668 target file's name.
669 file_dict A dictionary of files to scp from targeted system to this system
670
671 """
672
Peter D Phan72ce6b82021-06-03 06:18:26 -0500673 progress_counter = 0
674 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500675 if form_filename:
676 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500677 source_file_path = filename
678 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
679
Peter D Phanbabf2962021-07-07 11:24:40 -0500680 # If source file name contains wild card, copy filename as is.
681 if '*' in source_file_path:
Peter D Phan5963d632021-07-12 09:58:55 -0500682 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, self.ffdc_dir_path)
Peter D Phanbabf2962021-07-07 11:24:40 -0500683 else:
Peter D Phan5963d632021-07-12 09:58:55 -0500684 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500685
686 if not quiet:
687 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500688 self.logger.info(
689 "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500690 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500691 self.logger.info(
692 "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500693 else:
694 progress_counter += 1
695 self.print_progress(progress_counter)
696
Peter D Phan5e56f522021-12-20 13:19:41 -0600697 def set_ffdc_default_store_path(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500698 r"""
699 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
700 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
701 Individual ffdc file will have timestr_filename.
702
703 Description of class variables:
704 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
705
706 self.ffdc_prefix The prefix to be given to each ffdc file name.
707
708 """
709
710 timestr = time.strftime("%Y%m%d-%H%M%S")
711 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
712 self.ffdc_prefix = timestr + "_"
713 self.validate_local_store(self.ffdc_dir_path)
714
Peter D Phan5e56f522021-12-20 13:19:41 -0600715 # Need to verify local store path exists prior to instantiate this class.
716 # This class method is used to share the same code between CLI input parm
717 # and Robot Framework "${EXECDIR}/logs" before referencing this class.
718 @classmethod
719 def validate_local_store(cls, dir_path):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500720 r"""
721 Ensure path exists to store FFDC files locally.
722
723 Description of variable:
724 dir_path The dir path where collected ffdc data files will be stored.
725
726 """
727
728 if not os.path.exists(dir_path):
729 try:
George Keishing7b3a5132021-07-13 09:24:02 -0500730 os.makedirs(dir_path, 0o755)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500731 except (IOError, OSError) as e:
732 # PermissionError
733 if e.errno == EPERM or e.errno == EACCES:
Peter D Phane86d9a52021-07-15 10:42:25 -0500734 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500735 '\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500736 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500737 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500738 '\tERROR: os.makedirs %s failed with %s.\n' % (dir_path, e.strerror))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500739 sys.exit(-1)
740
741 def print_progress(self, progress):
742 r"""
743 Print activity progress +
744
745 Description of variable:
746 progress Progress counter.
747
748 """
749
750 sys.stdout.write("\r\t" + "+" * progress)
751 sys.stdout.flush()
752 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500753
754 def verify_redfish(self):
755 r"""
756 Verify remote host has redfish service active
757
758 """
George Keishing506b0582021-07-27 09:31:22 -0500759 redfish_parm = 'redfishtool -r ' \
Peter D Phan0c669772021-06-24 13:52:42 -0500760 + self.hostname + ' -S Always raw GET /redfish/v1/'
George Keishing506b0582021-07-27 09:31:22 -0500761 return(self.run_tool_cmd(redfish_parm, True))
Peter D Phan0c669772021-06-24 13:52:42 -0500762
George Keishingeafba182021-06-29 13:44:58 -0500763 def verify_ipmi(self):
764 r"""
765 Verify remote host has IPMI LAN service active
766
767 """
George Keishing484f8242021-07-27 01:42:02 -0500768 if self.target_type == 'OPENBMC':
769 ipmi_parm = 'ipmitool -I lanplus -C 17 -U ' + self.username + ' -P ' \
770 + self.password + ' -H ' + self.hostname + ' power status'
771 else:
772 ipmi_parm = 'ipmitool -I lanplus -P ' \
773 + self.password + ' -H ' + self.hostname + ' power status'
774
George Keishing506b0582021-07-27 09:31:22 -0500775 return(self.run_tool_cmd(ipmi_parm, True))
George Keishingeafba182021-06-29 13:44:58 -0500776
George Keishing506b0582021-07-27 09:31:22 -0500777 def run_tool_cmd(self,
George Keishingeafba182021-06-29 13:44:58 -0500778 parms_string,
779 quiet=False):
780 r"""
George Keishing506b0582021-07-27 09:31:22 -0500781 Run CLI standard tool or scripts.
George Keishingeafba182021-06-29 13:44:58 -0500782
783 Description of variable:
George Keishing506b0582021-07-27 09:31:22 -0500784 parms_string tool command options.
785 quiet do not print tool error message if True
George Keishingeafba182021-06-29 13:44:58 -0500786 """
787
George Keishing484f8242021-07-27 01:42:02 -0500788 result = subprocess.run([parms_string],
George Keishingeafba182021-06-29 13:44:58 -0500789 stdout=subprocess.PIPE,
790 stderr=subprocess.PIPE,
791 shell=True,
792 universal_newlines=True)
793
794 if result.stderr and not quiet:
George Keishing484f8242021-07-27 01:42:02 -0500795 self.logger.error('\n\t\tERROR with %s ' % parms_string)
Peter D Phane86d9a52021-07-15 10:42:25 -0500796 self.logger.error('\t\t' + result.stderr)
George Keishingeafba182021-06-29 13:44:58 -0500797
798 return result.stdout
George Keishing04d29102021-07-16 02:05:57 -0500799
George Keishingf5a57502021-07-22 16:43:47 -0500800 def verify_protocol(self, protocol_list):
801 r"""
802 Perform protocol working check.
803
804 Description of argument(s):
805 protocol_list List of protocol.
806 """
807
808 tmp_list = []
809 if self.target_is_pingable():
810 tmp_list.append("SHELL")
811
812 for protocol in protocol_list:
813 if self.remote_protocol != 'ALL':
814 if self.remote_protocol != protocol:
815 continue
816
817 # Only check SSH/SCP once for both protocols
818 if protocol == 'SSH' or protocol == 'SCP' and protocol not in tmp_list:
819 if self.ssh_to_target_system():
George Keishingaa638702021-07-26 11:48:28 -0500820 # Add only what user asked.
821 if self.remote_protocol != 'ALL':
822 tmp_list.append(self.remote_protocol)
823 else:
824 tmp_list.append('SSH')
825 tmp_list.append('SCP')
George Keishingf5a57502021-07-22 16:43:47 -0500826
827 if protocol == 'TELNET':
828 if self.telnet_to_target_system():
829 tmp_list.append(protocol)
830
831 if protocol == 'REDFISH':
832 if self.verify_redfish():
833 tmp_list.append(protocol)
834 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
835 else:
836 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname)
837
838 if protocol == 'IPMI':
839 if self.verify_ipmi():
840 tmp_list.append(protocol)
841 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
842 else:
843 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname)
844
845 return tmp_list
George Keishinge1686752021-07-27 12:55:28 -0500846
847 def load_env(self):
848 r"""
849 Perform protocol working check.
850
851 """
852 # This is for the env vars a user can use in YAML to load it at runtime.
853 # Example YAML:
854 # -COMMANDS:
855 # - my_command ${hostname} ${username} ${password}
856 os.environ['hostname'] = self.hostname
857 os.environ['username'] = self.username
858 os.environ['password'] = self.password
859
860 # Append default Env.
861 self.env_dict['hostname'] = self.hostname
862 self.env_dict['username'] = self.username
863 self.env_dict['password'] = self.password
864
865 try:
866 tmp_env_dict = {}
867 if self.env_vars:
868 tmp_env_dict = json.loads(self.env_vars)
869 # Export ENV vars default.
870 for key, value in tmp_env_dict.items():
871 os.environ[key] = value
872 self.env_dict[key] = str(value)
873
874 if self.econfig:
875 with open(self.econfig, 'r') as file:
George Keishinge9b23d32021-08-13 12:57:58 -0500876 try:
877 tmp_env_dict = yaml.load(file, Loader=yaml.FullLoader)
878 except yaml.YAMLError as e:
879 self.logger.error(e)
880 sys.exit(-1)
George Keishinge1686752021-07-27 12:55:28 -0500881 # Export ENV vars.
882 for key, value in tmp_env_dict['env_params'].items():
883 os.environ[key] = str(value)
884 self.env_dict[key] = str(value)
885 except json.decoder.JSONDecodeError as e:
886 self.logger.error("\n\tERROR: %s " % e)
887 sys.exit(-1)
888
889 # This to mask the password from displaying on the console.
890 mask_dict = self.env_dict.copy()
891 for k, v in mask_dict.items():
892 if k.lower().find("password") != -1:
893 hidden_text = []
894 hidden_text.append(v)
895 password_regex = '(' +\
896 '|'.join([re.escape(x) for x in hidden_text]) + ')'
897 mask_dict[k] = re.sub(password_regex, "********", v)
898
899 self.logger.info(json.dumps(mask_dict, indent=8, sort_keys=False))
George Keishingb97a9042021-07-29 07:41:20 -0500900
901 def execute_python_eval(self, eval_string):
902 r"""
George Keishing9348b402021-08-13 12:22:35 -0500903 Execute qualified python function string using eval.
George Keishingb97a9042021-07-29 07:41:20 -0500904
905 Description of argument(s):
906 eval_string Execute the python object.
907
908 Example:
909 eval(plugin.foo_func.foo_func(10))
910 """
911 try:
George Keishingdda48ce2021-08-12 07:02:27 -0500912 self.logger.info("\tExecuting plugin func()")
913 self.logger.debug("\tCall func: %s" % eval_string)
George Keishingb97a9042021-07-29 07:41:20 -0500914 result = eval(eval_string)
915 self.logger.info("\treturn: %s" % str(result))
George Keishing73b95d12021-08-13 14:30:52 -0500916 except (ValueError,
917 SyntaxError,
918 NameError,
919 AttributeError,
920 TypeError) as e:
George Keishing1e7b0182021-08-06 14:05:54 -0500921 self.logger.error("\tERROR: execute_python_eval: %s" % e)
922 # Set the plugin error state.
923 plugin_error_dict['exit_on_error'] = True
George Keishing73b95d12021-08-13 14:30:52 -0500924 self.logger.info("\treturn: PLUGIN_EVAL_ERROR")
925 return 'PLUGIN_EVAL_ERROR'
George Keishingb97a9042021-07-29 07:41:20 -0500926
927 return result
928
929 def execute_plugin_block(self, plugin_cmd_list):
930 r"""
Peter D Phan5e56f522021-12-20 13:19:41 -0600931 Pack the plugin command to qualifed python string object.
George Keishingb97a9042021-07-29 07:41:20 -0500932
933 Description of argument(s):
934 plugin_list_dict Plugin block read from YAML
935 [{'plugin_name': 'plugin.foo_func.my_func'},
936 {'plugin_args': [10]}]
937
938 Example:
939 - plugin:
940 - plugin_name: plugin.foo_func.my_func
941 - plugin_args:
942 - arg1
943 - arg2
944
945 - plugin:
946 - plugin_name: result = plugin.foo_func.my_func
947 - plugin_args:
948 - arg1
949 - arg2
950
951 - plugin:
952 - plugin_name: result1,result2 = plugin.foo_func.my_func
953 - plugin_args:
954 - arg1
955 - arg2
956 """
957 try:
George Keishinge9b23d32021-08-13 12:57:58 -0500958 idx = self.key_index_list_dict('plugin_name', plugin_cmd_list)
959 plugin_name = plugin_cmd_list[idx]['plugin_name']
George Keishingb97a9042021-07-29 07:41:20 -0500960 # Equal separator means plugin function returns result.
961 if ' = ' in plugin_name:
962 # Ex. ['result', 'plugin.foo_func.my_func']
963 plugin_name_args = plugin_name.split(' = ')
964 # plugin func return data.
965 for arg in plugin_name_args:
966 if arg == plugin_name_args[-1]:
967 plugin_name = arg
968 else:
969 plugin_resp = arg.split(',')
970 # ['result1','result2']
971 for x in plugin_resp:
972 global_plugin_list.append(x)
973 global_plugin_dict[x] = ""
974
975 # Walk the plugin args ['arg1,'arg2']
976 # If the YAML plugin statement 'plugin_args' is not declared.
977 if any('plugin_args' in d for d in plugin_cmd_list):
George Keishingde79a9b2021-08-12 16:14:43 -0500978 idx = self.key_index_list_dict('plugin_args', plugin_cmd_list)
979 plugin_args = plugin_cmd_list[idx]['plugin_args']
George Keishingb97a9042021-07-29 07:41:20 -0500980 if plugin_args:
981 plugin_args = self.yaml_args_populate(plugin_args)
982 else:
983 plugin_args = []
984 else:
985 plugin_args = self.yaml_args_populate([])
986
987 # Pack the args arg1, arg2, .... argn into
988 # "arg1","arg2","argn" string as params for function.
989 parm_args_str = self.yaml_args_string(plugin_args)
990 if parm_args_str:
991 plugin_func = plugin_name + '(' + parm_args_str + ')'
992 else:
993 plugin_func = plugin_name + '()'
994
995 # Execute plugin function.
996 if global_plugin_dict:
997 resp = self.execute_python_eval(plugin_func)
George Keishing9348b402021-08-13 12:22:35 -0500998 # Update plugin vars dict if there is any.
George Keishing73b95d12021-08-13 14:30:52 -0500999 if resp != 'PLUGIN_EVAL_ERROR':
1000 self.response_args_data(resp)
George Keishingb97a9042021-07-29 07:41:20 -05001001 else:
George Keishingcaa97e62021-08-03 14:00:09 -05001002 resp = self.execute_python_eval(plugin_func)
George Keishingb97a9042021-07-29 07:41:20 -05001003 except Exception as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001004 # Set the plugin error state.
1005 plugin_error_dict['exit_on_error'] = True
1006 self.logger.error("\tERROR: execute_plugin_block: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001007 pass
1008
George Keishing73b95d12021-08-13 14:30:52 -05001009 # There is a real error executing the plugin function.
1010 if resp == 'PLUGIN_EVAL_ERROR':
1011 return resp
1012
George Keishingde79a9b2021-08-12 16:14:43 -05001013 # Check if plugin_expects_return (int, string, list,dict etc)
1014 if any('plugin_expects_return' in d for d in plugin_cmd_list):
1015 idx = self.key_index_list_dict('plugin_expects_return', plugin_cmd_list)
1016 plugin_expects = plugin_cmd_list[idx]['plugin_expects_return']
1017 if plugin_expects:
1018 if resp:
1019 if self.plugin_expect_type(plugin_expects, resp) == 'INVALID':
1020 self.logger.error("\tWARN: Plugin error check skipped")
1021 elif not self.plugin_expect_type(plugin_expects, resp):
1022 self.logger.error("\tERROR: Plugin expects return data: %s"
1023 % plugin_expects)
1024 plugin_error_dict['exit_on_error'] = True
1025 elif not resp:
1026 self.logger.error("\tERROR: Plugin func failed to return data")
1027 plugin_error_dict['exit_on_error'] = True
1028
1029 return resp
1030
George Keishingb97a9042021-07-29 07:41:20 -05001031 def response_args_data(self, plugin_resp):
1032 r"""
George Keishing9348b402021-08-13 12:22:35 -05001033 Parse the plugin function response and update plugin return variable.
George Keishingb97a9042021-07-29 07:41:20 -05001034
1035 plugin_resp Response data from plugin function.
1036 """
1037 resp_list = []
George Keishing5765f792021-08-02 13:08:53 -05001038 resp_data = ""
George Keishing9348b402021-08-13 12:22:35 -05001039
George Keishingb97a9042021-07-29 07:41:20 -05001040 # There is nothing to update the plugin response.
1041 if len(global_plugin_list) == 0 or plugin_resp == 'None':
1042 return
1043
George Keishing5765f792021-08-02 13:08:53 -05001044 if isinstance(plugin_resp, str):
1045 resp_data = plugin_resp.strip('\r\n\t')
1046 resp_list.append(resp_data)
1047 elif isinstance(plugin_resp, bytes):
1048 resp_data = str(plugin_resp, 'UTF-8').strip('\r\n\t')
1049 resp_list.append(resp_data)
1050 elif isinstance(plugin_resp, tuple):
1051 if len(global_plugin_list) == 1:
George Keishingb97a9042021-07-29 07:41:20 -05001052 resp_list.append(plugin_resp)
George Keishing5765f792021-08-02 13:08:53 -05001053 else:
1054 resp_list = list(plugin_resp)
1055 resp_list = [x.strip('\r\n\t') for x in resp_list]
George Keishingb97a9042021-07-29 07:41:20 -05001056 elif isinstance(plugin_resp, list):
George Keishing5765f792021-08-02 13:08:53 -05001057 if len(global_plugin_list) == 1:
1058 resp_list.append([x.strip('\r\n\t') for x in plugin_resp])
1059 else:
1060 resp_list = [x.strip('\r\n\t') for x in plugin_resp]
1061 elif isinstance(plugin_resp, int) or isinstance(plugin_resp, float):
1062 resp_list.append(plugin_resp)
George Keishingb97a9042021-07-29 07:41:20 -05001063
George Keishing9348b402021-08-13 12:22:35 -05001064 # Iterate if there is a list of plugin return vars to update.
George Keishingb97a9042021-07-29 07:41:20 -05001065 for idx, item in enumerate(resp_list, start=0):
George Keishing9348b402021-08-13 12:22:35 -05001066 # Exit loop, done required loop.
George Keishingb97a9042021-07-29 07:41:20 -05001067 if idx >= len(global_plugin_list):
1068 break
1069 # Find the index of the return func in the list and
1070 # update the global func return dictionary.
1071 try:
1072 dict_idx = global_plugin_list[idx]
1073 global_plugin_dict[dict_idx] = item
1074 except (IndexError, ValueError) as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001075 self.logger.warn("\tWARN: response_args_data: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001076 pass
1077
1078 # Done updating plugin dict irrespective of pass or failed,
George Keishing9348b402021-08-13 12:22:35 -05001079 # clear all the list element for next plugin block execute.
George Keishingb97a9042021-07-29 07:41:20 -05001080 global_plugin_list.clear()
1081
1082 def yaml_args_string(self, plugin_args):
1083 r"""
1084 Pack the args into string.
1085
1086 plugin_args arg list ['arg1','arg2,'argn']
1087 """
1088 args_str = ''
1089 for args in plugin_args:
1090 if args:
George Keishing0581cb02021-08-05 15:08:58 -05001091 if isinstance(args, (int, float)):
George Keishingb97a9042021-07-29 07:41:20 -05001092 args_str += str(args)
George Keishing0581cb02021-08-05 15:08:58 -05001093 elif args in global_plugin_type_list:
1094 args_str += str(global_plugin_dict[args])
George Keishingb97a9042021-07-29 07:41:20 -05001095 else:
1096 args_str += '"' + str(args.strip('\r\n\t')) + '"'
1097 # Skip last list element.
1098 if args != plugin_args[-1]:
1099 args_str += ","
1100 return args_str
1101
1102 def yaml_args_populate(self, yaml_arg_list):
1103 r"""
George Keishing9348b402021-08-13 12:22:35 -05001104 Decode env and plugin vars and populate.
George Keishingb97a9042021-07-29 07:41:20 -05001105
1106 Description of argument(s):
1107 yaml_arg_list arg list read from YAML
1108
1109 Example:
1110 - plugin_args:
1111 - arg1
1112 - arg2
1113
1114 yaml_arg_list: [arg2, arg2]
1115 """
1116 # Get the env loaded keys as list ['hostname', 'username', 'password'].
1117 env_vars_list = list(self.env_dict)
1118
1119 if isinstance(yaml_arg_list, list):
1120 tmp_list = []
1121 for arg in yaml_arg_list:
George Keishing0581cb02021-08-05 15:08:58 -05001122 if isinstance(arg, (int, float)):
George Keishingb97a9042021-07-29 07:41:20 -05001123 tmp_list.append(arg)
1124 continue
1125 elif isinstance(arg, str):
1126 arg_str = self.yaml_env_and_plugin_vars_populate(str(arg))
1127 tmp_list.append(arg_str)
1128 else:
1129 tmp_list.append(arg)
1130
1131 # return populated list.
1132 return tmp_list
1133
1134 def yaml_env_and_plugin_vars_populate(self, yaml_arg_str):
1135 r"""
George Keishing9348b402021-08-13 12:22:35 -05001136 Update ${MY_VAR} and plugin vars.
George Keishingb97a9042021-07-29 07:41:20 -05001137
1138 Description of argument(s):
George Keishing9348b402021-08-13 12:22:35 -05001139 yaml_arg_str arg string read from YAML.
George Keishingb97a9042021-07-29 07:41:20 -05001140
1141 Example:
1142 - cat ${MY_VAR}
1143 - ls -AX my_plugin_var
1144 """
George Keishing9348b402021-08-13 12:22:35 -05001145 # Parse the string for env vars ${env_vars}.
George Keishingb97a9042021-07-29 07:41:20 -05001146 try:
1147 # Example, list of matching env vars ['username', 'password', 'hostname']
1148 # Extra escape \ for special symbols. '\$\{([^\}]+)\}' works good.
1149 var_name_regex = '\\$\\{([^\\}]+)\\}'
1150 env_var_names_list = re.findall(var_name_regex, yaml_arg_str)
1151 for var in env_var_names_list:
1152 env_var = os.environ[var]
1153 env_replace = '${' + var + '}'
1154 yaml_arg_str = yaml_arg_str.replace(env_replace, env_var)
1155 except Exception as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001156 self.logger.error("\tERROR:yaml_env_vars_populate: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001157 pass
1158
1159 # Parse the string for plugin vars.
1160 try:
1161 # Example, list of plugin vars ['my_username', 'my_data']
1162 plugin_var_name_list = global_plugin_dict.keys()
1163 for var in plugin_var_name_list:
George Keishing9348b402021-08-13 12:22:35 -05001164 # skip env var list already populated above code block list.
George Keishing0581cb02021-08-05 15:08:58 -05001165 if var in env_var_names_list:
1166 continue
George Keishing9348b402021-08-13 12:22:35 -05001167 # If this plugin var exist but empty in dict, don't replace.
George Keishing0581cb02021-08-05 15:08:58 -05001168 # This is either a YAML plugin statement incorrectly used or
George Keishing9348b402021-08-13 12:22:35 -05001169 # user added a plugin var which is not going to be populated.
George Keishing0581cb02021-08-05 15:08:58 -05001170 if yaml_arg_str in global_plugin_dict:
1171 if isinstance(global_plugin_dict[var], (list, dict)):
1172 # List data type or dict can't be replaced, use directly
1173 # in eval function call.
1174 global_plugin_type_list.append(var)
1175 else:
1176 yaml_arg_str = yaml_arg_str.replace(str(var), str(global_plugin_dict[var]))
1177 # Just a string like filename or command.
1178 else:
George Keishingb97a9042021-07-29 07:41:20 -05001179 yaml_arg_str = yaml_arg_str.replace(str(var), str(global_plugin_dict[var]))
1180 except (IndexError, ValueError) as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001181 self.logger.error("\tERROR: yaml_plugin_vars_populate: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001182 pass
1183
1184 return yaml_arg_str
George Keishing1e7b0182021-08-06 14:05:54 -05001185
1186 def plugin_error_check(self, plugin_dict):
1187 r"""
1188 Plugin error dict processing.
1189
1190 Description of argument(s):
1191 plugin_dict Dictionary of plugin error.
1192 """
1193 if any('plugin_error' in d for d in plugin_dict):
1194 for d in plugin_dict:
1195 if 'plugin_error' in d:
1196 value = d['plugin_error']
1197 # Reference if the error is set or not by plugin.
1198 return plugin_error_dict[value]
George Keishingde79a9b2021-08-12 16:14:43 -05001199
1200 def key_index_list_dict(self, key, list_dict):
1201 r"""
1202 Iterate list of dictionary and return index if the key match is found.
1203
1204 Description of argument(s):
1205 key Valid Key in a dict.
1206 list_dict list of dictionary.
1207 """
1208 for i, d in enumerate(list_dict):
1209 if key in d.keys():
1210 return i
1211
1212 def plugin_expect_type(self, type, data):
1213 r"""
1214 Plugin expect directive type check.
1215 """
1216 if type == 'int':
1217 return isinstance(data, int)
1218 elif type == 'float':
1219 return isinstance(data, float)
1220 elif type == 'str':
1221 return isinstance(data, str)
1222 elif type == 'list':
1223 return isinstance(data, list)
1224 elif type == 'dict':
1225 return isinstance(data, dict)
1226 elif type == 'tuple':
1227 return isinstance(data, tuple)
1228 else:
1229 self.logger.info("\tInvalid data type requested: %s" % type)
1230 return 'INVALID'