George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | See class prolog below for details. |
| 5 | """ |
| 6 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 7 | import json |
| 8 | import logging |
| 9 | import os |
| 10 | import platform |
| 11 | import re |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import time |
George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 15 | from errno import EACCES, EPERM |
| 16 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 17 | import yaml |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 18 | |
George Keishing | 168545d | 2025-05-12 19:26:54 +0530 | [diff] [blame^] | 19 | sys.dont_write_bytecode = True |
| 20 | |
| 21 | |
Peter D Phan | cb791d7 | 2022-02-08 12:23:03 -0600 | [diff] [blame] | 22 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 23 | sys.path.append(script_dir) |
| 24 | # Walk path and append to sys.path |
| 25 | for root, dirs, files in os.walk(script_dir): |
| 26 | for dir in dirs: |
| 27 | sys.path.append(os.path.join(root, dir)) |
| 28 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 29 | from ssh_utility import SSHRemoteclient # NOQA |
| 30 | from telnet_utility import TelnetRemoteclient # NOQA |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 31 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 32 | r""" |
| 33 | User define plugins python functions. |
| 34 | |
| 35 | It will imports files from directory plugins |
| 36 | |
| 37 | plugins |
| 38 | ├── file1.py |
| 39 | └── file2.py |
| 40 | |
| 41 | Example how to define in YAML: |
| 42 | - plugin: |
| 43 | - plugin_name: plugin.foo_func.foo_func_yaml |
| 44 | - plugin_args: |
| 45 | - arg1 |
| 46 | - arg2 |
| 47 | """ |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 48 | plugin_dir = os.path.join(os.path.dirname(__file__), "plugins") |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 49 | sys.path.append(plugin_dir) |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 50 | |
| 51 | for module in os.listdir(plugin_dir): |
| 52 | if module == "__init__.py" or not module.endswith(".py"): |
| 53 | continue |
| 54 | |
| 55 | plugin_module = f"plugins.{module[:-3]}" |
| 56 | try: |
| 57 | plugin = __import__(plugin_module, globals(), locals(), [], 0) |
| 58 | except Exception as e: |
| 59 | print(f"PLUGIN: Exception: {e}") |
| 60 | print(f"PLUGIN: Module import failed: {module}") |
| 61 | continue |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 62 | |
| 63 | r""" |
| 64 | This is for plugin functions returning data or responses to the caller |
| 65 | in YAML plugin setup. |
| 66 | |
| 67 | Example: |
| 68 | |
| 69 | - plugin: |
| 70 | - plugin_name: version = plugin.ssh_execution.ssh_execute_cmd |
| 71 | - plugin_args: |
| 72 | - ${hostname} |
| 73 | - ${username} |
| 74 | - ${password} |
| 75 | - "cat /etc/os-release | grep VERSION_ID | awk -F'=' '{print $2}'" |
| 76 | - plugin: |
| 77 | - plugin_name: plugin.print_vars.print_vars |
| 78 | - plugin_args: |
| 79 | - version |
| 80 | |
| 81 | where first plugin "version" var is used by another plugin in the YAML |
| 82 | block or plugin |
| 83 | |
| 84 | """ |
| 85 | global global_log_store_path |
| 86 | global global_plugin_dict |
| 87 | global global_plugin_list |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 88 | |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 89 | # Hold the plugin return values in dict and plugin return vars in list. |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 90 | # Dict is to reference and update vars processing in parser where as |
| 91 | # list is for current vars from the plugin block which needs processing. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 92 | global_plugin_dict = {} |
| 93 | global_plugin_list = [] |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 94 | |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 95 | # Hold the plugin return named declared if function returned values are |
| 96 | # list,dict. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 97 | # Refer this name list to look up the plugin dict for eval() args function |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 98 | # Example ['version'] |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 99 | global_plugin_type_list = [] |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 100 | |
| 101 | # Path where logs are to be stored or written. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 102 | global_log_store_path = "" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 103 | |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 104 | # Plugin error state defaults. |
| 105 | plugin_error_dict = { |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 106 | "exit_on_error": False, |
| 107 | "continue_on_error": False, |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 110 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 111 | class ffdc_collector: |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 112 | r""" |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 113 | Execute commands from configuration file to collect log files. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 114 | Fetch and store generated files at the specified location. |
| 115 | |
| 116 | """ |
| 117 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 118 | def __init__( |
| 119 | self, |
| 120 | hostname, |
| 121 | username, |
| 122 | password, |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 123 | port_ssh, |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 124 | port_https, |
| 125 | port_ipmi, |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 126 | ffdc_config, |
| 127 | location, |
| 128 | remote_type, |
| 129 | remote_protocol, |
| 130 | env_vars, |
| 131 | econfig, |
| 132 | log_level, |
| 133 | ): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 134 | r""" |
| 135 | Description of argument(s): |
| 136 | |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 137 | hostname Name/ip of the targeted (remote) system |
| 138 | username User on the targeted system with access to |
| 139 | FFDC files |
| 140 | password Password for user on targeted system |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 141 | port_ssh SSH port value. By default 22 |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 142 | port_https HTTPS port value. By default 443 |
| 143 | port_ipmi IPMI port value. By default 623 |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 144 | ffdc_config Configuration file listing commands and files |
| 145 | for FFDC |
| 146 | location Where to store collected FFDC |
| 147 | remote_type OS type of the remote host |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 148 | remote_protocol Protocol to use to collect data |
| 149 | env_vars User define CLI env vars '{"key : "value"}' |
| 150 | econfig User define env vars YAML file |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 151 | |
| 152 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 153 | |
| 154 | self.hostname = hostname |
| 155 | self.username = username |
| 156 | self.password = password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 157 | self.port_ssh = str(port_ssh) |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 158 | self.port_https = str(port_https) |
| 159 | self.port_ipmi = str(port_ipmi) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 160 | self.ffdc_config = ffdc_config |
| 161 | self.location = location + "/" + remote_type.upper() |
| 162 | self.ssh_remoteclient = None |
| 163 | self.telnet_remoteclient = None |
| 164 | self.ffdc_dir_path = "" |
| 165 | self.ffdc_prefix = "" |
| 166 | self.target_type = remote_type.upper() |
| 167 | self.remote_protocol = remote_protocol.upper() |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 168 | self.env_vars = env_vars |
| 169 | self.econfig = econfig |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 170 | self.start_time = 0 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 171 | self.elapsed_time = "" |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 172 | self.logger = None |
| 173 | |
| 174 | # Set prefix values for scp files and directory. |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 175 | # Since the time stamp is at second granularity, these values are set |
| 176 | # here to be sure that all files for this run will have same timestamps |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 177 | # and they will be saved in the same directory. |
| 178 | # self.location == local system for now |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 179 | self.set_ffdc_default_store_path() |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 180 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 181 | # Logger for this run. Need to be after set_ffdc_default_store_path() |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 182 | self.script_logging(getattr(logging, log_level.upper())) |
| 183 | |
| 184 | # Verify top level directory exists for storage |
| 185 | self.validate_local_store(self.location) |
| 186 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 187 | if self.verify_script_env(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 188 | # Load default or user define YAML configuration file. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 189 | with open(self.ffdc_config, "r") as file: |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 190 | try: |
Yunyun Lin | f87cc0a | 2022-06-08 16:57:04 -0700 | [diff] [blame] | 191 | self.ffdc_actions = yaml.load(file, Loader=yaml.SafeLoader) |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 192 | except yaml.YAMLError as e: |
| 193 | self.logger.error(e) |
| 194 | sys.exit(-1) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 195 | |
| 196 | if self.target_type not in self.ffdc_actions.keys(): |
| 197 | self.logger.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 198 | "\n\tERROR: %s is not listed in %s.\n\n" |
| 199 | % (self.target_type, self.ffdc_config) |
| 200 | ) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 201 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 202 | else: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 203 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 204 | |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 205 | # Load ENV vars from user. |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 206 | self.logger.info("\n\tENV: User define input YAML variables") |
| 207 | self.env_dict = {} |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 208 | self.load_env() |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 209 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 210 | def verify_script_env(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 211 | # Import to log version |
| 212 | import click |
| 213 | import paramiko |
| 214 | |
| 215 | run_env_ok = True |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 216 | |
George Keishing | d805bc0 | 2025-02-28 12:17:13 +0530 | [diff] [blame] | 217 | try: |
| 218 | redfishtool_version = ( |
| 219 | self.run_tool_cmd("redfishtool -V").split(" ")[2].strip("\n") |
| 220 | ) |
| 221 | except Exception as e: |
| 222 | self.logger.error("\tEXCEPTION redfishtool: %s", e) |
| 223 | redfishtool_version = "Not Installed (optional)" |
| 224 | |
| 225 | try: |
| 226 | ipmitool_version = self.run_tool_cmd("ipmitool -V").split(" ")[2] |
| 227 | except Exception as e: |
| 228 | self.logger.error("\tEXCEPTION ipmitool: %s", e) |
| 229 | ipmitool_version = "Not Installed (optional)" |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 230 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 231 | self.logger.info("\n\t---- Script host environment ----") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 232 | self.logger.info( |
| 233 | "\t{:<10} {:<10}".format("Script hostname", os.uname()[1]) |
| 234 | ) |
| 235 | self.logger.info( |
| 236 | "\t{:<10} {:<10}".format("Script host os", platform.platform()) |
| 237 | ) |
| 238 | self.logger.info( |
| 239 | "\t{:<10} {:>10}".format("Python", platform.python_version()) |
| 240 | ) |
| 241 | self.logger.info("\t{:<10} {:>10}".format("PyYAML", yaml.__version__)) |
| 242 | self.logger.info("\t{:<10} {:>10}".format("click", click.__version__)) |
| 243 | self.logger.info( |
| 244 | "\t{:<10} {:>10}".format("paramiko", paramiko.__version__) |
| 245 | ) |
| 246 | self.logger.info( |
| 247 | "\t{:<10} {:>9}".format("redfishtool", redfishtool_version) |
| 248 | ) |
| 249 | self.logger.info( |
| 250 | "\t{:<10} {:>12}".format("ipmitool", ipmitool_version) |
| 251 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 252 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 253 | if eval(yaml.__version__.replace(".", ",")) < (5, 3, 0): |
| 254 | self.logger.error( |
| 255 | "\n\tERROR: Python or python packages do not meet minimum" |
| 256 | " version requirement." |
| 257 | ) |
| 258 | self.logger.error( |
| 259 | "\tERROR: PyYAML version 5.3.0 or higher is needed.\n" |
| 260 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 261 | run_env_ok = False |
| 262 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 263 | self.logger.info("\t---- End script host environment ----") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 264 | return run_env_ok |
| 265 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 266 | def script_logging(self, log_level_attr): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 267 | r""" |
| 268 | Create logger |
| 269 | |
| 270 | """ |
| 271 | self.logger = logging.getLogger() |
| 272 | self.logger.setLevel(log_level_attr) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 273 | log_file_handler = logging.FileHandler( |
| 274 | self.ffdc_dir_path + "collector.log" |
| 275 | ) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 276 | |
| 277 | stdout_handler = logging.StreamHandler(sys.stdout) |
| 278 | self.logger.addHandler(log_file_handler) |
| 279 | self.logger.addHandler(stdout_handler) |
| 280 | |
| 281 | # Turn off paramiko INFO logging |
| 282 | logging.getLogger("paramiko").setLevel(logging.WARNING) |
| 283 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 284 | def target_is_pingable(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 285 | r""" |
| 286 | Check if target system is ping-able. |
| 287 | |
| 288 | """ |
George Keishing | 0662e94 | 2021-07-13 05:12:20 -0500 | [diff] [blame] | 289 | response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 290 | if response == 0: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 291 | self.logger.info( |
| 292 | "\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname |
| 293 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 294 | return True |
| 295 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 296 | self.logger.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 297 | "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" |
| 298 | % self.hostname |
| 299 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 300 | sys.exit(-1) |
| 301 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 302 | def collect_ffdc(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 303 | r""" |
| 304 | Initiate FFDC Collection depending on requested protocol. |
| 305 | |
| 306 | """ |
| 307 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 308 | self.logger.info( |
| 309 | "\n\t---- Start communicating with %s ----" % self.hostname |
| 310 | ) |
Peter D Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 311 | self.start_time = time.time() |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 312 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 313 | # Find the list of target and protocol supported. |
| 314 | check_protocol_list = [] |
| 315 | config_dict = self.ffdc_actions |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 316 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 317 | for target_type in config_dict.keys(): |
| 318 | if self.target_type != target_type: |
| 319 | continue |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 320 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 321 | for k, v in config_dict[target_type].items(): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 322 | if ( |
| 323 | config_dict[target_type][k]["PROTOCOL"][0] |
| 324 | not in check_protocol_list |
| 325 | ): |
| 326 | check_protocol_list.append( |
| 327 | config_dict[target_type][k]["PROTOCOL"][0] |
| 328 | ) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 329 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 330 | self.logger.info( |
| 331 | "\n\t %s protocol type: %s" |
| 332 | % (self.target_type, check_protocol_list) |
| 333 | ) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 334 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 335 | verified_working_protocol = self.verify_protocol(check_protocol_list) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 336 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 337 | if verified_working_protocol: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 338 | self.logger.info( |
| 339 | "\n\t---- Completed protocol pre-requisite check ----\n" |
| 340 | ) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 341 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 342 | # Verify top level directory exists for storage |
| 343 | self.validate_local_store(self.location) |
| 344 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 345 | if (self.remote_protocol not in verified_working_protocol) and ( |
| 346 | self.remote_protocol != "ALL" |
| 347 | ): |
| 348 | self.logger.info( |
| 349 | "\n\tWorking protocol list: %s" % verified_working_protocol |
| 350 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 351 | self.logger.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 352 | "\tERROR: Requested protocol %s is not in working protocol" |
George Keishing | 7899a45 | 2023-02-15 02:46:54 -0600 | [diff] [blame] | 353 | " list.\n" % self.remote_protocol |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 354 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 355 | sys.exit(-1) |
| 356 | else: |
| 357 | self.generate_ffdc(verified_working_protocol) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 358 | |
| 359 | def ssh_to_target_system(self): |
| 360 | r""" |
| 361 | Open a ssh connection to targeted system. |
| 362 | |
| 363 | """ |
| 364 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 365 | self.ssh_remoteclient = SSHRemoteclient( |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 366 | self.hostname, self.username, self.password, self.port_ssh |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 367 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 368 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 369 | if self.ssh_remoteclient.ssh_remoteclient_login(): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 370 | self.logger.info( |
| 371 | "\n\t[Check] %s SSH connection established.\t [OK]" |
| 372 | % self.hostname |
| 373 | ) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 374 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 375 | # Check scp connection. |
| 376 | # If scp connection fails, |
| 377 | # continue with FFDC generation but skip scp files to local host. |
| 378 | self.ssh_remoteclient.scp_connection() |
| 379 | return True |
| 380 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 381 | self.logger.info( |
| 382 | "\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" |
| 383 | % self.hostname |
| 384 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 385 | return False |
| 386 | |
| 387 | def telnet_to_target_system(self): |
| 388 | r""" |
| 389 | Open a telnet connection to targeted system. |
| 390 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 391 | self.telnet_remoteclient = TelnetRemoteclient( |
| 392 | self.hostname, self.username, self.password |
| 393 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 394 | if self.telnet_remoteclient.tn_remoteclient_login(): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 395 | self.logger.info( |
| 396 | "\n\t[Check] %s Telnet connection established.\t [OK]" |
| 397 | % self.hostname |
| 398 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 399 | return True |
| 400 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 401 | self.logger.info( |
| 402 | "\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" |
| 403 | % self.hostname |
| 404 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 405 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 406 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 407 | def generate_ffdc(self, working_protocol_list): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 408 | r""" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 409 | Determine actions based on remote host type |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 410 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 411 | Description of argument(s): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 412 | working_protocol_list List of confirmed working protocols to |
| 413 | connect to remote host. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 414 | """ |
| 415 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 416 | self.logger.info( |
| 417 | "\n\t---- Executing commands on " + self.hostname + " ----" |
| 418 | ) |
| 419 | self.logger.info( |
| 420 | "\n\tWorking protocol list: %s" % working_protocol_list |
| 421 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 422 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 423 | config_dict = self.ffdc_actions |
| 424 | for target_type in config_dict.keys(): |
| 425 | if self.target_type != target_type: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 426 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 427 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 428 | self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 429 | global_plugin_dict["global_log_store_path"] = self.ffdc_dir_path |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 430 | self.logger.info("\tSystem Type: %s" % target_type) |
| 431 | for k, v in config_dict[target_type].items(): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 432 | if ( |
| 433 | self.remote_protocol not in working_protocol_list |
| 434 | and self.remote_protocol != "ALL" |
| 435 | ): |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 436 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 437 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 438 | protocol = config_dict[target_type][k]["PROTOCOL"][0] |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 439 | |
| 440 | if protocol not in working_protocol_list: |
| 441 | continue |
| 442 | |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 443 | if protocol in working_protocol_list: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 444 | if protocol == "SSH" or protocol == "SCP": |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 445 | self.protocol_ssh(protocol, target_type, k) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 446 | elif protocol == "TELNET": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 447 | self.protocol_telnet(target_type, k) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 448 | elif ( |
| 449 | protocol == "REDFISH" |
| 450 | or protocol == "IPMI" |
| 451 | or protocol == "SHELL" |
| 452 | ): |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 453 | self.protocol_execute(protocol, target_type, k) |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 454 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 455 | self.logger.error( |
| 456 | "\n\tERROR: %s is not available for %s." |
| 457 | % (protocol, self.hostname) |
| 458 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 459 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 460 | # Close network connection after collecting all files |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 461 | self.elapsed_time = time.strftime( |
| 462 | "%H:%M:%S", time.gmtime(time.time() - self.start_time) |
| 463 | ) |
George Keishing | 48972ba | 2025-05-05 17:40:29 +0530 | [diff] [blame] | 464 | self.logger.info("\n\tTotal time taken: %s" % self.elapsed_time) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 465 | if self.ssh_remoteclient: |
| 466 | self.ssh_remoteclient.ssh_remoteclient_disconnect() |
| 467 | if self.telnet_remoteclient: |
| 468 | self.telnet_remoteclient.tn_remoteclient_disconnect() |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 469 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 470 | def protocol_ssh(self, protocol, target_type, sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 471 | r""" |
| 472 | Perform actions using SSH and SCP protocols. |
| 473 | |
| 474 | Description of argument(s): |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 475 | protocol Protocol to execute. |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 476 | target_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 477 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 478 | """ |
| 479 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 480 | if protocol == "SCP": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 481 | self.group_copy(self.ffdc_actions[target_type][sub_type]) |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 482 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 483 | self.collect_and_copy_ffdc( |
| 484 | self.ffdc_actions[target_type][sub_type] |
| 485 | ) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 486 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 487 | def protocol_telnet(self, target_type, sub_type): |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 488 | r""" |
| 489 | Perform actions using telnet protocol. |
| 490 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 491 | target_type OS Type of remote host. |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 492 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 493 | self.logger.info( |
| 494 | "\n\t[Run] Executing commands on %s using %s" |
| 495 | % (self.hostname, "TELNET") |
| 496 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 497 | telnet_files_saved = [] |
| 498 | progress_counter = 0 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 499 | list_of_commands = self.ffdc_actions[target_type][sub_type]["COMMANDS"] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 500 | for index, each_cmd in enumerate(list_of_commands, start=0): |
| 501 | command_txt, command_timeout = self.unpack_command(each_cmd) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 502 | result = self.telnet_remoteclient.execute_command( |
| 503 | command_txt, command_timeout |
| 504 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 505 | if result: |
| 506 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 507 | targ_file = self.ffdc_actions[target_type][sub_type][ |
| 508 | "FILES" |
| 509 | ][index] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 510 | except IndexError: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 511 | targ_file = command_txt |
| 512 | self.logger.warning( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 513 | "\n\t[WARN] Missing filename to store data from" |
| 514 | " telnet %s." % each_cmd |
| 515 | ) |
| 516 | self.logger.warning( |
| 517 | "\t[WARN] Data will be stored in %s." % targ_file |
| 518 | ) |
| 519 | targ_file_with_path = ( |
| 520 | self.ffdc_dir_path + self.ffdc_prefix + targ_file |
| 521 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 522 | # Creates a new file |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 523 | with open(targ_file_with_path, "w") as fp: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 524 | fp.write(result) |
| 525 | fp.close |
| 526 | telnet_files_saved.append(targ_file) |
| 527 | progress_counter += 1 |
| 528 | self.print_progress(progress_counter) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 529 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 530 | for file in telnet_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 531 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 532 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 533 | def protocol_execute(self, protocol, target_type, sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 534 | r""" |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 535 | Perform actions for a given protocol. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 536 | |
| 537 | Description of argument(s): |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 538 | protocol Protocol to execute. |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 539 | target_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 540 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 541 | """ |
| 542 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 543 | self.logger.info( |
| 544 | "\n\t[Run] Executing commands to %s using %s" |
| 545 | % (self.hostname, protocol) |
| 546 | ) |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 547 | executed_files_saved = [] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 548 | progress_counter = 0 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 549 | list_of_cmd = self.get_command_list( |
| 550 | self.ffdc_actions[target_type][sub_type] |
| 551 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 552 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 553 | plugin_call = False |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 554 | if isinstance(each_cmd, dict): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 555 | if "plugin" in each_cmd: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 556 | # If the error is set and plugin explicitly |
| 557 | # requested to skip execution on error.. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 558 | if plugin_error_dict[ |
| 559 | "exit_on_error" |
| 560 | ] and self.plugin_error_check(each_cmd["plugin"]): |
| 561 | self.logger.info( |
| 562 | "\n\t[PLUGIN-ERROR] exit_on_error: %s" |
| 563 | % plugin_error_dict["exit_on_error"] |
| 564 | ) |
| 565 | self.logger.info( |
| 566 | "\t[PLUGIN-SKIP] %s" % each_cmd["plugin"][0] |
| 567 | ) |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 568 | continue |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 569 | plugin_call = True |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 570 | # call the plugin |
| 571 | self.logger.info("\n\t[PLUGIN-START]") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 572 | result = self.execute_plugin_block(each_cmd["plugin"]) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 573 | self.logger.info("\t[PLUGIN-END]\n") |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 574 | else: |
George Keishing | 2b83e04 | 2021-08-03 12:56:11 -0500 | [diff] [blame] | 575 | each_cmd = self.yaml_env_and_plugin_vars_populate(each_cmd) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 576 | |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 577 | if not plugin_call: |
| 578 | result = self.run_tool_cmd(each_cmd) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 579 | if result: |
| 580 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 581 | file_name = self.get_file_list( |
| 582 | self.ffdc_actions[target_type][sub_type] |
| 583 | )[index] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 584 | # If file is specified as None. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 585 | if file_name == "None": |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 586 | continue |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 587 | targ_file = self.yaml_env_and_plugin_vars_populate( |
| 588 | file_name |
| 589 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 590 | except IndexError: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 591 | targ_file = each_cmd.split("/")[-1] |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 592 | self.logger.warning( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 593 | "\n\t[WARN] Missing filename to store data from %s." |
| 594 | % each_cmd |
| 595 | ) |
| 596 | self.logger.warning( |
| 597 | "\t[WARN] Data will be stored in %s." % targ_file |
| 598 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 599 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 600 | targ_file_with_path = ( |
| 601 | self.ffdc_dir_path + self.ffdc_prefix + targ_file |
| 602 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 603 | |
| 604 | # Creates a new file |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 605 | with open(targ_file_with_path, "w") as fp: |
George Keishing | 91308ea | 2021-08-10 14:43:15 -0500 | [diff] [blame] | 606 | if isinstance(result, dict): |
| 607 | fp.write(json.dumps(result)) |
| 608 | else: |
| 609 | fp.write(result) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 610 | fp.close |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 611 | executed_files_saved.append(targ_file) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 612 | |
| 613 | progress_counter += 1 |
| 614 | self.print_progress(progress_counter) |
| 615 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 616 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 617 | |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 618 | for file in executed_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 619 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 620 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 621 | def collect_and_copy_ffdc( |
| 622 | self, ffdc_actions_for_target_type, form_filename=False |
| 623 | ): |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 624 | r""" |
| 625 | Send commands in ffdc_config file to targeted system. |
| 626 | |
| 627 | Description of argument(s): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 628 | ffdc_actions_for_target_type Commands and files for the selected |
| 629 | remote host type. |
| 630 | form_filename If true, pre-pend self.target_type to |
| 631 | filename |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 632 | """ |
| 633 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 634 | # Executing commands, if any |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 635 | self.ssh_execute_ffdc_commands( |
| 636 | ffdc_actions_for_target_type, form_filename |
| 637 | ) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 638 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 639 | # Copying files |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 640 | if self.ssh_remoteclient.scpclient: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 641 | self.logger.info( |
| 642 | "\n\n\tCopying FFDC files from remote system %s.\n" |
| 643 | % self.hostname |
| 644 | ) |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 645 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 646 | # Retrieving files from target system |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 647 | list_of_files = self.get_file_list(ffdc_actions_for_target_type) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 648 | self.scp_ffdc( |
| 649 | self.ffdc_dir_path, |
| 650 | self.ffdc_prefix, |
| 651 | form_filename, |
| 652 | list_of_files, |
| 653 | ) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 654 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 655 | self.logger.info( |
| 656 | "\n\n\tSkip copying FFDC files from remote system %s.\n" |
| 657 | % self.hostname |
| 658 | ) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 659 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 660 | def get_command_list(self, ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 661 | r""" |
| 662 | Fetch list of commands from configuration file |
| 663 | |
| 664 | Description of argument(s): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 665 | ffdc_actions_for_target_type Commands and files for the selected |
| 666 | remote host type. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 667 | """ |
| 668 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 669 | list_of_commands = ffdc_actions_for_target_type["COMMANDS"] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 670 | except KeyError: |
| 671 | list_of_commands = [] |
| 672 | return list_of_commands |
| 673 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 674 | def get_file_list(self, ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 675 | r""" |
| 676 | Fetch list of commands from configuration file |
| 677 | |
| 678 | Description of argument(s): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 679 | ffdc_actions_for_target_type Commands and files for the selected |
| 680 | remote host type. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 681 | """ |
| 682 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 683 | list_of_files = ffdc_actions_for_target_type["FILES"] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 684 | except KeyError: |
| 685 | list_of_files = [] |
| 686 | return list_of_files |
| 687 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 688 | def unpack_command(self, command): |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 689 | r""" |
| 690 | Unpack command from config file |
| 691 | |
| 692 | Description of argument(s): |
| 693 | command Command from config file. |
| 694 | """ |
| 695 | if isinstance(command, dict): |
| 696 | command_txt = next(iter(command)) |
| 697 | command_timeout = next(iter(command.values())) |
| 698 | elif isinstance(command, str): |
| 699 | command_txt = command |
| 700 | # Default command timeout 60 seconds |
| 701 | command_timeout = 60 |
| 702 | |
| 703 | return command_txt, command_timeout |
| 704 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 705 | def ssh_execute_ffdc_commands( |
| 706 | self, ffdc_actions_for_target_type, form_filename=False |
| 707 | ): |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 708 | r""" |
| 709 | Send commands in ffdc_config file to targeted system. |
| 710 | |
| 711 | Description of argument(s): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 712 | ffdc_actions_for_target_type Commands and files for the selected |
| 713 | remote host type. |
| 714 | form_filename If true, pre-pend self.target_type to |
| 715 | filename |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 716 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 717 | self.logger.info( |
| 718 | "\n\t[Run] Executing commands on %s using %s" |
| 719 | % (self.hostname, ffdc_actions_for_target_type["PROTOCOL"][0]) |
| 720 | ) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 721 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 722 | list_of_commands = self.get_command_list(ffdc_actions_for_target_type) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 723 | # If command list is empty, returns |
| 724 | if not list_of_commands: |
| 725 | return |
| 726 | |
| 727 | progress_counter = 0 |
| 728 | for command in list_of_commands: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 729 | command_txt, command_timeout = self.unpack_command(command) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 730 | |
| 731 | if form_filename: |
| 732 | command_txt = str(command_txt % self.target_type) |
| 733 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 734 | ( |
| 735 | cmd_exit_code, |
| 736 | err, |
| 737 | response, |
| 738 | ) = self.ssh_remoteclient.execute_command( |
| 739 | command_txt, command_timeout |
| 740 | ) |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 741 | |
| 742 | if cmd_exit_code: |
| 743 | self.logger.warning( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 744 | "\n\t\t[WARN] %s exits with code %s." |
| 745 | % (command_txt, str(cmd_exit_code)) |
| 746 | ) |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 747 | self.logger.warning("\t\t[WARN] %s " % err) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 748 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 749 | progress_counter += 1 |
| 750 | self.print_progress(progress_counter) |
| 751 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 752 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 753 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 754 | def group_copy(self, ffdc_actions_for_target_type): |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 755 | r""" |
| 756 | scp group of files (wild card) from remote host. |
| 757 | |
| 758 | Description of argument(s): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 759 | fdc_actions_for_target_type Commands and files for the selected |
| 760 | remote host type. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 761 | """ |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 762 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 763 | if self.ssh_remoteclient.scpclient: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 764 | self.logger.info( |
| 765 | "\n\tCopying files from remote system %s via SCP.\n" |
| 766 | % self.hostname |
| 767 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 768 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 769 | list_of_commands = self.get_command_list( |
| 770 | ffdc_actions_for_target_type |
| 771 | ) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 772 | # If command list is empty, returns |
| 773 | if not list_of_commands: |
| 774 | return |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 775 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 776 | for command in list_of_commands: |
| 777 | try: |
George Keishing | b4540e7 | 2021-08-02 13:48:46 -0500 | [diff] [blame] | 778 | command = self.yaml_env_and_plugin_vars_populate(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 779 | except IndexError: |
George Keishing | b4540e7 | 2021-08-02 13:48:46 -0500 | [diff] [blame] | 780 | self.logger.error("\t\tInvalid command %s" % command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 781 | continue |
| 782 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 783 | ( |
| 784 | cmd_exit_code, |
| 785 | err, |
| 786 | response, |
| 787 | ) = self.ssh_remoteclient.execute_command(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 788 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 789 | # If file does not exist, code take no action. |
| 790 | # cmd_exit_code is ignored for this scenario. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 791 | if response: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 792 | scp_result = self.ssh_remoteclient.scp_file_from_remote( |
| 793 | response.split("\n"), self.ffdc_dir_path |
| 794 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 795 | if scp_result: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 796 | self.logger.info( |
| 797 | "\t\tSuccessfully copied from " |
| 798 | + self.hostname |
| 799 | + ":" |
| 800 | + command |
| 801 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 802 | else: |
George Keishing | a56e87b | 2021-08-06 00:24:19 -0500 | [diff] [blame] | 803 | self.logger.info("\t\t%s has no result" % command) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 804 | |
| 805 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 806 | self.logger.info( |
| 807 | "\n\n\tSkip copying files from remote system %s.\n" |
| 808 | % self.hostname |
| 809 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 810 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 811 | def scp_ffdc( |
| 812 | self, |
| 813 | targ_dir_path, |
| 814 | targ_file_prefix, |
| 815 | form_filename, |
| 816 | file_list=None, |
| 817 | quiet=None, |
| 818 | ): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 819 | r""" |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 820 | SCP all files in file_dict to the indicated directory on the local |
| 821 | system. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 822 | |
| 823 | Description of argument(s): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 824 | targ_dir_path The path of the directory to receive |
| 825 | the files. |
George Keishing | e16f158 | 2022-12-15 07:32:21 -0600 | [diff] [blame] | 826 | targ_file_prefix Prefix which will be prepended to each |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 827 | target file's name. |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 828 | file_dict A dictionary of files to scp from |
| 829 | targeted system to this system |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 830 | |
| 831 | """ |
| 832 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 833 | progress_counter = 0 |
| 834 | for filename in file_list: |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 835 | if form_filename: |
| 836 | filename = str(filename % self.target_type) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 837 | source_file_path = filename |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 838 | targ_file_path = ( |
| 839 | targ_dir_path + targ_file_prefix + filename.split("/")[-1] |
| 840 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 841 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 842 | # If source file name contains wild card, copy filename as is. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 843 | if "*" in source_file_path: |
| 844 | scp_result = self.ssh_remoteclient.scp_file_from_remote( |
| 845 | source_file_path, self.ffdc_dir_path |
| 846 | ) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 847 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 848 | scp_result = self.ssh_remoteclient.scp_file_from_remote( |
| 849 | source_file_path, targ_file_path |
| 850 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 851 | |
| 852 | if not quiet: |
| 853 | if scp_result: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 854 | self.logger.info( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 855 | "\t\tSuccessfully copied from " |
| 856 | + self.hostname |
| 857 | + ":" |
| 858 | + source_file_path |
| 859 | + ".\n" |
| 860 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 861 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 862 | self.logger.info( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 863 | "\t\tFail to copy from " |
| 864 | + self.hostname |
| 865 | + ":" |
| 866 | + source_file_path |
| 867 | + ".\n" |
| 868 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 869 | else: |
| 870 | progress_counter += 1 |
| 871 | self.print_progress(progress_counter) |
| 872 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 873 | def set_ffdc_default_store_path(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 874 | r""" |
| 875 | Set a default value for self.ffdc_dir_path and self.ffdc_prefix. |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 876 | Collected ffdc file will be stored in dir |
| 877 | /self.location/hostname_timestr/. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 878 | Individual ffdc file will have timestr_filename. |
| 879 | |
| 880 | Description of class variables: |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 881 | self.ffdc_dir_path The dir path where collected ffdc data files |
| 882 | should be put. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 883 | |
| 884 | self.ffdc_prefix The prefix to be given to each ffdc file name. |
| 885 | |
| 886 | """ |
| 887 | |
| 888 | timestr = time.strftime("%Y%m%d-%H%M%S") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 889 | self.ffdc_dir_path = ( |
| 890 | self.location + "/" + self.hostname + "_" + timestr + "/" |
| 891 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 892 | self.ffdc_prefix = timestr + "_" |
| 893 | self.validate_local_store(self.ffdc_dir_path) |
| 894 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 895 | # Need to verify local store path exists prior to instantiate this class. |
| 896 | # This class method is used to share the same code between CLI input parm |
| 897 | # and Robot Framework "${EXECDIR}/logs" before referencing this class. |
| 898 | @classmethod |
| 899 | def validate_local_store(cls, dir_path): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 900 | r""" |
| 901 | Ensure path exists to store FFDC files locally. |
| 902 | |
| 903 | Description of variable: |
| 904 | dir_path The dir path where collected ffdc data files will be stored. |
| 905 | |
| 906 | """ |
| 907 | |
| 908 | if not os.path.exists(dir_path): |
| 909 | try: |
George Keishing | 7b3a513 | 2021-07-13 09:24:02 -0500 | [diff] [blame] | 910 | os.makedirs(dir_path, 0o755) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 911 | except (IOError, OSError) as e: |
| 912 | # PermissionError |
| 913 | if e.errno == EPERM or e.errno == EACCES: |
George Keishing | 1535205 | 2025-04-24 18:55:47 +0530 | [diff] [blame] | 914 | print( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 915 | "\tERROR: os.makedirs %s failed with" |
| 916 | " PermissionError.\n" % dir_path |
| 917 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 918 | else: |
George Keishing | 1535205 | 2025-04-24 18:55:47 +0530 | [diff] [blame] | 919 | print( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 920 | "\tERROR: os.makedirs %s failed with %s.\n" |
| 921 | % (dir_path, e.strerror) |
| 922 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 923 | sys.exit(-1) |
| 924 | |
| 925 | def print_progress(self, progress): |
| 926 | r""" |
| 927 | Print activity progress + |
| 928 | |
| 929 | Description of variable: |
| 930 | progress Progress counter. |
| 931 | |
| 932 | """ |
| 933 | |
| 934 | sys.stdout.write("\r\t" + "+" * progress) |
| 935 | sys.stdout.flush() |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 936 | time.sleep(0.1) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 937 | |
| 938 | def verify_redfish(self): |
| 939 | r""" |
| 940 | Verify remote host has redfish service active |
| 941 | |
| 942 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 943 | redfish_parm = ( |
| 944 | "redfishtool -r " |
| 945 | + self.hostname |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 946 | + ":" |
| 947 | + self.port_https |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 948 | + " -S Always raw GET /redfish/v1/" |
| 949 | ) |
| 950 | return self.run_tool_cmd(redfish_parm, True) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 951 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 952 | def verify_ipmi(self): |
| 953 | r""" |
| 954 | Verify remote host has IPMI LAN service active |
| 955 | |
| 956 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 957 | if self.target_type == "OPENBMC": |
| 958 | ipmi_parm = ( |
| 959 | "ipmitool -I lanplus -C 17 -U " |
| 960 | + self.username |
| 961 | + " -P " |
| 962 | + self.password |
| 963 | + " -H " |
| 964 | + self.hostname |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 965 | + " -p " |
| 966 | + str(self.port_ipmi) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 967 | + " power status" |
| 968 | ) |
George Keishing | 484f824 | 2021-07-27 01:42:02 -0500 | [diff] [blame] | 969 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 970 | ipmi_parm = ( |
| 971 | "ipmitool -I lanplus -P " |
| 972 | + self.password |
| 973 | + " -H " |
| 974 | + self.hostname |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 975 | + " -p " |
| 976 | + str(self.port_ipmi) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 977 | + " power status" |
| 978 | ) |
George Keishing | 484f824 | 2021-07-27 01:42:02 -0500 | [diff] [blame] | 979 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 980 | return self.run_tool_cmd(ipmi_parm, True) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 981 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 982 | def run_tool_cmd(self, parms_string, quiet=False): |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 983 | r""" |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 984 | Run CLI standard tool or scripts. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 985 | |
| 986 | Description of variable: |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 987 | parms_string tool command options. |
| 988 | quiet do not print tool error message if True |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 989 | """ |
| 990 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 991 | result = subprocess.run( |
| 992 | [parms_string], |
| 993 | stdout=subprocess.PIPE, |
| 994 | stderr=subprocess.PIPE, |
| 995 | shell=True, |
| 996 | universal_newlines=True, |
| 997 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 998 | |
| 999 | if result.stderr and not quiet: |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1000 | if self.password in parms_string: |
| 1001 | parms_string = parms_string.replace(self.password, "********") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1002 | self.logger.error("\n\t\tERROR with %s " % parms_string) |
| 1003 | self.logger.error("\t\t" + result.stderr) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1004 | |
| 1005 | return result.stdout |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 1006 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1007 | def verify_protocol(self, protocol_list): |
| 1008 | r""" |
| 1009 | Perform protocol working check. |
| 1010 | |
| 1011 | Description of argument(s): |
| 1012 | protocol_list List of protocol. |
| 1013 | """ |
| 1014 | |
| 1015 | tmp_list = [] |
| 1016 | if self.target_is_pingable(): |
| 1017 | tmp_list.append("SHELL") |
| 1018 | |
| 1019 | for protocol in protocol_list: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1020 | if self.remote_protocol != "ALL": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1021 | if self.remote_protocol != protocol: |
| 1022 | continue |
| 1023 | |
| 1024 | # Only check SSH/SCP once for both protocols |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1025 | if ( |
| 1026 | protocol == "SSH" |
| 1027 | or protocol == "SCP" |
| 1028 | and protocol not in tmp_list |
| 1029 | ): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1030 | if self.ssh_to_target_system(): |
George Keishing | aa63870 | 2021-07-26 11:48:28 -0500 | [diff] [blame] | 1031 | # Add only what user asked. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1032 | if self.remote_protocol != "ALL": |
George Keishing | aa63870 | 2021-07-26 11:48:28 -0500 | [diff] [blame] | 1033 | tmp_list.append(self.remote_protocol) |
| 1034 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1035 | tmp_list.append("SSH") |
| 1036 | tmp_list.append("SCP") |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1037 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1038 | if protocol == "TELNET": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1039 | if self.telnet_to_target_system(): |
| 1040 | tmp_list.append(protocol) |
| 1041 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1042 | if protocol == "REDFISH": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1043 | if self.verify_redfish(): |
| 1044 | tmp_list.append(protocol) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1045 | self.logger.info( |
| 1046 | "\n\t[Check] %s Redfish Service.\t\t [OK]" |
| 1047 | % self.hostname |
| 1048 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1049 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1050 | self.logger.info( |
| 1051 | "\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" |
| 1052 | % self.hostname |
| 1053 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1054 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1055 | if protocol == "IPMI": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1056 | if self.verify_ipmi(): |
| 1057 | tmp_list.append(protocol) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1058 | self.logger.info( |
| 1059 | "\n\t[Check] %s IPMI LAN Service.\t\t [OK]" |
| 1060 | % self.hostname |
| 1061 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1062 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1063 | self.logger.info( |
| 1064 | "\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" |
| 1065 | % self.hostname |
| 1066 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1067 | |
| 1068 | return tmp_list |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1069 | |
| 1070 | def load_env(self): |
| 1071 | r""" |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1072 | Load the user environment variables from a YAML file. |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1073 | |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1074 | This method reads the environment variables from a YAML file specified |
| 1075 | in the ENV_FILE environment variable. If the file is not found or |
| 1076 | there is an error reading the file, an exception is raised. |
| 1077 | |
| 1078 | The YAML file should have the following format: |
| 1079 | |
| 1080 | .. code-block:: yaml |
| 1081 | |
| 1082 | VAR_NAME: VAR_VALUE |
| 1083 | |
| 1084 | Where VAR_NAME is the name of the environment variable, and |
| 1085 | VAR_VALUE is its value. |
| 1086 | |
| 1087 | After loading the environment variables, they are stored in the |
| 1088 | self.env attribute for later use. |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1089 | """ |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1090 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1091 | os.environ["hostname"] = self.hostname |
| 1092 | os.environ["username"] = self.username |
| 1093 | os.environ["password"] = self.password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 1094 | os.environ["port_ssh"] = self.port_ssh |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 1095 | os.environ["port_https"] = self.port_https |
| 1096 | os.environ["port_ipmi"] = self.port_ipmi |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1097 | |
| 1098 | # Append default Env. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1099 | self.env_dict["hostname"] = self.hostname |
| 1100 | self.env_dict["username"] = self.username |
| 1101 | self.env_dict["password"] = self.password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 1102 | self.env_dict["port_ssh"] = self.port_ssh |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 1103 | self.env_dict["port_https"] = self.port_https |
| 1104 | self.env_dict["port_ipmi"] = self.port_ipmi |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1105 | |
| 1106 | try: |
| 1107 | tmp_env_dict = {} |
| 1108 | if self.env_vars: |
| 1109 | tmp_env_dict = json.loads(self.env_vars) |
| 1110 | # Export ENV vars default. |
| 1111 | for key, value in tmp_env_dict.items(): |
| 1112 | os.environ[key] = value |
| 1113 | self.env_dict[key] = str(value) |
| 1114 | |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1115 | # Load user specified ENV config YAML. |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1116 | if self.econfig: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1117 | with open(self.econfig, "r") as file: |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 1118 | try: |
Yunyun Lin | f87cc0a | 2022-06-08 16:57:04 -0700 | [diff] [blame] | 1119 | tmp_env_dict = yaml.load(file, Loader=yaml.SafeLoader) |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 1120 | except yaml.YAMLError as e: |
| 1121 | self.logger.error(e) |
| 1122 | sys.exit(-1) |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1123 | # Export ENV vars. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1124 | for key, value in tmp_env_dict["env_params"].items(): |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1125 | os.environ[key] = str(value) |
| 1126 | self.env_dict[key] = str(value) |
| 1127 | except json.decoder.JSONDecodeError as e: |
| 1128 | self.logger.error("\n\tERROR: %s " % e) |
| 1129 | sys.exit(-1) |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1130 | except FileNotFoundError as e: |
| 1131 | self.logger.error("\n\tERROR: %s " % e) |
| 1132 | sys.exit(-1) |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1133 | |
| 1134 | # This to mask the password from displaying on the console. |
| 1135 | mask_dict = self.env_dict.copy() |
| 1136 | for k, v in mask_dict.items(): |
| 1137 | if k.lower().find("password") != -1: |
| 1138 | hidden_text = [] |
| 1139 | hidden_text.append(v) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1140 | password_regex = ( |
| 1141 | "(" + "|".join([re.escape(x) for x in hidden_text]) + ")" |
| 1142 | ) |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1143 | mask_dict[k] = re.sub(password_regex, "********", v) |
| 1144 | |
| 1145 | self.logger.info(json.dumps(mask_dict, indent=8, sort_keys=False)) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1146 | |
| 1147 | def execute_python_eval(self, eval_string): |
| 1148 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1149 | Execute qualified python function string using eval. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1150 | |
| 1151 | Description of argument(s): |
| 1152 | eval_string Execute the python object. |
| 1153 | |
| 1154 | Example: |
| 1155 | eval(plugin.foo_func.foo_func(10)) |
| 1156 | """ |
| 1157 | try: |
George Keishing | dda48ce | 2021-08-12 07:02:27 -0500 | [diff] [blame] | 1158 | self.logger.info("\tExecuting plugin func()") |
| 1159 | self.logger.debug("\tCall func: %s" % eval_string) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1160 | result = eval(eval_string) |
| 1161 | self.logger.info("\treturn: %s" % str(result)) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1162 | except ( |
| 1163 | ValueError, |
| 1164 | SyntaxError, |
| 1165 | NameError, |
| 1166 | AttributeError, |
| 1167 | TypeError, |
| 1168 | ) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1169 | self.logger.error("\tERROR: execute_python_eval: %s" % e) |
| 1170 | # Set the plugin error state. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1171 | plugin_error_dict["exit_on_error"] = True |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1172 | self.logger.info("\treturn: PLUGIN_EVAL_ERROR") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1173 | return "PLUGIN_EVAL_ERROR" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1174 | |
| 1175 | return result |
| 1176 | |
| 1177 | def execute_plugin_block(self, plugin_cmd_list): |
| 1178 | r""" |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 1179 | Pack the plugin command to qualifed python string object. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1180 | |
| 1181 | Description of argument(s): |
| 1182 | plugin_list_dict Plugin block read from YAML |
| 1183 | [{'plugin_name': 'plugin.foo_func.my_func'}, |
| 1184 | {'plugin_args': [10]}] |
| 1185 | |
| 1186 | Example: |
| 1187 | - plugin: |
| 1188 | - plugin_name: plugin.foo_func.my_func |
| 1189 | - plugin_args: |
| 1190 | - arg1 |
| 1191 | - arg2 |
| 1192 | |
| 1193 | - plugin: |
| 1194 | - plugin_name: result = plugin.foo_func.my_func |
| 1195 | - plugin_args: |
| 1196 | - arg1 |
| 1197 | - arg2 |
| 1198 | |
| 1199 | - plugin: |
| 1200 | - plugin_name: result1,result2 = plugin.foo_func.my_func |
| 1201 | - plugin_args: |
| 1202 | - arg1 |
| 1203 | - arg2 |
| 1204 | """ |
| 1205 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1206 | idx = self.key_index_list_dict("plugin_name", plugin_cmd_list) |
| 1207 | plugin_name = plugin_cmd_list[idx]["plugin_name"] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1208 | # Equal separator means plugin function returns result. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1209 | if " = " in plugin_name: |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1210 | # Ex. ['result', 'plugin.foo_func.my_func'] |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1211 | plugin_name_args = plugin_name.split(" = ") |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1212 | # plugin func return data. |
| 1213 | for arg in plugin_name_args: |
| 1214 | if arg == plugin_name_args[-1]: |
| 1215 | plugin_name = arg |
| 1216 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1217 | plugin_resp = arg.split(",") |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1218 | # ['result1','result2'] |
| 1219 | for x in plugin_resp: |
| 1220 | global_plugin_list.append(x) |
| 1221 | global_plugin_dict[x] = "" |
| 1222 | |
| 1223 | # Walk the plugin args ['arg1,'arg2'] |
| 1224 | # If the YAML plugin statement 'plugin_args' is not declared. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1225 | if any("plugin_args" in d for d in plugin_cmd_list): |
| 1226 | idx = self.key_index_list_dict("plugin_args", plugin_cmd_list) |
| 1227 | plugin_args = plugin_cmd_list[idx]["plugin_args"] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1228 | if plugin_args: |
| 1229 | plugin_args = self.yaml_args_populate(plugin_args) |
| 1230 | else: |
| 1231 | plugin_args = [] |
| 1232 | else: |
| 1233 | plugin_args = self.yaml_args_populate([]) |
| 1234 | |
| 1235 | # Pack the args arg1, arg2, .... argn into |
| 1236 | # "arg1","arg2","argn" string as params for function. |
| 1237 | parm_args_str = self.yaml_args_string(plugin_args) |
| 1238 | if parm_args_str: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1239 | plugin_func = plugin_name + "(" + parm_args_str + ")" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1240 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1241 | plugin_func = plugin_name + "()" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1242 | |
| 1243 | # Execute plugin function. |
| 1244 | if global_plugin_dict: |
| 1245 | resp = self.execute_python_eval(plugin_func) |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1246 | # Update plugin vars dict if there is any. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1247 | if resp != "PLUGIN_EVAL_ERROR": |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1248 | self.response_args_data(resp) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1249 | else: |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 1250 | resp = self.execute_python_eval(plugin_func) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1251 | except Exception as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1252 | # Set the plugin error state. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1253 | plugin_error_dict["exit_on_error"] = True |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1254 | self.logger.error("\tERROR: execute_plugin_block: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1255 | pass |
| 1256 | |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1257 | # There is a real error executing the plugin function. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1258 | if resp == "PLUGIN_EVAL_ERROR": |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1259 | return resp |
| 1260 | |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1261 | # Check if plugin_expects_return (int, string, list,dict etc) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1262 | if any("plugin_expects_return" in d for d in plugin_cmd_list): |
| 1263 | idx = self.key_index_list_dict( |
| 1264 | "plugin_expects_return", plugin_cmd_list |
| 1265 | ) |
| 1266 | plugin_expects = plugin_cmd_list[idx]["plugin_expects_return"] |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1267 | if plugin_expects: |
| 1268 | if resp: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1269 | if ( |
| 1270 | self.plugin_expect_type(plugin_expects, resp) |
| 1271 | == "INVALID" |
| 1272 | ): |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1273 | self.logger.error("\tWARN: Plugin error check skipped") |
| 1274 | elif not self.plugin_expect_type(plugin_expects, resp): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1275 | self.logger.error( |
| 1276 | "\tERROR: Plugin expects return data: %s" |
| 1277 | % plugin_expects |
| 1278 | ) |
| 1279 | plugin_error_dict["exit_on_error"] = True |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1280 | elif not resp: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1281 | self.logger.error( |
| 1282 | "\tERROR: Plugin func failed to return data" |
| 1283 | ) |
| 1284 | plugin_error_dict["exit_on_error"] = True |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1285 | |
| 1286 | return resp |
| 1287 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1288 | def response_args_data(self, plugin_resp): |
| 1289 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1290 | Parse the plugin function response and update plugin return variable. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1291 | |
| 1292 | plugin_resp Response data from plugin function. |
| 1293 | """ |
| 1294 | resp_list = [] |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1295 | resp_data = "" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1296 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1297 | # There is nothing to update the plugin response. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1298 | if len(global_plugin_list) == 0 or plugin_resp == "None": |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1299 | return |
| 1300 | |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1301 | if isinstance(plugin_resp, str): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1302 | resp_data = plugin_resp.strip("\r\n\t") |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1303 | resp_list.append(resp_data) |
| 1304 | elif isinstance(plugin_resp, bytes): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1305 | resp_data = str(plugin_resp, "UTF-8").strip("\r\n\t") |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1306 | resp_list.append(resp_data) |
| 1307 | elif isinstance(plugin_resp, tuple): |
| 1308 | if len(global_plugin_list) == 1: |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1309 | resp_list.append(plugin_resp) |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1310 | else: |
| 1311 | resp_list = list(plugin_resp) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1312 | resp_list = [x.strip("\r\n\t") for x in resp_list] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1313 | elif isinstance(plugin_resp, list): |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1314 | if len(global_plugin_list) == 1: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1315 | resp_list.append([x.strip("\r\n\t") for x in plugin_resp]) |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1316 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1317 | resp_list = [x.strip("\r\n\t") for x in plugin_resp] |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1318 | elif isinstance(plugin_resp, int) or isinstance(plugin_resp, float): |
| 1319 | resp_list.append(plugin_resp) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1320 | |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1321 | # Iterate if there is a list of plugin return vars to update. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1322 | for idx, item in enumerate(resp_list, start=0): |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1323 | # Exit loop, done required loop. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1324 | if idx >= len(global_plugin_list): |
| 1325 | break |
| 1326 | # Find the index of the return func in the list and |
| 1327 | # update the global func return dictionary. |
| 1328 | try: |
| 1329 | dict_idx = global_plugin_list[idx] |
| 1330 | global_plugin_dict[dict_idx] = item |
| 1331 | except (IndexError, ValueError) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1332 | self.logger.warn("\tWARN: response_args_data: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1333 | pass |
| 1334 | |
| 1335 | # Done updating plugin dict irrespective of pass or failed, |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1336 | # clear all the list element for next plugin block execute. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1337 | global_plugin_list.clear() |
| 1338 | |
| 1339 | def yaml_args_string(self, plugin_args): |
| 1340 | r""" |
| 1341 | Pack the args into string. |
| 1342 | |
| 1343 | plugin_args arg list ['arg1','arg2,'argn'] |
| 1344 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1345 | args_str = "" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1346 | for args in plugin_args: |
| 1347 | if args: |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1348 | if isinstance(args, (int, float)): |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1349 | args_str += str(args) |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1350 | elif args in global_plugin_type_list: |
| 1351 | args_str += str(global_plugin_dict[args]) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1352 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1353 | args_str += '"' + str(args.strip("\r\n\t")) + '"' |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1354 | # Skip last list element. |
| 1355 | if args != plugin_args[-1]: |
| 1356 | args_str += "," |
| 1357 | return args_str |
| 1358 | |
| 1359 | def yaml_args_populate(self, yaml_arg_list): |
| 1360 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1361 | Decode env and plugin vars and populate. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1362 | |
| 1363 | Description of argument(s): |
| 1364 | yaml_arg_list arg list read from YAML |
| 1365 | |
| 1366 | Example: |
| 1367 | - plugin_args: |
| 1368 | - arg1 |
| 1369 | - arg2 |
| 1370 | |
| 1371 | yaml_arg_list: [arg2, arg2] |
| 1372 | """ |
| 1373 | # Get the env loaded keys as list ['hostname', 'username', 'password']. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1374 | |
| 1375 | if isinstance(yaml_arg_list, list): |
| 1376 | tmp_list = [] |
| 1377 | for arg in yaml_arg_list: |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1378 | if isinstance(arg, (int, float)): |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1379 | tmp_list.append(arg) |
| 1380 | continue |
| 1381 | elif isinstance(arg, str): |
| 1382 | arg_str = self.yaml_env_and_plugin_vars_populate(str(arg)) |
| 1383 | tmp_list.append(arg_str) |
| 1384 | else: |
| 1385 | tmp_list.append(arg) |
| 1386 | |
| 1387 | # return populated list. |
| 1388 | return tmp_list |
| 1389 | |
| 1390 | def yaml_env_and_plugin_vars_populate(self, yaml_arg_str): |
| 1391 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1392 | Update ${MY_VAR} and plugin vars. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1393 | |
| 1394 | Description of argument(s): |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1395 | yaml_arg_str arg string read from YAML. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1396 | |
| 1397 | Example: |
| 1398 | - cat ${MY_VAR} |
| 1399 | - ls -AX my_plugin_var |
| 1400 | """ |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1401 | # Parse the string for env vars ${env_vars}. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1402 | try: |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 1403 | # Example, list of matching |
| 1404 | # env vars ['username', 'password', 'hostname'] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1405 | # Extra escape \ for special symbols. '\$\{([^\}]+)\}' works good. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1406 | var_name_regex = "\\$\\{([^\\}]+)\\}" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1407 | env_var_names_list = re.findall(var_name_regex, yaml_arg_str) |
| 1408 | for var in env_var_names_list: |
| 1409 | env_var = os.environ[var] |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1410 | env_replace = "${" + var + "}" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1411 | yaml_arg_str = yaml_arg_str.replace(env_replace, env_var) |
| 1412 | except Exception as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1413 | self.logger.error("\tERROR:yaml_env_vars_populate: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1414 | pass |
| 1415 | |
| 1416 | # Parse the string for plugin vars. |
| 1417 | try: |
| 1418 | # Example, list of plugin vars ['my_username', 'my_data'] |
| 1419 | plugin_var_name_list = global_plugin_dict.keys() |
| 1420 | for var in plugin_var_name_list: |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1421 | # skip env var list already populated above code block list. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1422 | if var in env_var_names_list: |
| 1423 | continue |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1424 | # If this plugin var exist but empty in dict, don't replace. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1425 | # This is either a YAML plugin statement incorrectly used or |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1426 | # user added a plugin var which is not going to be populated. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1427 | if yaml_arg_str in global_plugin_dict: |
| 1428 | if isinstance(global_plugin_dict[var], (list, dict)): |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 1429 | # List data type or dict can't be replaced, use |
| 1430 | # directly in eval function call. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1431 | global_plugin_type_list.append(var) |
| 1432 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1433 | yaml_arg_str = yaml_arg_str.replace( |
| 1434 | str(var), str(global_plugin_dict[var]) |
| 1435 | ) |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1436 | # Just a string like filename or command. |
| 1437 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1438 | yaml_arg_str = yaml_arg_str.replace( |
| 1439 | str(var), str(global_plugin_dict[var]) |
| 1440 | ) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1441 | except (IndexError, ValueError) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1442 | self.logger.error("\tERROR: yaml_plugin_vars_populate: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1443 | pass |
| 1444 | |
| 1445 | return yaml_arg_str |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1446 | |
| 1447 | def plugin_error_check(self, plugin_dict): |
| 1448 | r""" |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1449 | Process plugin error dictionary and return the corresponding error |
| 1450 | message. |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1451 | |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1452 | This method checks if any dictionary in the plugin_dict list contains |
| 1453 | a "plugin_error" key. If such a dictionary is found, it retrieves the |
| 1454 | value associated with the "plugin_error" key and returns the |
| 1455 | corresponding error message from the plugin_error_dict attribute. |
| 1456 | |
| 1457 | Parameters: |
| 1458 | plugin_dict (list of dict): A list of dictionaries containing |
| 1459 | plugin error information. |
| 1460 | |
| 1461 | Returns: |
| 1462 | str: The error message corresponding to the "plugin_error" value, |
| 1463 | or None if no error is found. |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1464 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1465 | if any("plugin_error" in d for d in plugin_dict): |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1466 | for d in plugin_dict: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1467 | if "plugin_error" in d: |
| 1468 | value = d["plugin_error"] |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1469 | return self.plugin_error_dict.get(value, None) |
| 1470 | return None |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1471 | |
| 1472 | def key_index_list_dict(self, key, list_dict): |
| 1473 | r""" |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1474 | Find the index of the first dictionary in the list that contains |
| 1475 | the specified key. |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1476 | |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1477 | Parameters: |
| 1478 | key (str): The key to search for in the |
| 1479 | dictionaries. |
| 1480 | list_dict (list of dict): A list of dictionaries to search |
| 1481 | through. |
| 1482 | |
| 1483 | Returns: |
| 1484 | int: The index of the first dictionary containing the key, or -1 |
| 1485 | if no match is found. |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1486 | """ |
| 1487 | for i, d in enumerate(list_dict): |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1488 | if key in d: |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1489 | return i |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1490 | return -1 |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1491 | |
| 1492 | def plugin_expect_type(self, type, data): |
| 1493 | r""" |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1494 | Check if the provided data matches the expected type. |
| 1495 | |
| 1496 | This method checks if the data argument matches the specified type. |
| 1497 | It supports the following types: "int", "float", "str", "list", "dict", |
| 1498 | and "tuple". |
| 1499 | |
| 1500 | If the type is not recognized, it logs an info message and returns |
| 1501 | "INVALID". |
| 1502 | |
| 1503 | Parameters: |
| 1504 | type (str): The expected data type. |
| 1505 | data: The data to check against the expected type. |
| 1506 | |
| 1507 | Returns: |
| 1508 | bool or str: True if the data matches the expected type, False if |
| 1509 | not, or "INVALID" if the type is not recognized. |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1510 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1511 | if type == "int": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1512 | return isinstance(data, int) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1513 | elif type == "float": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1514 | return isinstance(data, float) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1515 | elif type == "str": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1516 | return isinstance(data, str) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1517 | elif type == "list": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1518 | return isinstance(data, list) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1519 | elif type == "dict": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1520 | return isinstance(data, dict) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1521 | elif type == "tuple": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1522 | return isinstance(data, tuple) |
| 1523 | else: |
| 1524 | self.logger.info("\tInvalid data type requested: %s" % type) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1525 | return "INVALID" |