blob: 8f6e3241913e8f9e7a365b9f6b5b5d422760c93e [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
George Keishingb97a9042021-07-29 07:41:20 -050020r"""
21User define plugins python functions.
22
23It will imports files from directory plugins
24
25plugins
26├── file1.py
27└── file2.py
28
29Example how to define in YAML:
30 - plugin:
31 - plugin_name: plugin.foo_func.foo_func_yaml
32 - plugin_args:
33 - arg1
34 - arg2
35"""
36plugin_dir = 'plugins'
37try:
38 for module in os.listdir(plugin_dir):
39 if module == '__init__.py' or module[-3:] != '.py':
40 continue
41 plugin_module = "plugins." + module[:-3]
42 # To access the module plugin.<module name>.<function>
43 # Example: plugin.foo_func.foo_func_yaml()
44 try:
45 plugin = __import__(plugin_module, globals(), locals(), [], 0)
46 except Exception as e:
47 print("PLUGIN: Module import failed: %s" % module)
48 pass
49except FileNotFoundError as e:
50 print("PLUGIN: %s" % e)
51 pass
52
53r"""
54This is for plugin functions returning data or responses to the caller
55in YAML plugin setup.
56
57Example:
58
59 - plugin:
60 - plugin_name: version = plugin.ssh_execution.ssh_execute_cmd
61 - plugin_args:
62 - ${hostname}
63 - ${username}
64 - ${password}
65 - "cat /etc/os-release | grep VERSION_ID | awk -F'=' '{print $2}'"
66 - plugin:
67 - plugin_name: plugin.print_vars.print_vars
68 - plugin_args:
69 - version
70
71where first plugin "version" var is used by another plugin in the YAML
72block or plugin
73
74"""
75global global_log_store_path
76global global_plugin_dict
77global global_plugin_list
78global_plugin_dict = {}
79global_plugin_list = []
80global_log_store_path = ''
81
Peter D Phan72ce6b82021-06-03 06:18:26 -050082
83class FFDCCollector:
84
85 r"""
86 Sends commands from configuration file to the targeted system to collect log files.
87 Fetch and store generated files at the specified location.
88
89 """
90
Peter D Phan0c669772021-06-24 13:52:42 -050091 def __init__(self,
92 hostname,
93 username,
94 password,
95 ffdc_config,
96 location,
97 remote_type,
Peter D Phane86d9a52021-07-15 10:42:25 -050098 remote_protocol,
George Keishing4885b2f2021-07-21 15:22:45 -050099 env_vars,
George Keishing8e94f8c2021-07-23 15:06:32 -0500100 econfig,
Peter D Phane86d9a52021-07-15 10:42:25 -0500101 log_level):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500102 r"""
103 Description of argument(s):
104
George Keishing8e94f8c2021-07-23 15:06:32 -0500105 hostname name/ip of the targeted (remote) system
106 username user on the targeted system with access to FFDC files
107 password password for user on targeted system
108 ffdc_config configuration file listing commands and files for FFDC
109 location where to store collected FFDC
110 remote_type os type of the remote host
111 remote_protocol Protocol to use to collect data
112 env_vars User define CLI env vars '{"key : "value"}'
113 econfig User define env vars YAML file
Peter D Phan72ce6b82021-06-03 06:18:26 -0500114
115 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500116
117 self.hostname = hostname
118 self.username = username
119 self.password = password
120 self.ffdc_config = ffdc_config
121 self.location = location + "/" + remote_type.upper()
122 self.ssh_remoteclient = None
123 self.telnet_remoteclient = None
124 self.ffdc_dir_path = ""
125 self.ffdc_prefix = ""
126 self.target_type = remote_type.upper()
127 self.remote_protocol = remote_protocol.upper()
George Keishinge1686752021-07-27 12:55:28 -0500128 self.env_vars = env_vars
129 self.econfig = econfig
Peter D Phane86d9a52021-07-15 10:42:25 -0500130 self.start_time = 0
131 self.elapsed_time = ''
132 self.logger = None
133
134 # Set prefix values for scp files and directory.
135 # Since the time stamp is at second granularity, these values are set here
136 # to be sure that all files for this run will have same timestamps
137 # and they will be saved in the same directory.
138 # self.location == local system for now
139 self.set_ffdc_defaults()
140
141 # Logger for this run. Need to be after set_ffdc_defaults()
142 self.script_logging(getattr(logging, log_level.upper()))
143
144 # Verify top level directory exists for storage
145 self.validate_local_store(self.location)
146
Peter D Phan72ce6b82021-06-03 06:18:26 -0500147 if self.verify_script_env():
Peter D Phane86d9a52021-07-15 10:42:25 -0500148 # Load default or user define YAML configuration file.
149 with open(self.ffdc_config, 'r') as file:
150 self.ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
151
152 if self.target_type not in self.ffdc_actions.keys():
153 self.logger.error(
154 "\n\tERROR: %s is not listed in %s.\n\n" % (self.target_type, self.ffdc_config))
155 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500156 else:
Peter D Phan8462faf2021-06-16 12:24:15 -0500157 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500158
George Keishing4885b2f2021-07-21 15:22:45 -0500159 # Load ENV vars from user.
George Keishingaa1f8482021-07-22 00:54:55 -0500160 self.logger.info("\n\tENV: User define input YAML variables")
161 self.env_dict = {}
George Keishinge1686752021-07-27 12:55:28 -0500162 self. load_env()
George Keishingaa1f8482021-07-22 00:54:55 -0500163
Peter D Phan72ce6b82021-06-03 06:18:26 -0500164 def verify_script_env(self):
165
166 # Import to log version
167 import click
168 import paramiko
169
170 run_env_ok = True
Peter D Phan0c669772021-06-24 13:52:42 -0500171
George Keishing506b0582021-07-27 09:31:22 -0500172 redfishtool_version = self.run_tool_cmd('redfishtool -V').split(' ')[2].strip('\n')
173 ipmitool_version = self.run_tool_cmd('ipmitool -V').split(' ')[2]
Peter D Phan0c669772021-06-24 13:52:42 -0500174
Peter D Phane86d9a52021-07-15 10:42:25 -0500175 self.logger.info("\n\t---- Script host environment ----")
176 self.logger.info("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
177 self.logger.info("\t{:<10} {:<10}".format('Script host os', platform.platform()))
178 self.logger.info("\t{:<10} {:>10}".format('Python', platform.python_version()))
179 self.logger.info("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
180 self.logger.info("\t{:<10} {:>10}".format('click', click.__version__))
181 self.logger.info("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
182 self.logger.info("\t{:<10} {:>9}".format('redfishtool', redfishtool_version))
183 self.logger.info("\t{:<10} {:>12}".format('ipmitool', ipmitool_version))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500184
Peter D Phan8462faf2021-06-16 12:24:15 -0500185 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
Peter D Phane86d9a52021-07-15 10:42:25 -0500186 self.logger.error("\n\tERROR: Python or python packages do not meet minimum version requirement.")
187 self.logger.error("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500188 run_env_ok = False
189
Peter D Phane86d9a52021-07-15 10:42:25 -0500190 self.logger.info("\t---- End script host environment ----")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500191 return run_env_ok
192
Peter D Phane86d9a52021-07-15 10:42:25 -0500193 def script_logging(self,
194 log_level_attr):
195 r"""
196 Create logger
197
198 """
199 self.logger = logging.getLogger()
200 self.logger.setLevel(log_level_attr)
201 log_file_handler = logging.FileHandler(self.ffdc_dir_path + "collector.log")
202
203 stdout_handler = logging.StreamHandler(sys.stdout)
204 self.logger.addHandler(log_file_handler)
205 self.logger.addHandler(stdout_handler)
206
207 # Turn off paramiko INFO logging
208 logging.getLogger("paramiko").setLevel(logging.WARNING)
209
Peter D Phan72ce6b82021-06-03 06:18:26 -0500210 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500211 r"""
212 Check if target system is ping-able.
213
214 """
George Keishing0662e942021-07-13 05:12:20 -0500215 response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500216 if response == 0:
Peter D Phane86d9a52021-07-15 10:42:25 -0500217 self.logger.info("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500218 return True
219 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500220 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500221 "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500222 sys.exit(-1)
223
Peter D Phan72ce6b82021-06-03 06:18:26 -0500224 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500225 r"""
226 Initiate FFDC Collection depending on requested protocol.
227
228 """
229
Peter D Phane86d9a52021-07-15 10:42:25 -0500230 self.logger.info("\n\t---- Start communicating with %s ----" % self.hostname)
Peter D Phan7610bc42021-07-06 06:31:05 -0500231 self.start_time = time.time()
Peter D Phan0c669772021-06-24 13:52:42 -0500232
George Keishingf5a57502021-07-22 16:43:47 -0500233 # Find the list of target and protocol supported.
234 check_protocol_list = []
235 config_dict = self.ffdc_actions
Peter D Phan0c669772021-06-24 13:52:42 -0500236
George Keishingf5a57502021-07-22 16:43:47 -0500237 for target_type in config_dict.keys():
238 if self.target_type != target_type:
239 continue
George Keishingeafba182021-06-29 13:44:58 -0500240
George Keishingf5a57502021-07-22 16:43:47 -0500241 for k, v in config_dict[target_type].items():
242 if config_dict[target_type][k]['PROTOCOL'][0] not in check_protocol_list:
243 check_protocol_list.append(config_dict[target_type][k]['PROTOCOL'][0])
Peter D Phanbff617a2021-07-22 08:41:35 -0500244
George Keishingf5a57502021-07-22 16:43:47 -0500245 self.logger.info("\n\t %s protocol type: %s" % (self.target_type, check_protocol_list))
Peter D Phanbff617a2021-07-22 08:41:35 -0500246
George Keishingf5a57502021-07-22 16:43:47 -0500247 verified_working_protocol = self.verify_protocol(check_protocol_list)
Peter D Phanbff617a2021-07-22 08:41:35 -0500248
George Keishingf5a57502021-07-22 16:43:47 -0500249 if verified_working_protocol:
Peter D Phane86d9a52021-07-15 10:42:25 -0500250 self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500251
George Keishingf5a57502021-07-22 16:43:47 -0500252 # Verify top level directory exists for storage
253 self.validate_local_store(self.location)
254
255 if ((self.remote_protocol not in verified_working_protocol) and (self.remote_protocol != 'ALL')):
256 self.logger.info("\n\tWorking protocol list: %s" % verified_working_protocol)
257 self.logger.error(
258 '\tERROR: Requested protocol %s is not in working protocol list.\n'
259 % self.remote_protocol)
260 sys.exit(-1)
261 else:
262 self.generate_ffdc(verified_working_protocol)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500263
264 def ssh_to_target_system(self):
265 r"""
266 Open a ssh connection to targeted system.
267
268 """
269
Peter D Phan5963d632021-07-12 09:58:55 -0500270 self.ssh_remoteclient = SSHRemoteclient(self.hostname,
271 self.username,
272 self.password)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500273
Peter D Phan5963d632021-07-12 09:58:55 -0500274 if self.ssh_remoteclient.ssh_remoteclient_login():
Peter D Phane86d9a52021-07-15 10:42:25 -0500275 self.logger.info("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500276
Peter D Phan5963d632021-07-12 09:58:55 -0500277 # Check scp connection.
278 # If scp connection fails,
279 # continue with FFDC generation but skip scp files to local host.
280 self.ssh_remoteclient.scp_connection()
281 return True
282 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500283 self.logger.info("\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500284 return False
285
286 def telnet_to_target_system(self):
287 r"""
288 Open a telnet connection to targeted system.
289 """
290 self.telnet_remoteclient = TelnetRemoteclient(self.hostname,
291 self.username,
292 self.password)
293 if self.telnet_remoteclient.tn_remoteclient_login():
Peter D Phane86d9a52021-07-15 10:42:25 -0500294 self.logger.info("\n\t[Check] %s Telnet connection established.\t [OK]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500295 return True
296 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500297 self.logger.info("\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500298 return False
Peter D Phan72ce6b82021-06-03 06:18:26 -0500299
George Keishing772c9772021-06-16 23:23:42 -0500300 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500301 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500302 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500303
Peter D Phan04aca3b2021-06-21 10:37:18 -0500304 Description of argument(s):
305 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500306 """
307
Peter D Phane86d9a52021-07-15 10:42:25 -0500308 self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----")
309 self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500310
George Keishingf5a57502021-07-22 16:43:47 -0500311 config_dict = self.ffdc_actions
312 for target_type in config_dict.keys():
313 if self.target_type != target_type:
George Keishing6ea92b02021-07-01 11:20:50 -0500314 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500315
Peter D Phane86d9a52021-07-15 10:42:25 -0500316 self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path)
George Keishingb97a9042021-07-29 07:41:20 -0500317 global_plugin_dict['global_log_store_path'] = self.ffdc_dir_path
George Keishingf5a57502021-07-22 16:43:47 -0500318 self.logger.info("\tSystem Type: %s" % target_type)
319 for k, v in config_dict[target_type].items():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500320
George Keishingf5a57502021-07-22 16:43:47 -0500321 if self.remote_protocol not in working_protocol_list \
George Keishing6ea92b02021-07-01 11:20:50 -0500322 and self.remote_protocol != 'ALL':
323 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500324
George Keishingf5a57502021-07-22 16:43:47 -0500325 protocol = config_dict[target_type][k]['PROTOCOL'][0]
326
327 if protocol not in working_protocol_list:
328 continue
329
George Keishingb7607612021-07-27 13:31:23 -0500330 if protocol in working_protocol_list:
331 if protocol == 'SSH' or protocol == 'SCP':
George Keishing12fd0652021-07-27 13:57:11 -0500332 self.protocol_ssh(protocol, target_type, k)
George Keishingb7607612021-07-27 13:31:23 -0500333 elif protocol == 'TELNET':
George Keishingf5a57502021-07-22 16:43:47 -0500334 self.protocol_telnet(target_type, k)
George Keishingb7607612021-07-27 13:31:23 -0500335 elif protocol == 'REDFISH' or protocol == 'IPMI' or protocol == 'SHELL':
George Keishing506b0582021-07-27 09:31:22 -0500336 self.protocol_execute(protocol, target_type, k)
George Keishingb7607612021-07-27 13:31:23 -0500337 else:
338 self.logger.error("\n\tERROR: %s is not available for %s." % (protocol, self.hostname))
George Keishingeafba182021-06-29 13:44:58 -0500339
Peter D Phan04aca3b2021-06-21 10:37:18 -0500340 # Close network connection after collecting all files
Peter D Phan7610bc42021-07-06 06:31:05 -0500341 self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time))
Peter D Phanbff617a2021-07-22 08:41:35 -0500342 if self.ssh_remoteclient:
343 self.ssh_remoteclient.ssh_remoteclient_disconnect()
344 if self.telnet_remoteclient:
345 self.telnet_remoteclient.tn_remoteclient_disconnect()
Peter D Phan04aca3b2021-06-21 10:37:18 -0500346
Peter D Phan0c669772021-06-24 13:52:42 -0500347 def protocol_ssh(self,
George Keishing12fd0652021-07-27 13:57:11 -0500348 protocol,
George Keishingf5a57502021-07-22 16:43:47 -0500349 target_type,
George Keishing6ea92b02021-07-01 11:20:50 -0500350 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500351 r"""
352 Perform actions using SSH and SCP protocols.
353
354 Description of argument(s):
George Keishing12fd0652021-07-27 13:57:11 -0500355 protocol Protocol to execute.
George Keishingf5a57502021-07-22 16:43:47 -0500356 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500357 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500358 """
359
George Keishing12fd0652021-07-27 13:57:11 -0500360 if protocol == 'SCP':
George Keishingf5a57502021-07-22 16:43:47 -0500361 self.group_copy(self.ffdc_actions[target_type][sub_type])
George Keishing6ea92b02021-07-01 11:20:50 -0500362 else:
George Keishingf5a57502021-07-22 16:43:47 -0500363 self.collect_and_copy_ffdc(self.ffdc_actions[target_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500364
Peter D Phan5963d632021-07-12 09:58:55 -0500365 def protocol_telnet(self,
George Keishingf5a57502021-07-22 16:43:47 -0500366 target_type,
Peter D Phan5963d632021-07-12 09:58:55 -0500367 sub_type):
368 r"""
369 Perform actions using telnet protocol.
370 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500371 target_type OS Type of remote host.
Peter D Phan5963d632021-07-12 09:58:55 -0500372 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500373 self.logger.info("\n\t[Run] Executing commands on %s using %s" % (self.hostname, 'TELNET'))
Peter D Phan5963d632021-07-12 09:58:55 -0500374 telnet_files_saved = []
375 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500376 list_of_commands = self.ffdc_actions[target_type][sub_type]['COMMANDS']
Peter D Phan5963d632021-07-12 09:58:55 -0500377 for index, each_cmd in enumerate(list_of_commands, start=0):
378 command_txt, command_timeout = self.unpack_command(each_cmd)
379 result = self.telnet_remoteclient.execute_command(command_txt, command_timeout)
380 if result:
381 try:
George Keishingf5a57502021-07-22 16:43:47 -0500382 targ_file = self.ffdc_actions[target_type][sub_type]['FILES'][index]
Peter D Phan5963d632021-07-12 09:58:55 -0500383 except IndexError:
Peter D Phane86d9a52021-07-15 10:42:25 -0500384 targ_file = command_txt
385 self.logger.warning(
386 "\n\t[WARN] Missing filename to store data from telnet %s." % each_cmd)
387 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
Peter D Phan5963d632021-07-12 09:58:55 -0500388 targ_file_with_path = (self.ffdc_dir_path
389 + self.ffdc_prefix
390 + targ_file)
391 # Creates a new file
Peter D Phanb76e1752021-08-03 12:50:05 -0500392 with open(targ_file_with_path, 'w') as fp:
Peter D Phan5963d632021-07-12 09:58:55 -0500393 fp.write(result)
394 fp.close
395 telnet_files_saved.append(targ_file)
396 progress_counter += 1
397 self.print_progress(progress_counter)
Peter D Phane86d9a52021-07-15 10:42:25 -0500398 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan5963d632021-07-12 09:58:55 -0500399 for file in telnet_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500400 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
Peter D Phan5963d632021-07-12 09:58:55 -0500401
George Keishing506b0582021-07-27 09:31:22 -0500402 def protocol_execute(self,
403 protocol,
George Keishingf5a57502021-07-22 16:43:47 -0500404 target_type,
George Keishing6ea92b02021-07-01 11:20:50 -0500405 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500406 r"""
George Keishing506b0582021-07-27 09:31:22 -0500407 Perform actions for a given protocol.
Peter D Phan0c669772021-06-24 13:52:42 -0500408
409 Description of argument(s):
George Keishing506b0582021-07-27 09:31:22 -0500410 protocol Protocol to execute.
George Keishingf5a57502021-07-22 16:43:47 -0500411 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500412 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500413 """
414
George Keishing506b0582021-07-27 09:31:22 -0500415 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, protocol))
416 executed_files_saved = []
George Keishingeafba182021-06-29 13:44:58 -0500417 progress_counter = 0
George Keishingf5a57502021-07-22 16:43:47 -0500418 list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type])
George Keishingeafba182021-06-29 13:44:58 -0500419 for index, each_cmd in enumerate(list_of_cmd, start=0):
George Keishingcaa97e62021-08-03 14:00:09 -0500420 plugin_call = False
George Keishingb97a9042021-07-29 07:41:20 -0500421 if isinstance(each_cmd, dict):
422 if 'plugin' in each_cmd:
George Keishingcaa97e62021-08-03 14:00:09 -0500423 plugin_call = True
George Keishingb97a9042021-07-29 07:41:20 -0500424 # call the plugin
425 self.logger.info("\n\t[PLUGIN-START]")
George Keishingcaa97e62021-08-03 14:00:09 -0500426 result = self.execute_plugin_block(each_cmd['plugin'])
George Keishingb97a9042021-07-29 07:41:20 -0500427 self.logger.info("\t[PLUGIN-END]\n")
George Keishingb97a9042021-07-29 07:41:20 -0500428 else:
George Keishing2b83e042021-08-03 12:56:11 -0500429 each_cmd = self.yaml_env_and_plugin_vars_populate(each_cmd)
George Keishingb97a9042021-07-29 07:41:20 -0500430
George Keishingcaa97e62021-08-03 14:00:09 -0500431 if not plugin_call:
432 result = self.run_tool_cmd(each_cmd)
George Keishingeafba182021-06-29 13:44:58 -0500433 if result:
434 try:
George Keishingcaa97e62021-08-03 14:00:09 -0500435 file_name = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
436 targ_file = self.yaml_env_and_plugin_vars_populate(file_name)
George Keishingb97a9042021-07-29 07:41:20 -0500437 # If file is specified as None.
George Keishingcaa97e62021-08-03 14:00:09 -0500438 if targ_file == "None":
George Keishingb97a9042021-07-29 07:41:20 -0500439 continue
George Keishingeafba182021-06-29 13:44:58 -0500440 except IndexError:
George Keishing6ea92b02021-07-01 11:20:50 -0500441 targ_file = each_cmd.split('/')[-1]
George Keishing506b0582021-07-27 09:31:22 -0500442 self.logger.warning(
443 "\n\t[WARN] Missing filename to store data from %s." % each_cmd)
Peter D Phane86d9a52021-07-15 10:42:25 -0500444 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500445
446 targ_file_with_path = (self.ffdc_dir_path
447 + self.ffdc_prefix
448 + targ_file)
449
450 # Creates a new file
451 with open(targ_file_with_path, 'w') as fp:
452 fp.write(result)
453 fp.close
George Keishing506b0582021-07-27 09:31:22 -0500454 executed_files_saved.append(targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500455
456 progress_counter += 1
457 self.print_progress(progress_counter)
458
Peter D Phane86d9a52021-07-15 10:42:25 -0500459 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
George Keishingeafba182021-06-29 13:44:58 -0500460
George Keishing506b0582021-07-27 09:31:22 -0500461 for file in executed_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500462 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
George Keishingeafba182021-06-29 13:44:58 -0500463
Peter D Phan04aca3b2021-06-21 10:37:18 -0500464 def collect_and_copy_ffdc(self,
George Keishingf5a57502021-07-22 16:43:47 -0500465 ffdc_actions_for_target_type,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500466 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500467 r"""
468 Send commands in ffdc_config file to targeted system.
469
470 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500471 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500472 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500473 """
474
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500475 # Executing commands, if any
George Keishingf5a57502021-07-22 16:43:47 -0500476 self.ssh_execute_ffdc_commands(ffdc_actions_for_target_type,
Peter D Phan3beb02e2021-07-06 13:25:17 -0500477 form_filename)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500478
Peter D Phan3beb02e2021-07-06 13:25:17 -0500479 # Copying files
Peter D Phan5963d632021-07-12 09:58:55 -0500480 if self.ssh_remoteclient.scpclient:
Peter D Phane86d9a52021-07-15 10:42:25 -0500481 self.logger.info("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500482
Peter D Phan04aca3b2021-06-21 10:37:18 -0500483 # Retrieving files from target system
George Keishingf5a57502021-07-22 16:43:47 -0500484 list_of_files = self.get_file_list(ffdc_actions_for_target_type)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500485 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500486 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500487 self.logger.info("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500488
Peter D Phanbabf2962021-07-07 11:24:40 -0500489 def get_command_list(self,
George Keishingf5a57502021-07-22 16:43:47 -0500490 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500491 r"""
492 Fetch list of commands from configuration file
493
494 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500495 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500496 """
497 try:
George Keishingf5a57502021-07-22 16:43:47 -0500498 list_of_commands = ffdc_actions_for_target_type['COMMANDS']
Peter D Phanbabf2962021-07-07 11:24:40 -0500499 except KeyError:
500 list_of_commands = []
501 return list_of_commands
502
503 def get_file_list(self,
George Keishingf5a57502021-07-22 16:43:47 -0500504 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500505 r"""
506 Fetch list of commands from configuration file
507
508 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500509 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500510 """
511 try:
George Keishingf5a57502021-07-22 16:43:47 -0500512 list_of_files = ffdc_actions_for_target_type['FILES']
Peter D Phanbabf2962021-07-07 11:24:40 -0500513 except KeyError:
514 list_of_files = []
515 return list_of_files
516
Peter D Phan5963d632021-07-12 09:58:55 -0500517 def unpack_command(self,
518 command):
519 r"""
520 Unpack command from config file
521
522 Description of argument(s):
523 command Command from config file.
524 """
525 if isinstance(command, dict):
526 command_txt = next(iter(command))
527 command_timeout = next(iter(command.values()))
528 elif isinstance(command, str):
529 command_txt = command
530 # Default command timeout 60 seconds
531 command_timeout = 60
532
533 return command_txt, command_timeout
534
Peter D Phan3beb02e2021-07-06 13:25:17 -0500535 def ssh_execute_ffdc_commands(self,
George Keishingf5a57502021-07-22 16:43:47 -0500536 ffdc_actions_for_target_type,
Peter D Phan3beb02e2021-07-06 13:25:17 -0500537 form_filename=False):
538 r"""
539 Send commands in ffdc_config file to targeted system.
540
541 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500542 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan3beb02e2021-07-06 13:25:17 -0500543 form_filename if true, pre-pend self.target_type to filename
544 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500545 self.logger.info("\n\t[Run] Executing commands on %s using %s"
George Keishingf5a57502021-07-22 16:43:47 -0500546 % (self.hostname, ffdc_actions_for_target_type['PROTOCOL'][0]))
Peter D Phan3beb02e2021-07-06 13:25:17 -0500547
George Keishingf5a57502021-07-22 16:43:47 -0500548 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500549 # If command list is empty, returns
550 if not list_of_commands:
551 return
552
553 progress_counter = 0
554 for command in list_of_commands:
Peter D Phan5963d632021-07-12 09:58:55 -0500555 command_txt, command_timeout = self.unpack_command(command)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500556
557 if form_filename:
558 command_txt = str(command_txt % self.target_type)
559
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500560 cmd_exit_code, err, response = \
561 self.ssh_remoteclient.execute_command(command_txt, command_timeout)
562
563 if cmd_exit_code:
564 self.logger.warning(
565 "\n\t\t[WARN] %s exits with code %s." % (command_txt, str(cmd_exit_code)))
566 self.logger.warning("\t\t[WARN] %s " % err)
Peter D Phanbabf2962021-07-07 11:24:40 -0500567
Peter D Phan3beb02e2021-07-06 13:25:17 -0500568 progress_counter += 1
569 self.print_progress(progress_counter)
570
Peter D Phane86d9a52021-07-15 10:42:25 -0500571 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan3beb02e2021-07-06 13:25:17 -0500572
Peter D Phan56429a62021-06-23 08:38:29 -0500573 def group_copy(self,
George Keishingf5a57502021-07-22 16:43:47 -0500574 ffdc_actions_for_target_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500575 r"""
576 scp group of files (wild card) from remote host.
577
578 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500579 fdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan56429a62021-06-23 08:38:29 -0500580 """
Peter D Phan3beb02e2021-07-06 13:25:17 -0500581
Peter D Phan5963d632021-07-12 09:58:55 -0500582 if self.ssh_remoteclient.scpclient:
George Keishing12fd0652021-07-27 13:57:11 -0500583 self.logger.info("\n\tCopying files from remote system %s via SCP.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500584
George Keishingf5a57502021-07-22 16:43:47 -0500585 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phanbabf2962021-07-07 11:24:40 -0500586 # If command list is empty, returns
587 if not list_of_commands:
588 return
Peter D Phan56429a62021-06-23 08:38:29 -0500589
Peter D Phanbabf2962021-07-07 11:24:40 -0500590 for command in list_of_commands:
591 try:
George Keishingb4540e72021-08-02 13:48:46 -0500592 command = self.yaml_env_and_plugin_vars_populate(command)
George Keishing685480b2021-08-01 08:30:04 -0500593 filename = command.split('ls -AX')[1]
George Keishingb4540e72021-08-02 13:48:46 -0500594 filename = self.yaml_env_and_plugin_vars_populate(filename)
Peter D Phanbabf2962021-07-07 11:24:40 -0500595 except IndexError:
George Keishingb4540e72021-08-02 13:48:46 -0500596 self.logger.error("\t\tInvalid command %s" % command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500597 continue
598
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500599 cmd_exit_code, err, response = \
600 self.ssh_remoteclient.execute_command(command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500601
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500602 # If file does not exist, code take no action.
603 # cmd_exit_code is ignored for this scenario.
Peter D Phan56429a62021-06-23 08:38:29 -0500604 if response:
Peter D Phan5963d632021-07-12 09:58:55 -0500605 scp_result = self.ssh_remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
Peter D Phan56429a62021-06-23 08:38:29 -0500606 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500607 self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
Peter D Phan56429a62021-06-23 08:38:29 -0500608 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500609 self.logger.info("\t\tThere is no " + filename)
Peter D Phan56429a62021-06-23 08:38:29 -0500610
611 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500612 self.logger.info("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500613
Peter D Phan72ce6b82021-06-03 06:18:26 -0500614 def scp_ffdc(self,
615 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500616 targ_file_prefix,
617 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500618 file_list=None,
619 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500620 r"""
621 SCP all files in file_dict to the indicated directory on the local system.
622
623 Description of argument(s):
624 targ_dir_path The path of the directory to receive the files.
625 targ_file_prefix Prefix which will be pre-pended to each
626 target file's name.
627 file_dict A dictionary of files to scp from targeted system to this system
628
629 """
630
Peter D Phan72ce6b82021-06-03 06:18:26 -0500631 progress_counter = 0
632 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500633 if form_filename:
634 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500635 source_file_path = filename
636 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
637
Peter D Phanbabf2962021-07-07 11:24:40 -0500638 # If source file name contains wild card, copy filename as is.
639 if '*' in source_file_path:
Peter D Phan5963d632021-07-12 09:58:55 -0500640 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, self.ffdc_dir_path)
Peter D Phanbabf2962021-07-07 11:24:40 -0500641 else:
Peter D Phan5963d632021-07-12 09:58:55 -0500642 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500643
644 if not quiet:
645 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500646 self.logger.info(
647 "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500648 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500649 self.logger.info(
650 "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500651 else:
652 progress_counter += 1
653 self.print_progress(progress_counter)
654
Peter D Phan72ce6b82021-06-03 06:18:26 -0500655 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500656 r"""
657 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
658 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
659 Individual ffdc file will have timestr_filename.
660
661 Description of class variables:
662 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
663
664 self.ffdc_prefix The prefix to be given to each ffdc file name.
665
666 """
667
668 timestr = time.strftime("%Y%m%d-%H%M%S")
669 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
670 self.ffdc_prefix = timestr + "_"
671 self.validate_local_store(self.ffdc_dir_path)
672
673 def validate_local_store(self, dir_path):
674 r"""
675 Ensure path exists to store FFDC files locally.
676
677 Description of variable:
678 dir_path The dir path where collected ffdc data files will be stored.
679
680 """
681
682 if not os.path.exists(dir_path):
683 try:
George Keishing7b3a5132021-07-13 09:24:02 -0500684 os.makedirs(dir_path, 0o755)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500685 except (IOError, OSError) as e:
686 # PermissionError
687 if e.errno == EPERM or e.errno == EACCES:
Peter D Phane86d9a52021-07-15 10:42:25 -0500688 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500689 '\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500690 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500691 self.logger.error(
George Keishing7bf55092021-07-22 12:33:34 -0500692 '\tERROR: os.makedirs %s failed with %s.\n' % (dir_path, e.strerror))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500693 sys.exit(-1)
694
695 def print_progress(self, progress):
696 r"""
697 Print activity progress +
698
699 Description of variable:
700 progress Progress counter.
701
702 """
703
704 sys.stdout.write("\r\t" + "+" * progress)
705 sys.stdout.flush()
706 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500707
708 def verify_redfish(self):
709 r"""
710 Verify remote host has redfish service active
711
712 """
George Keishing506b0582021-07-27 09:31:22 -0500713 redfish_parm = 'redfishtool -r ' \
Peter D Phan0c669772021-06-24 13:52:42 -0500714 + self.hostname + ' -S Always raw GET /redfish/v1/'
George Keishing506b0582021-07-27 09:31:22 -0500715 return(self.run_tool_cmd(redfish_parm, True))
Peter D Phan0c669772021-06-24 13:52:42 -0500716
George Keishingeafba182021-06-29 13:44:58 -0500717 def verify_ipmi(self):
718 r"""
719 Verify remote host has IPMI LAN service active
720
721 """
George Keishing484f8242021-07-27 01:42:02 -0500722 if self.target_type == 'OPENBMC':
723 ipmi_parm = 'ipmitool -I lanplus -C 17 -U ' + self.username + ' -P ' \
724 + self.password + ' -H ' + self.hostname + ' power status'
725 else:
726 ipmi_parm = 'ipmitool -I lanplus -P ' \
727 + self.password + ' -H ' + self.hostname + ' power status'
728
George Keishing506b0582021-07-27 09:31:22 -0500729 return(self.run_tool_cmd(ipmi_parm, True))
George Keishingeafba182021-06-29 13:44:58 -0500730
George Keishing506b0582021-07-27 09:31:22 -0500731 def run_tool_cmd(self,
George Keishingeafba182021-06-29 13:44:58 -0500732 parms_string,
733 quiet=False):
734 r"""
George Keishing506b0582021-07-27 09:31:22 -0500735 Run CLI standard tool or scripts.
George Keishingeafba182021-06-29 13:44:58 -0500736
737 Description of variable:
George Keishing506b0582021-07-27 09:31:22 -0500738 parms_string tool command options.
739 quiet do not print tool error message if True
George Keishingeafba182021-06-29 13:44:58 -0500740 """
741
George Keishing484f8242021-07-27 01:42:02 -0500742 result = subprocess.run([parms_string],
George Keishingeafba182021-06-29 13:44:58 -0500743 stdout=subprocess.PIPE,
744 stderr=subprocess.PIPE,
745 shell=True,
746 universal_newlines=True)
747
748 if result.stderr and not quiet:
George Keishing484f8242021-07-27 01:42:02 -0500749 self.logger.error('\n\t\tERROR with %s ' % parms_string)
Peter D Phane86d9a52021-07-15 10:42:25 -0500750 self.logger.error('\t\t' + result.stderr)
George Keishingeafba182021-06-29 13:44:58 -0500751
752 return result.stdout
George Keishing04d29102021-07-16 02:05:57 -0500753
George Keishingf5a57502021-07-22 16:43:47 -0500754 def verify_protocol(self, protocol_list):
755 r"""
756 Perform protocol working check.
757
758 Description of argument(s):
759 protocol_list List of protocol.
760 """
761
762 tmp_list = []
763 if self.target_is_pingable():
764 tmp_list.append("SHELL")
765
766 for protocol in protocol_list:
767 if self.remote_protocol != 'ALL':
768 if self.remote_protocol != protocol:
769 continue
770
771 # Only check SSH/SCP once for both protocols
772 if protocol == 'SSH' or protocol == 'SCP' and protocol not in tmp_list:
773 if self.ssh_to_target_system():
George Keishingaa638702021-07-26 11:48:28 -0500774 # Add only what user asked.
775 if self.remote_protocol != 'ALL':
776 tmp_list.append(self.remote_protocol)
777 else:
778 tmp_list.append('SSH')
779 tmp_list.append('SCP')
George Keishingf5a57502021-07-22 16:43:47 -0500780
781 if protocol == 'TELNET':
782 if self.telnet_to_target_system():
783 tmp_list.append(protocol)
784
785 if protocol == 'REDFISH':
786 if self.verify_redfish():
787 tmp_list.append(protocol)
788 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
789 else:
790 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname)
791
792 if protocol == 'IPMI':
793 if self.verify_ipmi():
794 tmp_list.append(protocol)
795 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
796 else:
797 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname)
798
799 return tmp_list
George Keishinge1686752021-07-27 12:55:28 -0500800
801 def load_env(self):
802 r"""
803 Perform protocol working check.
804
805 """
806 # This is for the env vars a user can use in YAML to load it at runtime.
807 # Example YAML:
808 # -COMMANDS:
809 # - my_command ${hostname} ${username} ${password}
810 os.environ['hostname'] = self.hostname
811 os.environ['username'] = self.username
812 os.environ['password'] = self.password
813
814 # Append default Env.
815 self.env_dict['hostname'] = self.hostname
816 self.env_dict['username'] = self.username
817 self.env_dict['password'] = self.password
818
819 try:
820 tmp_env_dict = {}
821 if self.env_vars:
822 tmp_env_dict = json.loads(self.env_vars)
823 # Export ENV vars default.
824 for key, value in tmp_env_dict.items():
825 os.environ[key] = value
826 self.env_dict[key] = str(value)
827
828 if self.econfig:
829 with open(self.econfig, 'r') as file:
830 tmp_env_dict = yaml.load(file, Loader=yaml.FullLoader)
831 # Export ENV vars.
832 for key, value in tmp_env_dict['env_params'].items():
833 os.environ[key] = str(value)
834 self.env_dict[key] = str(value)
835 except json.decoder.JSONDecodeError as e:
836 self.logger.error("\n\tERROR: %s " % e)
837 sys.exit(-1)
838
839 # This to mask the password from displaying on the console.
840 mask_dict = self.env_dict.copy()
841 for k, v in mask_dict.items():
842 if k.lower().find("password") != -1:
843 hidden_text = []
844 hidden_text.append(v)
845 password_regex = '(' +\
846 '|'.join([re.escape(x) for x in hidden_text]) + ')'
847 mask_dict[k] = re.sub(password_regex, "********", v)
848
849 self.logger.info(json.dumps(mask_dict, indent=8, sort_keys=False))
George Keishingb97a9042021-07-29 07:41:20 -0500850
851 def execute_python_eval(self, eval_string):
852 r"""
853 Execute qualified python function using eval.
854
855 Description of argument(s):
856 eval_string Execute the python object.
857
858 Example:
859 eval(plugin.foo_func.foo_func(10))
860 """
861 try:
862 self.logger.info("\tCall func: %s" % eval_string)
863 result = eval(eval_string)
864 self.logger.info("\treturn: %s" % str(result))
865 except (ValueError, SyntaxError, NameError) as e:
866 self.logger.error("execute_python_eval: %s" % e)
867 pass
868
869 return result
870
871 def execute_plugin_block(self, plugin_cmd_list):
872 r"""
873 Pack the plugin command to quailifed python string object.
874
875 Description of argument(s):
876 plugin_list_dict Plugin block read from YAML
877 [{'plugin_name': 'plugin.foo_func.my_func'},
878 {'plugin_args': [10]}]
879
880 Example:
881 - plugin:
882 - plugin_name: plugin.foo_func.my_func
883 - plugin_args:
884 - arg1
885 - arg2
886
887 - plugin:
888 - plugin_name: result = plugin.foo_func.my_func
889 - plugin_args:
890 - arg1
891 - arg2
892
893 - plugin:
894 - plugin_name: result1,result2 = plugin.foo_func.my_func
895 - plugin_args:
896 - arg1
897 - arg2
898 """
899 try:
900 plugin_name = plugin_cmd_list[0]['plugin_name']
901 # Equal separator means plugin function returns result.
902 if ' = ' in plugin_name:
903 # Ex. ['result', 'plugin.foo_func.my_func']
904 plugin_name_args = plugin_name.split(' = ')
905 # plugin func return data.
906 for arg in plugin_name_args:
907 if arg == plugin_name_args[-1]:
908 plugin_name = arg
909 else:
910 plugin_resp = arg.split(',')
911 # ['result1','result2']
912 for x in plugin_resp:
913 global_plugin_list.append(x)
914 global_plugin_dict[x] = ""
915
916 # Walk the plugin args ['arg1,'arg2']
917 # If the YAML plugin statement 'plugin_args' is not declared.
918 if any('plugin_args' in d for d in plugin_cmd_list):
919 plugin_args = plugin_cmd_list[1]['plugin_args']
920 if plugin_args:
921 plugin_args = self.yaml_args_populate(plugin_args)
922 else:
923 plugin_args = []
924 else:
925 plugin_args = self.yaml_args_populate([])
926
927 # Pack the args arg1, arg2, .... argn into
928 # "arg1","arg2","argn" string as params for function.
929 parm_args_str = self.yaml_args_string(plugin_args)
930 if parm_args_str:
931 plugin_func = plugin_name + '(' + parm_args_str + ')'
932 else:
933 plugin_func = plugin_name + '()'
934
935 # Execute plugin function.
936 if global_plugin_dict:
937 resp = self.execute_python_eval(plugin_func)
938 self.response_args_data(resp)
939 else:
George Keishingcaa97e62021-08-03 14:00:09 -0500940 resp = self.execute_python_eval(plugin_func)
941 return resp
George Keishingb97a9042021-07-29 07:41:20 -0500942 except Exception as e:
943 self.logger.error("execute_plugin_block: %s" % e)
944 pass
945
946 def response_args_data(self, plugin_resp):
947 r"""
948 Parse the plugin function response.
949
950 plugin_resp Response data from plugin function.
951 """
952 resp_list = []
George Keishing5765f792021-08-02 13:08:53 -0500953 resp_data = ""
George Keishingb97a9042021-07-29 07:41:20 -0500954 # There is nothing to update the plugin response.
955 if len(global_plugin_list) == 0 or plugin_resp == 'None':
956 return
957
George Keishing5765f792021-08-02 13:08:53 -0500958 if isinstance(plugin_resp, str):
959 resp_data = plugin_resp.strip('\r\n\t')
960 resp_list.append(resp_data)
961 elif isinstance(plugin_resp, bytes):
962 resp_data = str(plugin_resp, 'UTF-8').strip('\r\n\t')
963 resp_list.append(resp_data)
964 elif isinstance(plugin_resp, tuple):
965 if len(global_plugin_list) == 1:
George Keishingb97a9042021-07-29 07:41:20 -0500966 resp_list.append(plugin_resp)
George Keishing5765f792021-08-02 13:08:53 -0500967 else:
968 resp_list = list(plugin_resp)
969 resp_list = [x.strip('\r\n\t') for x in resp_list]
George Keishingb97a9042021-07-29 07:41:20 -0500970 elif isinstance(plugin_resp, list):
George Keishing5765f792021-08-02 13:08:53 -0500971 if len(global_plugin_list) == 1:
972 resp_list.append([x.strip('\r\n\t') for x in plugin_resp])
973 else:
974 resp_list = [x.strip('\r\n\t') for x in plugin_resp]
975 elif isinstance(plugin_resp, int) or isinstance(plugin_resp, float):
976 resp_list.append(plugin_resp)
George Keishingb97a9042021-07-29 07:41:20 -0500977
978 for idx, item in enumerate(resp_list, start=0):
979 # Exit loop
980 if idx >= len(global_plugin_list):
981 break
982 # Find the index of the return func in the list and
983 # update the global func return dictionary.
984 try:
985 dict_idx = global_plugin_list[idx]
986 global_plugin_dict[dict_idx] = item
987 except (IndexError, ValueError) as e:
988 self.logger.warn("\tresponse_args_data: %s" % e)
989 pass
990
991 # Done updating plugin dict irrespective of pass or failed,
992 # clear all the list element.
993 global_plugin_list.clear()
994
995 def yaml_args_string(self, plugin_args):
996 r"""
997 Pack the args into string.
998
999 plugin_args arg list ['arg1','arg2,'argn']
1000 """
1001 args_str = ''
1002 for args in plugin_args:
1003 if args:
1004 if isinstance(args, int):
1005 args_str += str(args)
1006 else:
1007 args_str += '"' + str(args.strip('\r\n\t')) + '"'
1008 # Skip last list element.
1009 if args != plugin_args[-1]:
1010 args_str += ","
1011 return args_str
1012
1013 def yaml_args_populate(self, yaml_arg_list):
1014 r"""
1015 Decode ${MY_VAR} and load env data when read from YAML.
1016
1017 Description of argument(s):
1018 yaml_arg_list arg list read from YAML
1019
1020 Example:
1021 - plugin_args:
1022 - arg1
1023 - arg2
1024
1025 yaml_arg_list: [arg2, arg2]
1026 """
1027 # Get the env loaded keys as list ['hostname', 'username', 'password'].
1028 env_vars_list = list(self.env_dict)
1029
1030 if isinstance(yaml_arg_list, list):
1031 tmp_list = []
1032 for arg in yaml_arg_list:
1033 if isinstance(arg, int):
1034 tmp_list.append(arg)
1035 continue
1036 elif isinstance(arg, str):
1037 arg_str = self.yaml_env_and_plugin_vars_populate(str(arg))
1038 tmp_list.append(arg_str)
1039 else:
1040 tmp_list.append(arg)
1041
1042 # return populated list.
1043 return tmp_list
1044
1045 def yaml_env_and_plugin_vars_populate(self, yaml_arg_str):
1046 r"""
1047 Update ${MY_VAR} and my_plugin_vars
1048
1049 Description of argument(s):
1050 yaml_arg_str arg string read from YAML
1051
1052 Example:
1053 - cat ${MY_VAR}
1054 - ls -AX my_plugin_var
1055 """
1056 # Parse the string for env vars.
1057 try:
1058 # Example, list of matching env vars ['username', 'password', 'hostname']
1059 # Extra escape \ for special symbols. '\$\{([^\}]+)\}' works good.
1060 var_name_regex = '\\$\\{([^\\}]+)\\}'
1061 env_var_names_list = re.findall(var_name_regex, yaml_arg_str)
1062 for var in env_var_names_list:
1063 env_var = os.environ[var]
1064 env_replace = '${' + var + '}'
1065 yaml_arg_str = yaml_arg_str.replace(env_replace, env_var)
1066 except Exception as e:
1067 self.logger.error("yaml_env_vars_populate: %s" % e)
1068 pass
1069
1070 # Parse the string for plugin vars.
1071 try:
1072 # Example, list of plugin vars ['my_username', 'my_data']
1073 plugin_var_name_list = global_plugin_dict.keys()
1074 for var in plugin_var_name_list:
1075 # If this plugin var exist but empty value in dict, don't replace.
1076 # This is either a YAML plugin statement incorrecly used or
1077 # user added a plugin var which is not populated.
1078 if str(global_plugin_dict[var]):
1079 yaml_arg_str = yaml_arg_str.replace(str(var), str(global_plugin_dict[var]))
1080 except (IndexError, ValueError) as e:
1081 self.logger.error("yaml_plugin_vars_populate: %s" % e)
1082 pass
1083
1084 return yaml_arg_str