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""" |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 135 | Initialize the FFDCCollector object with the provided parameters. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 136 | |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 137 | This method initializes an FFDCCollector object with the given |
| 138 | attributes. The attributes represent the configuration for connecting |
| 139 | to a remote system, collecting log data, and storing the collected |
| 140 | data. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 141 | |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 142 | Parameters: |
| 143 | hostname (str): Name or IP address of the targeted |
| 144 | (remote) system. |
| 145 | username (str): User on the targeted system with access |
| 146 | to log files. |
| 147 | password (str): Password for the user on the targeted |
| 148 | system. |
| 149 | port_ssh (int, optional): SSH port value. Defaults to 22. |
| 150 | port_https (int, optional): HTTPS port value. Defaults to 443. |
| 151 | port_ipmi (int, optional): IPMI port value. Defaults to 623. |
| 152 | ffdc_config (str): Configuration file listing commands |
| 153 | and files for FFDC. |
| 154 | location (str): Where to store collected log data. |
| 155 | remote_type (str): Block YAML type name of the remote |
| 156 | host. |
| 157 | remote_protocol (str): Protocol to use to collect data. |
| 158 | env_vars (dict, optional): User-defined CLI environment variables. |
| 159 | Defaults to None. |
| 160 | econfig (str, optional): User-defined environment variables |
| 161 | YAML file. Defaults to None. |
| 162 | log_level (str, optional): Log level for the collector. |
| 163 | Defaults to "INFO". |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 164 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 165 | |
| 166 | self.hostname = hostname |
| 167 | self.username = username |
| 168 | self.password = password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 169 | self.port_ssh = str(port_ssh) |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 170 | self.port_https = str(port_https) |
| 171 | self.port_ipmi = str(port_ipmi) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 172 | self.ffdc_config = ffdc_config |
| 173 | self.location = location + "/" + remote_type.upper() |
| 174 | self.ssh_remoteclient = None |
| 175 | self.telnet_remoteclient = None |
| 176 | self.ffdc_dir_path = "" |
| 177 | self.ffdc_prefix = "" |
| 178 | self.target_type = remote_type.upper() |
| 179 | self.remote_protocol = remote_protocol.upper() |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 180 | self.env_vars = env_vars if env_vars else {} |
| 181 | self.econfig = econfig if econfig else {} |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 182 | self.start_time = 0 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 183 | self.elapsed_time = "" |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 184 | self.env_dict = {} |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 185 | self.logger = None |
| 186 | |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 187 | """ |
| 188 | Set prefix values for SCP files and directories. |
| 189 | Since the time stamp is at second granularity, these values are set |
| 190 | here to be sure that all files for this run will have the same |
| 191 | timestamps and be saved in the same directory. |
| 192 | self.location == local system for now |
| 193 | """ |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 194 | self.set_ffdc_default_store_path() |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 195 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 196 | # 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] | 197 | self.script_logging(getattr(logging, log_level.upper())) |
| 198 | |
| 199 | # Verify top level directory exists for storage |
| 200 | self.validate_local_store(self.location) |
| 201 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 202 | if self.verify_script_env(): |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 203 | try: |
| 204 | with open(self.ffdc_config, "r") as file: |
| 205 | self.ffdc_actions = yaml.safe_load(file) |
| 206 | except yaml.YAMLError as e: |
| 207 | self.logger.error(e) |
| 208 | sys.exit(-1) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 209 | |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 210 | if self.target_type not in self.ffdc_actions: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 211 | self.logger.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 212 | "\n\tERROR: %s is not listed in %s.\n\n" |
| 213 | % (self.target_type, self.ffdc_config) |
| 214 | ) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 215 | sys.exit(-1) |
George Keishing | 04eb44e | 2025-05-16 22:14:14 +0530 | [diff] [blame] | 216 | |
| 217 | self.logger.info("\n\tENV: User define input YAML variables") |
| 218 | self.env_dict = self.load_env() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 219 | else: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 220 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 221 | |
| 222 | def verify_script_env(self): |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 223 | r""" |
| 224 | Verify that all required environment variables are set. |
| 225 | |
| 226 | This method checks if all required environment variables are set. |
| 227 | If any required variable is missing, the method returns False. |
| 228 | Otherwise, it returns True. |
| 229 | |
| 230 | Returns: |
| 231 | bool: True if all required environment variables are set, |
| 232 | False otherwise. |
| 233 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 234 | # Import to log version |
| 235 | import click |
| 236 | import paramiko |
| 237 | |
| 238 | run_env_ok = True |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 239 | |
George Keishing | d805bc0 | 2025-02-28 12:17:13 +0530 | [diff] [blame] | 240 | try: |
| 241 | redfishtool_version = ( |
| 242 | self.run_tool_cmd("redfishtool -V").split(" ")[2].strip("\n") |
| 243 | ) |
| 244 | except Exception as e: |
| 245 | self.logger.error("\tEXCEPTION redfishtool: %s", e) |
| 246 | redfishtool_version = "Not Installed (optional)" |
| 247 | |
| 248 | try: |
| 249 | ipmitool_version = self.run_tool_cmd("ipmitool -V").split(" ")[2] |
| 250 | except Exception as e: |
| 251 | self.logger.error("\tEXCEPTION ipmitool: %s", e) |
| 252 | ipmitool_version = "Not Installed (optional)" |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 253 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 254 | self.logger.info("\n\t---- Script host environment ----") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 255 | self.logger.info( |
| 256 | "\t{:<10} {:<10}".format("Script hostname", os.uname()[1]) |
| 257 | ) |
| 258 | self.logger.info( |
| 259 | "\t{:<10} {:<10}".format("Script host os", platform.platform()) |
| 260 | ) |
| 261 | self.logger.info( |
| 262 | "\t{:<10} {:>10}".format("Python", platform.python_version()) |
| 263 | ) |
| 264 | self.logger.info("\t{:<10} {:>10}".format("PyYAML", yaml.__version__)) |
| 265 | self.logger.info("\t{:<10} {:>10}".format("click", click.__version__)) |
| 266 | self.logger.info( |
| 267 | "\t{:<10} {:>10}".format("paramiko", paramiko.__version__) |
| 268 | ) |
| 269 | self.logger.info( |
| 270 | "\t{:<10} {:>9}".format("redfishtool", redfishtool_version) |
| 271 | ) |
| 272 | self.logger.info( |
| 273 | "\t{:<10} {:>12}".format("ipmitool", ipmitool_version) |
| 274 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 275 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 276 | if eval(yaml.__version__.replace(".", ",")) < (5, 3, 0): |
| 277 | self.logger.error( |
| 278 | "\n\tERROR: Python or python packages do not meet minimum" |
| 279 | " version requirement." |
| 280 | ) |
| 281 | self.logger.error( |
| 282 | "\tERROR: PyYAML version 5.3.0 or higher is needed.\n" |
| 283 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 284 | run_env_ok = False |
| 285 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 286 | self.logger.info("\t---- End script host environment ----") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 287 | return run_env_ok |
| 288 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 289 | def script_logging(self, log_level_attr): |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 290 | """ |
| 291 | Create a logger for the script with the specified log level. |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 292 | |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 293 | This method creates a logger for the script with the specified |
| 294 | log level. The logger is configured to write log messages to a file |
| 295 | and the console. |
| 296 | |
| 297 | self.logger = logging.getLogger(__name__) |
| 298 | |
| 299 | Setting logger with __name__ will add the trace |
| 300 | Example: |
| 301 | |
| 302 | INFO:ffdc_collector: System Type: OPENBMC |
| 303 | |
| 304 | Currently, set to empty purposely to log as |
| 305 | System Type: OPENBMC |
| 306 | |
| 307 | Parameters: |
| 308 | log_level_attr (str): The log level for the logger |
| 309 | (e.g., "DEBUG", "INFO", "WARNING", |
| 310 | "ERROR", "CRITICAL"). |
| 311 | |
| 312 | Returns: |
| 313 | None |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 314 | """ |
| 315 | self.logger = logging.getLogger() |
| 316 | self.logger.setLevel(log_level_attr) |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 317 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 318 | log_file_handler = logging.FileHandler( |
| 319 | self.ffdc_dir_path + "collector.log" |
| 320 | ) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 321 | stdout_handler = logging.StreamHandler(sys.stdout) |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 322 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 323 | self.logger.addHandler(log_file_handler) |
| 324 | self.logger.addHandler(stdout_handler) |
| 325 | |
| 326 | # Turn off paramiko INFO logging |
| 327 | logging.getLogger("paramiko").setLevel(logging.WARNING) |
| 328 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 329 | def target_is_pingable(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 330 | r""" |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 331 | Check if the target system is ping-able. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 332 | |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 333 | This method checks if the target system is reachable by sending an |
| 334 | ICMP echo request (ping). If the target system responds to the ping, |
| 335 | the method returns True. Otherwise, it returns False. |
| 336 | |
| 337 | Returns: |
| 338 | bool: True if the target system is ping-able, False otherwise. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 339 | """ |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 340 | response = os.system("ping -c 2 %s 2>&1 >/dev/null" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 341 | if response == 0: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 342 | self.logger.info( |
| 343 | "\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname |
| 344 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 345 | return True |
| 346 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 347 | self.logger.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 348 | "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" |
| 349 | % self.hostname |
| 350 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 351 | sys.exit(-1) |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 352 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 353 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 354 | def collect_ffdc(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 355 | r""" |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 356 | Initiate FFDC collection based on the requested protocol. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 357 | |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 358 | This method initiates FFDC (First Failure Data Capture) collection |
| 359 | based on the requested protocol (SSH,SCP, TELNET, REDFISH, IPMI). |
| 360 | The method establishes a connection to the target system using the |
| 361 | specified protocol and collects the required FFDC data. |
| 362 | |
| 363 | Returns: |
| 364 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 365 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 366 | self.logger.info( |
| 367 | "\n\t---- Start communicating with %s ----" % self.hostname |
| 368 | ) |
Peter D Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 369 | self.start_time = time.time() |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 370 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 371 | # Find the list of target and protocol supported. |
| 372 | check_protocol_list = [] |
| 373 | config_dict = self.ffdc_actions |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 374 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 375 | for target_type in config_dict.keys(): |
| 376 | if self.target_type != target_type: |
| 377 | continue |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 378 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 379 | for k, v in config_dict[target_type].items(): |
George Keishing | 967c1ed | 2025-05-17 16:32:41 +0530 | [diff] [blame] | 380 | if v["PROTOCOL"][0] not in check_protocol_list: |
| 381 | check_protocol_list.append(v["PROTOCOL"][0]) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 382 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 383 | self.logger.info( |
| 384 | "\n\t %s protocol type: %s" |
| 385 | % (self.target_type, check_protocol_list) |
| 386 | ) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 387 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 388 | verified_working_protocol = self.verify_protocol(check_protocol_list) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 389 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 390 | if verified_working_protocol: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 391 | self.logger.info( |
| 392 | "\n\t---- Completed protocol pre-requisite check ----\n" |
| 393 | ) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 394 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 395 | # Verify top level directory exists for storage |
| 396 | self.validate_local_store(self.location) |
| 397 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 398 | if (self.remote_protocol not in verified_working_protocol) and ( |
| 399 | self.remote_protocol != "ALL" |
| 400 | ): |
| 401 | self.logger.info( |
| 402 | "\n\tWorking protocol list: %s" % verified_working_protocol |
| 403 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 404 | self.logger.error( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 405 | "\tERROR: Requested protocol %s is not in working protocol" |
George Keishing | 7899a45 | 2023-02-15 02:46:54 -0600 | [diff] [blame] | 406 | " list.\n" % self.remote_protocol |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 407 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 408 | sys.exit(-1) |
| 409 | else: |
| 410 | self.generate_ffdc(verified_working_protocol) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 411 | |
| 412 | def ssh_to_target_system(self): |
| 413 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 414 | Establish an SSH connection to the target system. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 415 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 416 | This method establishes an SSH connection to the target system using |
| 417 | the provided hostname, username, password, and SSH port. If the |
| 418 | connection is successful, the method returns True. Otherwise, it logs |
| 419 | an error message and returns False. |
| 420 | |
| 421 | Returns: |
| 422 | bool: True if the connection is successful, False otherwise. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 423 | """ |
| 424 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 425 | self.ssh_remoteclient = SSHRemoteclient( |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 426 | self.hostname, self.username, self.password, self.port_ssh |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 427 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 428 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 429 | if self.ssh_remoteclient.ssh_remoteclient_login(): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 430 | self.logger.info( |
| 431 | "\n\t[Check] %s SSH connection established.\t [OK]" |
| 432 | % self.hostname |
| 433 | ) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 434 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 435 | # Check scp connection. |
| 436 | # If scp connection fails, |
| 437 | # continue with FFDC generation but skip scp files to local host. |
| 438 | self.ssh_remoteclient.scp_connection() |
| 439 | return True |
| 440 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 441 | self.logger.info( |
| 442 | "\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" |
| 443 | % self.hostname |
| 444 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 445 | return False |
| 446 | |
| 447 | def telnet_to_target_system(self): |
| 448 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 449 | Establish a Telnet connection to the target system. |
| 450 | |
| 451 | This method establishes a Telnet connection to the target system using |
| 452 | the provided hostname, username, and Telnet port. If the connection is |
| 453 | successful, the method returns True. Otherwise, it logs an error |
| 454 | message and returns False. |
| 455 | |
| 456 | Returns: |
| 457 | bool: True if the connection is successful, False otherwise. |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 458 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 459 | self.telnet_remoteclient = TelnetRemoteclient( |
| 460 | self.hostname, self.username, self.password |
| 461 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 462 | if self.telnet_remoteclient.tn_remoteclient_login(): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 463 | self.logger.info( |
| 464 | "\n\t[Check] %s Telnet connection established.\t [OK]" |
| 465 | % self.hostname |
| 466 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 467 | return True |
| 468 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 469 | self.logger.info( |
| 470 | "\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" |
| 471 | % self.hostname |
| 472 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 473 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 474 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 475 | def generate_ffdc(self, working_protocol_list): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 476 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 477 | Generate FFDC (First Failure Data Capture) based on the remote host |
| 478 | type and working protocols. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 479 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 480 | This method determines the actions to be performed for generating FFDC |
| 481 | based on the remote host type and the list of confirmed working |
| 482 | protocols. The method iterates through the available actions for the |
| 483 | remote host type and checks if any of the working protocols are |
| 484 | supported. If a supported protocol is found, the method executes the |
| 485 | corresponding FFDC generation action. |
| 486 | |
| 487 | Parameters: |
| 488 | working_protocol_list (list): A list of confirmed working |
| 489 | protocols to connect to the |
| 490 | remote host. |
| 491 | |
| 492 | Returns: |
| 493 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 494 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 495 | self.logger.info( |
| 496 | "\n\t---- Executing commands on " + self.hostname + " ----" |
| 497 | ) |
| 498 | self.logger.info( |
| 499 | "\n\tWorking protocol list: %s" % working_protocol_list |
| 500 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 501 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 502 | config_dict = self.ffdc_actions |
| 503 | for target_type in config_dict.keys(): |
| 504 | if self.target_type != target_type: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 505 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 506 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 507 | self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 508 | global_plugin_dict["global_log_store_path"] = self.ffdc_dir_path |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 509 | self.logger.info("\tSystem Type: %s" % target_type) |
| 510 | for k, v in config_dict[target_type].items(): |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 511 | protocol = v["PROTOCOL"][0] |
| 512 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 513 | if ( |
| 514 | self.remote_protocol not in working_protocol_list |
| 515 | and self.remote_protocol != "ALL" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 516 | ) or protocol not in working_protocol_list: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 517 | continue |
| 518 | |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 519 | if protocol in working_protocol_list: |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 520 | if protocol in ["SSH", "SCP"]: |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 521 | self.protocol_ssh(protocol, target_type, k) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 522 | elif protocol == "TELNET": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 523 | self.protocol_telnet(target_type, k) |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 524 | elif protocol in ["REDFISH", "IPMI", "SHELL"]: |
| 525 | self.protocol_service_execute(protocol, target_type, k) |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 526 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 527 | self.logger.error( |
| 528 | "\n\tERROR: %s is not available for %s." |
| 529 | % (protocol, self.hostname) |
| 530 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 531 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 532 | # Close network connection after collecting all files |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 533 | self.elapsed_time = time.strftime( |
| 534 | "%H:%M:%S", time.gmtime(time.time() - self.start_time) |
| 535 | ) |
George Keishing | 48972ba | 2025-05-05 17:40:29 +0530 | [diff] [blame] | 536 | self.logger.info("\n\tTotal time taken: %s" % self.elapsed_time) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 537 | if self.ssh_remoteclient: |
| 538 | self.ssh_remoteclient.ssh_remoteclient_disconnect() |
| 539 | if self.telnet_remoteclient: |
| 540 | self.telnet_remoteclient.tn_remoteclient_disconnect() |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 541 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 542 | def protocol_ssh(self, protocol, target_type, sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 543 | r""" |
| 544 | Perform actions using SSH and SCP protocols. |
| 545 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 546 | This method executes a set of commands using the SSH protocol to |
| 547 | connect to the target system and collect FFDC data. The method takes |
| 548 | the protocol, target type, and sub-type as arguments and performs the |
| 549 | corresponding actions based on the provided parameters. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 550 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 551 | Parameters: |
| 552 | protocol (str): The protocol to execute (SSH or SCP). |
| 553 | target_type (str): The type group of the remote host. |
| 554 | sub_type (str): The group type of commands to execute. |
| 555 | |
| 556 | Returns: |
| 557 | None |
| 558 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 559 | if protocol == "SCP": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 560 | self.group_copy(self.ffdc_actions[target_type][sub_type]) |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 561 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 562 | self.collect_and_copy_ffdc( |
| 563 | self.ffdc_actions[target_type][sub_type] |
| 564 | ) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 565 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 566 | def protocol_telnet(self, target_type, sub_type): |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 567 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 568 | Perform actions using the Telnet protocol. |
| 569 | |
| 570 | This method executes a set of commands using the Telnet protocol to |
| 571 | connect to the target system and collect FFDC data. The method takes |
| 572 | the target type and sub-type as arguments and performs the |
| 573 | corresponding actions based on the provided parameters. |
| 574 | |
| 575 | Parameters: |
| 576 | target_type (str): The type group of the remote host. |
| 577 | sub_type (str): The group type of commands to execute. |
| 578 | |
| 579 | Returns: |
| 580 | None |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 581 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 582 | self.logger.info( |
| 583 | "\n\t[Run] Executing commands on %s using %s" |
| 584 | % (self.hostname, "TELNET") |
| 585 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 586 | telnet_files_saved = [] |
| 587 | progress_counter = 0 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 588 | list_of_commands = self.ffdc_actions[target_type][sub_type]["COMMANDS"] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 589 | for index, each_cmd in enumerate(list_of_commands, start=0): |
| 590 | command_txt, command_timeout = self.unpack_command(each_cmd) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 591 | result = self.telnet_remoteclient.execute_command( |
| 592 | command_txt, command_timeout |
| 593 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 594 | if result: |
| 595 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 596 | targ_file = self.ffdc_actions[target_type][sub_type][ |
| 597 | "FILES" |
| 598 | ][index] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 599 | except IndexError: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 600 | targ_file = command_txt |
| 601 | self.logger.warning( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 602 | "\n\t[WARN] Missing filename to store data from" |
| 603 | " telnet %s." % each_cmd |
| 604 | ) |
| 605 | self.logger.warning( |
| 606 | "\t[WARN] Data will be stored in %s." % targ_file |
| 607 | ) |
| 608 | targ_file_with_path = ( |
| 609 | self.ffdc_dir_path + self.ffdc_prefix + targ_file |
| 610 | ) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 611 | # Creates a new file |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 612 | with open(targ_file_with_path, "w") as fp: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 613 | fp.write(result) |
| 614 | fp.close |
| 615 | telnet_files_saved.append(targ_file) |
| 616 | progress_counter += 1 |
| 617 | self.print_progress(progress_counter) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 618 | 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] | 619 | for file in telnet_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 620 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 621 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 622 | def protocol_service_execute(self, protocol, target_type, sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 623 | r""" |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 624 | Perform actions for a given protocol. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 625 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 626 | This method executes a set of commands using the specified protocol to |
| 627 | connect to the target system and collect FFDC data. The method takes |
| 628 | the protocol, target type, and sub-type as arguments and performs the |
| 629 | corresponding actions based on the provided parameters. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 630 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 631 | Parameters: |
| 632 | protocol (str): The protocol to execute |
| 633 | (REDFISH, IPMI, or SHELL). |
| 634 | target_type (str): The type group of the remote host. |
| 635 | sub_type (str): The group type of commands to execute. |
| 636 | |
| 637 | Returns: |
| 638 | None |
| 639 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 640 | self.logger.info( |
| 641 | "\n\t[Run] Executing commands to %s using %s" |
| 642 | % (self.hostname, protocol) |
| 643 | ) |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 644 | executed_files_saved = [] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 645 | progress_counter = 0 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 646 | list_of_cmd = self.get_command_list( |
| 647 | self.ffdc_actions[target_type][sub_type] |
| 648 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 649 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 650 | plugin_call = False |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 651 | if isinstance(each_cmd, dict): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 652 | if "plugin" in each_cmd: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 653 | # If the error is set and plugin explicitly |
| 654 | # requested to skip execution on error.. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 655 | if plugin_error_dict[ |
| 656 | "exit_on_error" |
| 657 | ] and self.plugin_error_check(each_cmd["plugin"]): |
| 658 | self.logger.info( |
| 659 | "\n\t[PLUGIN-ERROR] exit_on_error: %s" |
| 660 | % plugin_error_dict["exit_on_error"] |
| 661 | ) |
| 662 | self.logger.info( |
| 663 | "\t[PLUGIN-SKIP] %s" % each_cmd["plugin"][0] |
| 664 | ) |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 665 | continue |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 666 | plugin_call = True |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 667 | # call the plugin |
| 668 | self.logger.info("\n\t[PLUGIN-START]") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 669 | result = self.execute_plugin_block(each_cmd["plugin"]) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 670 | self.logger.info("\t[PLUGIN-END]\n") |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 671 | else: |
George Keishing | 2b83e04 | 2021-08-03 12:56:11 -0500 | [diff] [blame] | 672 | each_cmd = self.yaml_env_and_plugin_vars_populate(each_cmd) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 673 | |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 674 | if not plugin_call: |
| 675 | result = self.run_tool_cmd(each_cmd) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 676 | if result: |
| 677 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 678 | file_name = self.get_file_list( |
| 679 | self.ffdc_actions[target_type][sub_type] |
| 680 | )[index] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 681 | # If file is specified as None. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 682 | if file_name == "None": |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 683 | continue |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 684 | targ_file = self.yaml_env_and_plugin_vars_populate( |
| 685 | file_name |
| 686 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 687 | except IndexError: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 688 | targ_file = each_cmd.split("/")[-1] |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 689 | self.logger.warning( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 690 | "\n\t[WARN] Missing filename to store data from %s." |
| 691 | % each_cmd |
| 692 | ) |
| 693 | self.logger.warning( |
| 694 | "\t[WARN] Data will be stored in %s." % targ_file |
| 695 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 696 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 697 | targ_file_with_path = ( |
| 698 | self.ffdc_dir_path + self.ffdc_prefix + targ_file |
| 699 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 700 | |
| 701 | # Creates a new file |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 702 | with open(targ_file_with_path, "w") as fp: |
George Keishing | 91308ea | 2021-08-10 14:43:15 -0500 | [diff] [blame] | 703 | if isinstance(result, dict): |
| 704 | fp.write(json.dumps(result)) |
| 705 | else: |
| 706 | fp.write(result) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 707 | fp.close |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 708 | executed_files_saved.append(targ_file) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 709 | |
| 710 | progress_counter += 1 |
| 711 | self.print_progress(progress_counter) |
| 712 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 713 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 714 | |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 715 | for file in executed_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 716 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 717 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 718 | def collect_and_copy_ffdc( |
| 719 | self, ffdc_actions_for_target_type, form_filename=False |
| 720 | ): |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 721 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 722 | Send commands and collect FFDC data from the targeted system. |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 723 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 724 | This method sends a set of commands and collects FFDC data from the |
| 725 | targeted system based on the provided ffdc_actions_for_target_type |
| 726 | dictionary. The method also has an optional form_filename parameter, |
| 727 | which, if set to True, prepends the target type to the output file |
| 728 | name. |
| 729 | |
| 730 | Parameters: |
| 731 | ffdc_actions_for_target_type (dict): A dictionary containing |
| 732 | commands and files for the |
| 733 | selected remote host type. |
| 734 | form_filename (bool, optional): If True, prepends the target |
| 735 | type to the output file name. |
| 736 | Defaults to False. |
| 737 | |
| 738 | Returns: |
| 739 | None |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 740 | """ |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 741 | # Executing commands, if any |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 742 | self.ssh_execute_ffdc_commands( |
| 743 | ffdc_actions_for_target_type, form_filename |
| 744 | ) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 745 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 746 | # Copying files |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 747 | if self.ssh_remoteclient.scpclient: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 748 | self.logger.info( |
| 749 | "\n\n\tCopying FFDC files from remote system %s.\n" |
| 750 | % self.hostname |
| 751 | ) |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 752 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 753 | # Retrieving files from target system |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 754 | list_of_files = self.get_file_list(ffdc_actions_for_target_type) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 755 | self.scp_ffdc( |
| 756 | self.ffdc_dir_path, |
| 757 | self.ffdc_prefix, |
| 758 | form_filename, |
| 759 | list_of_files, |
| 760 | ) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 761 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 762 | self.logger.info( |
| 763 | "\n\n\tSkip copying FFDC files from remote system %s.\n" |
| 764 | % self.hostname |
| 765 | ) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 766 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 767 | def get_command_list(self, ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 768 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 769 | Fetch a list of commands from the configuration file. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 770 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 771 | This method retrieves a list of commands from the |
| 772 | ffdc_actions_for_target_type dictionary, which contains commands and |
| 773 | files for the selected remote host type. The method returns the list |
| 774 | of commands. |
| 775 | |
| 776 | Parameters: |
| 777 | ffdc_actions_for_target_type (dict): A dictionary containing |
| 778 | commands and files for the |
| 779 | selected remote host type. |
| 780 | |
| 781 | Returns: |
| 782 | list: A list of commands. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 783 | """ |
| 784 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 785 | list_of_commands = ffdc_actions_for_target_type["COMMANDS"] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 786 | except KeyError: |
| 787 | list_of_commands = [] |
| 788 | return list_of_commands |
| 789 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 790 | def get_file_list(self, ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 791 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 792 | Fetch a list of files from the configuration file. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 793 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 794 | This method retrieves a list of files from the |
| 795 | ffdc_actions_for_target_type dictionary, which contains commands and |
| 796 | files for the selected remote host type. The method returns the list |
| 797 | of files. |
| 798 | |
| 799 | Parameters: |
| 800 | ffdc_actions_for_target_type (dict): A dictionary containing |
| 801 | commands and files for the |
| 802 | selected remote host type. |
| 803 | |
| 804 | Returns: |
| 805 | list: A list of files. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 806 | """ |
| 807 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 808 | list_of_files = ffdc_actions_for_target_type["FILES"] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 809 | except KeyError: |
| 810 | list_of_files = [] |
| 811 | return list_of_files |
| 812 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 813 | def unpack_command(self, command): |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 814 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 815 | Unpack a command from the configuration file, handling both dictionary |
| 816 | and string inputs. |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 817 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 818 | This method takes a command from the configuration file, which can be |
| 819 | either a dictionary or a string. If the input is a dictionary, the |
| 820 | method extracts the command text and timeout from the dictionary. |
| 821 | If the input is a string, the method assumes a default timeout of |
| 822 | 60 seconds. |
| 823 | The method returns a tuple containing the command text and timeout. |
| 824 | |
| 825 | Parameters: |
| 826 | command (dict or str): A command from the configuration file, |
| 827 | which can be either a dictionary or a |
| 828 | string. |
| 829 | |
| 830 | Returns: |
| 831 | tuple: A tuple containing the command text and timeout. |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 832 | """ |
| 833 | if isinstance(command, dict): |
| 834 | command_txt = next(iter(command)) |
| 835 | command_timeout = next(iter(command.values())) |
| 836 | elif isinstance(command, str): |
| 837 | command_txt = command |
| 838 | # Default command timeout 60 seconds |
| 839 | command_timeout = 60 |
| 840 | |
| 841 | return command_txt, command_timeout |
| 842 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 843 | def ssh_execute_ffdc_commands( |
| 844 | self, ffdc_actions_for_target_type, form_filename=False |
| 845 | ): |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 846 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 847 | Send commands in the ffdc_config file to the targeted system using SSH. |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 848 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 849 | This method sends a set of commands and collects FFDC data from the |
| 850 | targeted system using the SSH protocol. The method takes the |
| 851 | ffdc_actions_for_target_type dictionary and an optional |
| 852 | form_filename parameter as arguments. |
| 853 | |
| 854 | If form_filename is set to True, the method prepends the target type |
| 855 | to the output file name. The method returns the output of the executed |
| 856 | commands. |
| 857 | |
| 858 | It also prints the progress counter string + on the console. |
| 859 | |
| 860 | Parameters: |
| 861 | ffdc_actions_for_target_type (dict): A dictionary containing |
| 862 | commands and files for the |
| 863 | selected remote host type. |
| 864 | form_filename (bool, optional): If True, prepends the target |
| 865 | type to the output file name. |
| 866 | Defaults to False. |
| 867 | |
| 868 | Returns: |
| 869 | None |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 870 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 871 | self.logger.info( |
| 872 | "\n\t[Run] Executing commands on %s using %s" |
| 873 | % (self.hostname, ffdc_actions_for_target_type["PROTOCOL"][0]) |
| 874 | ) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 875 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 876 | 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] | 877 | # If command list is empty, returns |
| 878 | if not list_of_commands: |
| 879 | return |
| 880 | |
| 881 | progress_counter = 0 |
| 882 | for command in list_of_commands: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 883 | command_txt, command_timeout = self.unpack_command(command) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 884 | |
| 885 | if form_filename: |
| 886 | command_txt = str(command_txt % self.target_type) |
| 887 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 888 | ( |
| 889 | cmd_exit_code, |
| 890 | err, |
| 891 | response, |
| 892 | ) = self.ssh_remoteclient.execute_command( |
| 893 | command_txt, command_timeout |
| 894 | ) |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 895 | |
| 896 | if cmd_exit_code: |
| 897 | self.logger.warning( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 898 | "\n\t\t[WARN] %s exits with code %s." |
| 899 | % (command_txt, str(cmd_exit_code)) |
| 900 | ) |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 901 | self.logger.warning("\t\t[WARN] %s " % err) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 902 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 903 | progress_counter += 1 |
| 904 | self.print_progress(progress_counter) |
| 905 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 906 | 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] | 907 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 908 | def group_copy(self, ffdc_actions_for_target_type): |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 909 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 910 | SCP a group of files (wildcard) from the remote host. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 911 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 912 | This method copies a group of files from the remote host using the SCP |
| 913 | protocol. The method takes the fdc_actions_for_target_type dictionary |
| 914 | as an argument, which contains commands and files for the selected |
| 915 | remote host type. |
| 916 | |
| 917 | Parameters: |
| 918 | fdc_actions_for_target_type (dict): A dictionary containing |
| 919 | commands and files for the |
| 920 | selected remote host type. |
| 921 | |
| 922 | Returns: |
| 923 | None |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 924 | """ |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 925 | if self.ssh_remoteclient.scpclient: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 926 | self.logger.info( |
| 927 | "\n\tCopying files from remote system %s via SCP.\n" |
| 928 | % self.hostname |
| 929 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 930 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 931 | list_of_commands = self.get_command_list( |
| 932 | ffdc_actions_for_target_type |
| 933 | ) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 934 | # If command list is empty, returns |
| 935 | if not list_of_commands: |
| 936 | return |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 937 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 938 | for command in list_of_commands: |
| 939 | try: |
George Keishing | b4540e7 | 2021-08-02 13:48:46 -0500 | [diff] [blame] | 940 | command = self.yaml_env_and_plugin_vars_populate(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 941 | except IndexError: |
George Keishing | b4540e7 | 2021-08-02 13:48:46 -0500 | [diff] [blame] | 942 | self.logger.error("\t\tInvalid command %s" % command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 943 | continue |
| 944 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 945 | ( |
| 946 | cmd_exit_code, |
| 947 | err, |
| 948 | response, |
| 949 | ) = self.ssh_remoteclient.execute_command(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 950 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 951 | # If file does not exist, code take no action. |
| 952 | # cmd_exit_code is ignored for this scenario. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 953 | if response: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 954 | scp_result = self.ssh_remoteclient.scp_file_from_remote( |
| 955 | response.split("\n"), self.ffdc_dir_path |
| 956 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 957 | if scp_result: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 958 | self.logger.info( |
| 959 | "\t\tSuccessfully copied from " |
| 960 | + self.hostname |
| 961 | + ":" |
| 962 | + command |
| 963 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 964 | else: |
George Keishing | a56e87b | 2021-08-06 00:24:19 -0500 | [diff] [blame] | 965 | self.logger.info("\t\t%s has no result" % command) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 966 | |
| 967 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 968 | self.logger.info( |
| 969 | "\n\n\tSkip copying files from remote system %s.\n" |
| 970 | % self.hostname |
| 971 | ) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 972 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 973 | def scp_ffdc( |
| 974 | self, |
| 975 | targ_dir_path, |
| 976 | targ_file_prefix, |
| 977 | form_filename, |
| 978 | file_list=None, |
| 979 | quiet=None, |
| 980 | ): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 981 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 982 | SCP all files in the file_dict to the indicated directory on the local |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 983 | system. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 984 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 985 | This method copies all files specified in the file_dict dictionary |
| 986 | from the targeted system to the local system using the SCP protocol. |
| 987 | The method takes the target directory path, target file prefix, and a |
| 988 | boolean flag form_filename as required arguments. |
| 989 | |
| 990 | The file_dict argument is optional and contains the files to be copied. |
| 991 | The quiet argument is also optional and, if set to True, suppresses |
| 992 | the output of the SCP operation. |
| 993 | |
| 994 | Parameters: |
| 995 | targ_dir_path (str): The path of the directory to receive |
| 996 | the files on the local system. |
| 997 | targ_file_prefix (str): Prefix which will be prepended to each |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 998 | target file's name. |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 999 | form_filename (bool): If True, prepends the target type to |
| 1000 | the file names. |
| 1001 | file_dict (dict, optional): A dictionary containing the files to |
| 1002 | be copied. Defaults to None. |
| 1003 | quiet (bool, optional): If True, suppresses the output of the |
| 1004 | SCP operation. Defaults to None. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1005 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1006 | Returns: |
| 1007 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1008 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1009 | progress_counter = 0 |
| 1010 | for filename in file_list: |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 1011 | if form_filename: |
| 1012 | filename = str(filename % self.target_type) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1013 | source_file_path = filename |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1014 | targ_file_path = ( |
| 1015 | targ_dir_path + targ_file_prefix + filename.split("/")[-1] |
| 1016 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1017 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 1018 | # If source file name contains wild card, copy filename as is. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1019 | if "*" in source_file_path: |
| 1020 | scp_result = self.ssh_remoteclient.scp_file_from_remote( |
| 1021 | source_file_path, self.ffdc_dir_path |
| 1022 | ) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 1023 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1024 | scp_result = self.ssh_remoteclient.scp_file_from_remote( |
| 1025 | source_file_path, targ_file_path |
| 1026 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1027 | |
| 1028 | if not quiet: |
| 1029 | if scp_result: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 1030 | self.logger.info( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1031 | "\t\tSuccessfully copied from " |
| 1032 | + self.hostname |
| 1033 | + ":" |
| 1034 | + source_file_path |
| 1035 | + ".\n" |
| 1036 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1037 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 1038 | self.logger.info( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1039 | "\t\tFail to copy from " |
| 1040 | + self.hostname |
| 1041 | + ":" |
| 1042 | + source_file_path |
| 1043 | + ".\n" |
| 1044 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1045 | else: |
| 1046 | progress_counter += 1 |
| 1047 | self.print_progress(progress_counter) |
| 1048 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 1049 | def set_ffdc_default_store_path(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1050 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1051 | Set default values for self.ffdc_dir_path and self.ffdc_prefix. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1052 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1053 | This method sets default values for the self.ffdc_dir_path and |
| 1054 | self.ffdc_prefix class variables. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1055 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1056 | The collected FFDC files will be stored in the directory |
| 1057 | /self.location/hostname_timestr/, with individual files having the |
| 1058 | format timestr_filename where timestr is in %Y%m%d-%H%M%S. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1059 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1060 | Returns: |
| 1061 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1062 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1063 | timestr = time.strftime("%Y%m%d-%H%M%S") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1064 | self.ffdc_dir_path = ( |
| 1065 | self.location + "/" + self.hostname + "_" + timestr + "/" |
| 1066 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1067 | self.ffdc_prefix = timestr + "_" |
| 1068 | self.validate_local_store(self.ffdc_dir_path) |
| 1069 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 1070 | # Need to verify local store path exists prior to instantiate this class. |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1071 | # This class method to validate log path before referencing this class. |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 1072 | @classmethod |
| 1073 | def validate_local_store(cls, dir_path): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1074 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1075 | Ensure the specified directory exists to store FFDC files locally. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1076 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1077 | This method checks if the provided dir_path exists. If the directory |
| 1078 | does not exist, the method creates it. The method does not return any |
| 1079 | value. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1080 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1081 | Parameters: |
| 1082 | dir_path (str): The directory path where collected FFDC data files |
| 1083 | will be stored. |
| 1084 | |
| 1085 | Returns: |
| 1086 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1087 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1088 | if not os.path.exists(dir_path): |
| 1089 | try: |
George Keishing | 7b3a513 | 2021-07-13 09:24:02 -0500 | [diff] [blame] | 1090 | os.makedirs(dir_path, 0o755) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1091 | except (IOError, OSError) as e: |
| 1092 | # PermissionError |
| 1093 | if e.errno == EPERM or e.errno == EACCES: |
George Keishing | 1535205 | 2025-04-24 18:55:47 +0530 | [diff] [blame] | 1094 | print( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1095 | "\tERROR: os.makedirs %s failed with" |
| 1096 | " PermissionError.\n" % dir_path |
| 1097 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1098 | else: |
George Keishing | 1535205 | 2025-04-24 18:55:47 +0530 | [diff] [blame] | 1099 | print( |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1100 | "\tERROR: os.makedirs %s failed with %s.\n" |
| 1101 | % (dir_path, e.strerror) |
| 1102 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1103 | sys.exit(-1) |
| 1104 | |
| 1105 | def print_progress(self, progress): |
| 1106 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1107 | Print the current activity progress. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1108 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1109 | This method prints the current activity progress using the provided |
| 1110 | progress counter. The method does not return any value. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1111 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1112 | Parameters: |
| 1113 | progress (int): The current activity progress counter. |
| 1114 | |
| 1115 | Returns: |
| 1116 | None |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1117 | """ |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1118 | sys.stdout.write("\r\t" + "+" * progress) |
| 1119 | sys.stdout.flush() |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1120 | time.sleep(0.1) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 1121 | |
| 1122 | def verify_redfish(self): |
| 1123 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1124 | Verify if the remote host has the Redfish service active. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 1125 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1126 | This method checks if the remote host has the Redfish service active |
| 1127 | by sending a GET request to the Redfish base URL /redfish/v1/. |
| 1128 | If the request is successful (status code 200), the method returns |
| 1129 | stdout output of the run else error message. |
| 1130 | |
| 1131 | Returns: |
| 1132 | str: Redfish service executed output. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 1133 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1134 | redfish_parm = ( |
| 1135 | "redfishtool -r " |
| 1136 | + self.hostname |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 1137 | + ":" |
| 1138 | + self.port_https |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1139 | + " -S Always raw GET /redfish/v1/" |
| 1140 | ) |
| 1141 | return self.run_tool_cmd(redfish_parm, True) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 1142 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1143 | def verify_ipmi(self): |
| 1144 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1145 | Verify if the remote host has the IPMI LAN service active. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1146 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1147 | This method checks if the remote host has the IPMI LAN service active |
| 1148 | by sending an IPMI "power status" command. |
| 1149 | |
| 1150 | If the command is successful (returns a non-empty response), |
| 1151 | else error message. |
| 1152 | |
| 1153 | Returns: |
| 1154 | str: IPMI LAN service executed output. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1155 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1156 | if self.target_type == "OPENBMC": |
| 1157 | ipmi_parm = ( |
| 1158 | "ipmitool -I lanplus -C 17 -U " |
| 1159 | + self.username |
| 1160 | + " -P " |
| 1161 | + self.password |
| 1162 | + " -H " |
| 1163 | + self.hostname |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 1164 | + " -p " |
| 1165 | + str(self.port_ipmi) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1166 | + " power status" |
| 1167 | ) |
George Keishing | 484f824 | 2021-07-27 01:42:02 -0500 | [diff] [blame] | 1168 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1169 | ipmi_parm = ( |
| 1170 | "ipmitool -I lanplus -P " |
| 1171 | + self.password |
| 1172 | + " -H " |
| 1173 | + self.hostname |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 1174 | + " -p " |
| 1175 | + str(self.port_ipmi) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1176 | + " power status" |
| 1177 | ) |
George Keishing | 484f824 | 2021-07-27 01:42:02 -0500 | [diff] [blame] | 1178 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1179 | return self.run_tool_cmd(ipmi_parm, True) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1180 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1181 | def run_tool_cmd(self, parms_string, quiet=False): |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1182 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1183 | Run a CLI standard tool or script with the provided command options. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1184 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1185 | This method runs a CLI standard tool or script with the provided |
| 1186 | parms_string command options. If the quiet parameter is set to True, |
| 1187 | the method suppresses the output of the command. |
| 1188 | The method returns the output of the command as a string. |
| 1189 | |
| 1190 | Parameters: |
| 1191 | parms_string (str): The command options for the CLI tool or |
| 1192 | script. |
| 1193 | quiet (bool, optional): If True, suppresses the output of the |
| 1194 | command. Defaults to False. |
| 1195 | |
| 1196 | Returns: |
| 1197 | str: The output of the command as a string. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1198 | """ |
| 1199 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1200 | result = subprocess.run( |
| 1201 | [parms_string], |
| 1202 | stdout=subprocess.PIPE, |
| 1203 | stderr=subprocess.PIPE, |
| 1204 | shell=True, |
| 1205 | universal_newlines=True, |
| 1206 | ) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1207 | |
| 1208 | if result.stderr and not quiet: |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1209 | if self.password in parms_string: |
| 1210 | parms_string = parms_string.replace(self.password, "********") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1211 | self.logger.error("\n\t\tERROR with %s " % parms_string) |
| 1212 | self.logger.error("\t\t" + result.stderr) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 1213 | |
| 1214 | return result.stdout |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 1215 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1216 | def verify_protocol(self, protocol_list): |
| 1217 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1218 | Perform a working check for the provided list of protocols. |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1219 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1220 | This method checks if the specified protocols are available on the |
| 1221 | remote host. The method iterates through the protocol_list and |
| 1222 | attempts to establish a connection using each protocol. |
| 1223 | |
| 1224 | If a connection is successfully established, the method append to the |
| 1225 | list and if any protocol fails to connect, the method ignores it. |
| 1226 | |
| 1227 | Parameters: |
| 1228 | protocol_list (list): A list of protocols to check. |
| 1229 | |
| 1230 | Returns: |
| 1231 | list: All protocols are available list. |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1232 | """ |
| 1233 | |
| 1234 | tmp_list = [] |
| 1235 | if self.target_is_pingable(): |
| 1236 | tmp_list.append("SHELL") |
| 1237 | |
| 1238 | for protocol in protocol_list: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1239 | if self.remote_protocol != "ALL": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1240 | if self.remote_protocol != protocol: |
| 1241 | continue |
| 1242 | |
| 1243 | # Only check SSH/SCP once for both protocols |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1244 | if ( |
| 1245 | protocol == "SSH" |
| 1246 | or protocol == "SCP" |
| 1247 | and protocol not in tmp_list |
| 1248 | ): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1249 | if self.ssh_to_target_system(): |
George Keishing | aa63870 | 2021-07-26 11:48:28 -0500 | [diff] [blame] | 1250 | # Add only what user asked. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1251 | if self.remote_protocol != "ALL": |
George Keishing | aa63870 | 2021-07-26 11:48:28 -0500 | [diff] [blame] | 1252 | tmp_list.append(self.remote_protocol) |
| 1253 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1254 | tmp_list.append("SSH") |
| 1255 | tmp_list.append("SCP") |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1256 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1257 | if protocol == "TELNET": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1258 | if self.telnet_to_target_system(): |
| 1259 | tmp_list.append(protocol) |
| 1260 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1261 | if protocol == "REDFISH": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1262 | if self.verify_redfish(): |
| 1263 | tmp_list.append(protocol) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1264 | self.logger.info( |
| 1265 | "\n\t[Check] %s Redfish Service.\t\t [OK]" |
| 1266 | % self.hostname |
| 1267 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1268 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1269 | self.logger.info( |
| 1270 | "\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" |
| 1271 | % self.hostname |
| 1272 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1273 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1274 | if protocol == "IPMI": |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1275 | if self.verify_ipmi(): |
| 1276 | tmp_list.append(protocol) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1277 | self.logger.info( |
| 1278 | "\n\t[Check] %s IPMI LAN Service.\t\t [OK]" |
| 1279 | % self.hostname |
| 1280 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1281 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1282 | self.logger.info( |
| 1283 | "\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" |
| 1284 | % self.hostname |
| 1285 | ) |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 1286 | |
| 1287 | return tmp_list |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1288 | |
| 1289 | def load_env(self): |
| 1290 | r""" |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1291 | Load the user environment variables from a YAML file. |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1292 | |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1293 | This method reads the environment variables from a YAML file specified |
| 1294 | in the ENV_FILE environment variable. If the file is not found or |
| 1295 | there is an error reading the file, an exception is raised. |
| 1296 | |
| 1297 | The YAML file should have the following format: |
| 1298 | |
| 1299 | .. code-block:: yaml |
| 1300 | |
| 1301 | VAR_NAME: VAR_VALUE |
| 1302 | |
| 1303 | Where VAR_NAME is the name of the environment variable, and |
| 1304 | VAR_VALUE is its value. |
| 1305 | |
| 1306 | After loading the environment variables, they are stored in the |
| 1307 | self.env attribute for later use. |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1308 | """ |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1309 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1310 | os.environ["hostname"] = self.hostname |
| 1311 | os.environ["username"] = self.username |
| 1312 | os.environ["password"] = self.password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 1313 | os.environ["port_ssh"] = self.port_ssh |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 1314 | os.environ["port_https"] = self.port_https |
| 1315 | os.environ["port_ipmi"] = self.port_ipmi |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1316 | |
| 1317 | # Append default Env. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1318 | self.env_dict["hostname"] = self.hostname |
| 1319 | self.env_dict["username"] = self.username |
| 1320 | self.env_dict["password"] = self.password |
George Keishing | 7a61aa2 | 2023-06-26 13:18:37 +0530 | [diff] [blame] | 1321 | self.env_dict["port_ssh"] = self.port_ssh |
George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame] | 1322 | self.env_dict["port_https"] = self.port_https |
| 1323 | self.env_dict["port_ipmi"] = self.port_ipmi |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1324 | |
| 1325 | try: |
| 1326 | tmp_env_dict = {} |
| 1327 | if self.env_vars: |
| 1328 | tmp_env_dict = json.loads(self.env_vars) |
| 1329 | # Export ENV vars default. |
| 1330 | for key, value in tmp_env_dict.items(): |
| 1331 | os.environ[key] = value |
| 1332 | self.env_dict[key] = str(value) |
| 1333 | |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1334 | # Load user specified ENV config YAML. |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1335 | if self.econfig: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1336 | with open(self.econfig, "r") as file: |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 1337 | try: |
Yunyun Lin | f87cc0a | 2022-06-08 16:57:04 -0700 | [diff] [blame] | 1338 | tmp_env_dict = yaml.load(file, Loader=yaml.SafeLoader) |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 1339 | except yaml.YAMLError as e: |
| 1340 | self.logger.error(e) |
| 1341 | sys.exit(-1) |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1342 | # Export ENV vars. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1343 | for key, value in tmp_env_dict["env_params"].items(): |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1344 | os.environ[key] = str(value) |
| 1345 | self.env_dict[key] = str(value) |
| 1346 | except json.decoder.JSONDecodeError as e: |
| 1347 | self.logger.error("\n\tERROR: %s " % e) |
| 1348 | sys.exit(-1) |
George Keishing | 0e9b5ba | 2025-05-08 12:17:58 +0530 | [diff] [blame] | 1349 | except FileNotFoundError as e: |
| 1350 | self.logger.error("\n\tERROR: %s " % e) |
| 1351 | sys.exit(-1) |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1352 | |
| 1353 | # This to mask the password from displaying on the console. |
| 1354 | mask_dict = self.env_dict.copy() |
| 1355 | for k, v in mask_dict.items(): |
| 1356 | if k.lower().find("password") != -1: |
| 1357 | hidden_text = [] |
| 1358 | hidden_text.append(v) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1359 | password_regex = ( |
| 1360 | "(" + "|".join([re.escape(x) for x in hidden_text]) + ")" |
| 1361 | ) |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 1362 | mask_dict[k] = re.sub(password_regex, "********", v) |
| 1363 | |
| 1364 | self.logger.info(json.dumps(mask_dict, indent=8, sort_keys=False)) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1365 | |
| 1366 | def execute_python_eval(self, eval_string): |
| 1367 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1368 | Execute a qualified Python function string using the eval() function. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1369 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1370 | This method executes a provided Python function string using the |
| 1371 | eval() function. |
| 1372 | |
| 1373 | The method takes the eval_string as an argument, which is expected to |
| 1374 | be a valid Python function call. |
| 1375 | |
| 1376 | The method returns the result of the executed function. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1377 | |
| 1378 | Example: |
| 1379 | eval(plugin.foo_func.foo_func(10)) |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1380 | |
| 1381 | Parameters: |
| 1382 | eval_string (str): A valid Python function call string. |
| 1383 | |
| 1384 | Returns: |
| 1385 | str: The result of the executed function and on failure return |
| 1386 | PLUGIN_EVAL_ERROR. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1387 | """ |
| 1388 | try: |
George Keishing | dda48ce | 2021-08-12 07:02:27 -0500 | [diff] [blame] | 1389 | self.logger.info("\tExecuting plugin func()") |
| 1390 | self.logger.debug("\tCall func: %s" % eval_string) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1391 | result = eval(eval_string) |
| 1392 | self.logger.info("\treturn: %s" % str(result)) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1393 | except ( |
| 1394 | ValueError, |
| 1395 | SyntaxError, |
| 1396 | NameError, |
| 1397 | AttributeError, |
| 1398 | TypeError, |
| 1399 | ) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1400 | self.logger.error("\tERROR: execute_python_eval: %s" % e) |
| 1401 | # Set the plugin error state. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1402 | plugin_error_dict["exit_on_error"] = True |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1403 | self.logger.info("\treturn: PLUGIN_EVAL_ERROR") |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1404 | return "PLUGIN_EVAL_ERROR" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1405 | |
| 1406 | return result |
| 1407 | |
| 1408 | def execute_plugin_block(self, plugin_cmd_list): |
| 1409 | r""" |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1410 | Pack the plugin commands into qualified Python string objects. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1411 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1412 | This method processes the plugin_cmd_list argument, which is expected |
| 1413 | to contain a list of plugin commands read from a YAML file. The method |
| 1414 | iterates through the list, constructs a qualified Python string object |
| 1415 | for each plugin command, and returns a list of these string objects. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1416 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1417 | Parameters: |
| 1418 | plugin_cmd_list (list): A list of plugin commands containing |
| 1419 | plugin names and arguments. |
| 1420 | Plugin block read from YAML |
| 1421 | [ |
| 1422 | {'plugin_name':'plugin.foo_func.my_func'}, |
| 1423 | {'plugin_args':[10]}, |
| 1424 | ] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1425 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1426 | Example: |
| 1427 | Execute and no return response |
| 1428 | - plugin: |
| 1429 | - plugin_name: plugin.foo_func.my_func |
| 1430 | - plugin_args: |
| 1431 | - arg1 |
| 1432 | - arg2 |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1433 | |
George Keishing | 0441609 | 2025-05-17 18:47:10 +0530 | [diff] [blame^] | 1434 | Execute and return a response |
| 1435 | - plugin: |
| 1436 | - plugin_name: result = plugin.foo_func.my_func |
| 1437 | - plugin_args: |
| 1438 | - arg1 |
| 1439 | - arg2 |
| 1440 | |
| 1441 | Execute and return multiple values response |
| 1442 | - plugin: |
| 1443 | - plugin_name: result1,result2 = plugin.foo_func.my_func |
| 1444 | - plugin_args: |
| 1445 | - arg1 |
| 1446 | - arg2 |
| 1447 | |
| 1448 | Returns: |
| 1449 | str: Execute and not response or a string value(s) responses, |
| 1450 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1451 | """ |
| 1452 | try: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1453 | idx = self.key_index_list_dict("plugin_name", plugin_cmd_list) |
| 1454 | plugin_name = plugin_cmd_list[idx]["plugin_name"] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1455 | # Equal separator means plugin function returns result. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1456 | if " = " in plugin_name: |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1457 | # Ex. ['result', 'plugin.foo_func.my_func'] |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1458 | plugin_name_args = plugin_name.split(" = ") |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1459 | # plugin func return data. |
| 1460 | for arg in plugin_name_args: |
| 1461 | if arg == plugin_name_args[-1]: |
| 1462 | plugin_name = arg |
| 1463 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1464 | plugin_resp = arg.split(",") |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1465 | # ['result1','result2'] |
| 1466 | for x in plugin_resp: |
| 1467 | global_plugin_list.append(x) |
| 1468 | global_plugin_dict[x] = "" |
| 1469 | |
| 1470 | # Walk the plugin args ['arg1,'arg2'] |
| 1471 | # If the YAML plugin statement 'plugin_args' is not declared. |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1472 | plugin_args = [] |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1473 | if any("plugin_args" in d for d in plugin_cmd_list): |
| 1474 | idx = self.key_index_list_dict("plugin_args", plugin_cmd_list) |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1475 | if idx is not None: |
| 1476 | plugin_args = plugin_cmd_list[idx].get("plugin_args", []) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1477 | plugin_args = self.yaml_args_populate(plugin_args) |
| 1478 | else: |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1479 | plugin_args = self.yaml_args_populate([]) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1480 | |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1481 | # Pack the args list to string parameters for plugin function. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1482 | parm_args_str = self.yaml_args_string(plugin_args) |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1483 | |
| 1484 | """ |
| 1485 | Example of plugin_func: |
| 1486 | plugin.redfish.enumerate_request( |
| 1487 | "xx.xx.xx.xx:443", |
| 1488 | "root", |
| 1489 | "********", |
| 1490 | "/redfish/v1/", |
| 1491 | "json") |
| 1492 | """ |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1493 | if parm_args_str: |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1494 | plugin_func = f"{plugin_name}({parm_args_str})" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1495 | else: |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1496 | plugin_func = f"{plugin_name}()" |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1497 | |
| 1498 | # Execute plugin function. |
| 1499 | if global_plugin_dict: |
| 1500 | resp = self.execute_python_eval(plugin_func) |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1501 | # Update plugin vars dict if there is any. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1502 | if resp != "PLUGIN_EVAL_ERROR": |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1503 | self.response_args_data(resp) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1504 | else: |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 1505 | resp = self.execute_python_eval(plugin_func) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1506 | except Exception as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1507 | # Set the plugin error state. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1508 | plugin_error_dict["exit_on_error"] = True |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1509 | self.logger.error("\tERROR: execute_plugin_block: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1510 | pass |
| 1511 | |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1512 | # There is a real error executing the plugin function. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1513 | if resp == "PLUGIN_EVAL_ERROR": |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1514 | return resp |
| 1515 | |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1516 | # Check if plugin_expects_return (int, string, list,dict etc) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1517 | if any("plugin_expects_return" in d for d in plugin_cmd_list): |
| 1518 | idx = self.key_index_list_dict( |
| 1519 | "plugin_expects_return", plugin_cmd_list |
| 1520 | ) |
| 1521 | plugin_expects = plugin_cmd_list[idx]["plugin_expects_return"] |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1522 | if plugin_expects: |
| 1523 | if resp: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1524 | if ( |
| 1525 | self.plugin_expect_type(plugin_expects, resp) |
| 1526 | == "INVALID" |
| 1527 | ): |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1528 | self.logger.error("\tWARN: Plugin error check skipped") |
| 1529 | elif not self.plugin_expect_type(plugin_expects, resp): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1530 | self.logger.error( |
| 1531 | "\tERROR: Plugin expects return data: %s" |
| 1532 | % plugin_expects |
| 1533 | ) |
| 1534 | plugin_error_dict["exit_on_error"] = True |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1535 | elif not resp: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1536 | self.logger.error( |
| 1537 | "\tERROR: Plugin func failed to return data" |
| 1538 | ) |
| 1539 | plugin_error_dict["exit_on_error"] = True |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1540 | |
| 1541 | return resp |
| 1542 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1543 | def response_args_data(self, plugin_resp): |
| 1544 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1545 | Parse the plugin function response and update plugin return variable. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1546 | |
| 1547 | plugin_resp Response data from plugin function. |
| 1548 | """ |
| 1549 | resp_list = [] |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1550 | resp_data = "" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1551 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1552 | # There is nothing to update the plugin response. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1553 | if len(global_plugin_list) == 0 or plugin_resp == "None": |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1554 | return |
| 1555 | |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1556 | if isinstance(plugin_resp, str): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1557 | resp_data = plugin_resp.strip("\r\n\t") |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1558 | resp_list.append(resp_data) |
| 1559 | elif isinstance(plugin_resp, bytes): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1560 | resp_data = str(plugin_resp, "UTF-8").strip("\r\n\t") |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1561 | resp_list.append(resp_data) |
| 1562 | elif isinstance(plugin_resp, tuple): |
| 1563 | if len(global_plugin_list) == 1: |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1564 | resp_list.append(plugin_resp) |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1565 | else: |
| 1566 | resp_list = list(plugin_resp) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1567 | resp_list = [x.strip("\r\n\t") for x in resp_list] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1568 | elif isinstance(plugin_resp, list): |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1569 | if len(global_plugin_list) == 1: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1570 | 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] | 1571 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1572 | resp_list = [x.strip("\r\n\t") for x in plugin_resp] |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1573 | elif isinstance(plugin_resp, int) or isinstance(plugin_resp, float): |
| 1574 | resp_list.append(plugin_resp) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1575 | |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1576 | # Iterate if there is a list of plugin return vars to update. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1577 | for idx, item in enumerate(resp_list, start=0): |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1578 | # Exit loop, done required loop. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1579 | if idx >= len(global_plugin_list): |
| 1580 | break |
| 1581 | # Find the index of the return func in the list and |
| 1582 | # update the global func return dictionary. |
| 1583 | try: |
| 1584 | dict_idx = global_plugin_list[idx] |
| 1585 | global_plugin_dict[dict_idx] = item |
| 1586 | except (IndexError, ValueError) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1587 | self.logger.warn("\tWARN: response_args_data: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1588 | pass |
| 1589 | |
| 1590 | # Done updating plugin dict irrespective of pass or failed, |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1591 | # clear all the list element for next plugin block execute. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1592 | global_plugin_list.clear() |
| 1593 | |
| 1594 | def yaml_args_string(self, plugin_args): |
| 1595 | r""" |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1596 | Pack the arguments into a string representation. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1597 | |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1598 | This method processes the plugin_arg argument, which is expected to |
| 1599 | contain a list of arguments. The method iterates through the list, |
| 1600 | converts each argument to a string, and concatenates them into a |
| 1601 | single string. Special handling is applied for integer, float, and |
| 1602 | predefined plugin variable types. |
| 1603 | |
| 1604 | Ecample: |
| 1605 | From |
| 1606 | ['xx.xx.xx.xx:443', 'root', '********', '/redfish/v1/', 'json'] |
| 1607 | to |
| 1608 | "xx.xx.xx.xx:443","root","********","/redfish/v1/","json" |
| 1609 | |
| 1610 | Parameters: |
| 1611 | plugin_args (list): A list of arguments to be packed into |
| 1612 | a string. |
| 1613 | |
| 1614 | Returns: |
| 1615 | str: A string representation of the arguments. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1616 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1617 | args_str = "" |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1618 | |
| 1619 | for i, arg in enumerate(plugin_args): |
| 1620 | if arg: |
| 1621 | if isinstance(arg, (int, float)): |
| 1622 | args_str += str(arg) |
| 1623 | elif arg in global_plugin_type_list: |
| 1624 | args_str += str(global_plugin_dict[arg]) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1625 | else: |
George Keishing | 450f92f | 2025-05-15 23:12:51 +0530 | [diff] [blame] | 1626 | args_str += f'"{arg.strip("\r\n\t")}"' |
| 1627 | |
| 1628 | # Skip last list element. |
| 1629 | if i != len(plugin_args) - 1: |
| 1630 | args_str += "," |
| 1631 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1632 | return args_str |
| 1633 | |
| 1634 | def yaml_args_populate(self, yaml_arg_list): |
| 1635 | r""" |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1636 | Decode environment and plugin variables and populate the argument list. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1637 | |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1638 | This method processes the yaml_arg_list argument, which is expected to |
| 1639 | contain a list of arguments read from a YAML file. The method iterates |
| 1640 | through the list, decodes environment and plugin variables, and |
| 1641 | returns a populated list of arguments. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1642 | |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1643 | .. code-block:: yaml |
| 1644 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1645 | - plugin_args: |
| 1646 | - arg1 |
| 1647 | - arg2 |
| 1648 | |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1649 | ['${hostname}:${port_https}', '${username}', '/redfish/v1/', 'json'] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1650 | |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1651 | Returns the populated plugin list |
| 1652 | ['xx.xx.xx.xx:443', 'root', '/redfish/v1/', 'json'] |
| 1653 | |
| 1654 | Parameters: |
| 1655 | yaml_arg_list (list): A list of arguments containing environment |
| 1656 | and plugin variables. |
| 1657 | |
| 1658 | Returns: |
| 1659 | list: A populated list of arguments with decoded environment and |
| 1660 | plugin variables. |
| 1661 | """ |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1662 | if isinstance(yaml_arg_list, list): |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1663 | populated_list = [] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1664 | for arg in yaml_arg_list: |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1665 | if isinstance(arg, (int, float)): |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1666 | populated_list.append(arg) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1667 | elif isinstance(arg, str): |
| 1668 | arg_str = self.yaml_env_and_plugin_vars_populate(str(arg)) |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1669 | populated_list.append(arg_str) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1670 | else: |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1671 | populated_list.append(arg) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1672 | |
George Keishing | f0eb1d6 | 2025-05-14 15:07:02 +0530 | [diff] [blame] | 1673 | return populated_list |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1674 | |
| 1675 | def yaml_env_and_plugin_vars_populate(self, yaml_arg_str): |
| 1676 | r""" |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1677 | Update environment variables and plugin variables based on the |
| 1678 | provided YAML argument string. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1679 | |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1680 | This method processes the yaml_arg_str argument, which is expected |
| 1681 | to contain a string representing environment variables and plugin |
| 1682 | variables in the format: |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1683 | |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1684 | .. code-block:: yaml |
| 1685 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1686 | - cat ${MY_VAR} |
| 1687 | - ls -AX my_plugin_var |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1688 | |
| 1689 | The method parses the string, extracts the variable names, and updates |
| 1690 | the corresponding environment variables and plugin variables. |
| 1691 | |
| 1692 | Parameters: |
| 1693 | yaml_arg_str (str): A string containing environment and plugin |
| 1694 | variable definitions in YAML format. |
| 1695 | |
| 1696 | Returns: |
| 1697 | str: The updated YAML argument string with plugin variables |
| 1698 | replaced. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1699 | """ |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1700 | |
| 1701 | # Parse and convert the Plugin YAML vars string to python vars |
| 1702 | # Example: |
| 1703 | # ${my_hostname}:${port_https} -> ['my_hostname', 'port_https'] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1704 | try: |
George Keishing | c754b43 | 2025-04-24 14:27:14 +0530 | [diff] [blame] | 1705 | # Example, list of matching |
| 1706 | # env vars ['username', 'password', 'hostname'] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1707 | # Extra escape \ for special symbols. '\$\{([^\}]+)\}' works good. |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1708 | env_var_regex = r"\$\{([^\}]+)\}" |
| 1709 | env_var_names_list = re.findall(env_var_regex, yaml_arg_str) |
| 1710 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1711 | for var in env_var_names_list: |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1712 | env_var = os.environ.get(var) |
| 1713 | if env_var: |
| 1714 | env_replace = "${" + var + "}" |
| 1715 | yaml_arg_str = yaml_arg_str.replace(env_replace, env_var) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1716 | except Exception as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1717 | self.logger.error("\tERROR:yaml_env_vars_populate: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1718 | pass |
| 1719 | |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1720 | """ |
| 1721 | Parse the string for plugin vars. |
| 1722 | Implement the logic to update environment variables based on the |
| 1723 | extracted variable names. |
| 1724 | """ |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1725 | try: |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1726 | # Example, list of plugin vars env_var_names_list |
| 1727 | # ['my_hostname', 'port_https'] |
| 1728 | global_plugin_dict_keys = set(global_plugin_dict.keys()) |
| 1729 | # Skip env var list already populated above code block list. |
| 1730 | plugin_var_name_list = [ |
| 1731 | var |
| 1732 | for var in global_plugin_dict_keys |
| 1733 | if var not in env_var_names_list |
| 1734 | ] |
| 1735 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1736 | for var in plugin_var_name_list: |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1737 | plugin_var_value = global_plugin_dict[var] |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1738 | if yaml_arg_str in global_plugin_dict: |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1739 | """ |
| 1740 | If this plugin var exist but empty in dict, don't replace. |
| 1741 | his is either a YAML plugin statement incorrectly used or |
| 1742 | user added a plugin var which is not going to be populated. |
| 1743 | """ |
| 1744 | if isinstance(plugin_var_value, (list, dict)): |
| 1745 | """ |
| 1746 | List data type or dict can't be replaced, use |
| 1747 | directly in eval function call. |
| 1748 | """ |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1749 | global_plugin_type_list.append(var) |
| 1750 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1751 | yaml_arg_str = yaml_arg_str.replace( |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1752 | str(var), str(plugin_var_value) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1753 | ) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1754 | except (IndexError, ValueError) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1755 | self.logger.error("\tERROR: yaml_plugin_vars_populate: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1756 | pass |
| 1757 | |
George Keishing | a593f4b | 2025-05-13 20:02:36 +0530 | [diff] [blame] | 1758 | # From ${my_hostname}:${port_https} -> ['my_hostname', 'port_https'] |
| 1759 | # to populated values string as |
| 1760 | # Example: xx.xx.xx.xx:443 and return the string |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1761 | return yaml_arg_str |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1762 | |
| 1763 | def plugin_error_check(self, plugin_dict): |
| 1764 | r""" |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1765 | Process plugin error dictionary and return the corresponding error |
| 1766 | message. |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1767 | |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1768 | This method checks if any dictionary in the plugin_dict list contains |
| 1769 | a "plugin_error" key. If such a dictionary is found, it retrieves the |
| 1770 | value associated with the "plugin_error" key and returns the |
| 1771 | corresponding error message from the plugin_error_dict attribute. |
| 1772 | |
| 1773 | Parameters: |
| 1774 | plugin_dict (list of dict): A list of dictionaries containing |
| 1775 | plugin error information. |
| 1776 | |
| 1777 | Returns: |
| 1778 | str: The error message corresponding to the "plugin_error" value, |
| 1779 | or None if no error is found. |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1780 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1781 | if any("plugin_error" in d for d in plugin_dict): |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1782 | for d in plugin_dict: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1783 | if "plugin_error" in d: |
| 1784 | value = d["plugin_error"] |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1785 | return self.plugin_error_dict.get(value, None) |
| 1786 | return None |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1787 | |
| 1788 | def key_index_list_dict(self, key, list_dict): |
| 1789 | r""" |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1790 | Find the index of the first dictionary in the list that contains |
| 1791 | the specified key. |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1792 | |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1793 | Parameters: |
| 1794 | key (str): The key to search for in the |
| 1795 | dictionaries. |
| 1796 | list_dict (list of dict): A list of dictionaries to search |
| 1797 | through. |
| 1798 | |
| 1799 | Returns: |
| 1800 | int: The index of the first dictionary containing the key, or -1 |
| 1801 | if no match is found. |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1802 | """ |
| 1803 | for i, d in enumerate(list_dict): |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1804 | if key in d: |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1805 | return i |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1806 | return -1 |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1807 | |
| 1808 | def plugin_expect_type(self, type, data): |
| 1809 | r""" |
George Keishing | 1e87742 | 2025-05-09 20:45:09 +0530 | [diff] [blame] | 1810 | Check if the provided data matches the expected type. |
| 1811 | |
| 1812 | This method checks if the data argument matches the specified type. |
| 1813 | It supports the following types: "int", "float", "str", "list", "dict", |
| 1814 | and "tuple". |
| 1815 | |
| 1816 | If the type is not recognized, it logs an info message and returns |
| 1817 | "INVALID". |
| 1818 | |
| 1819 | Parameters: |
| 1820 | type (str): The expected data type. |
| 1821 | data: The data to check against the expected type. |
| 1822 | |
| 1823 | Returns: |
| 1824 | bool or str: True if the data matches the expected type, False if |
| 1825 | not, or "INVALID" if the type is not recognized. |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1826 | """ |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1827 | if type == "int": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1828 | return isinstance(data, int) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1829 | elif type == "float": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1830 | return isinstance(data, float) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1831 | elif type == "str": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1832 | return isinstance(data, str) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1833 | elif type == "list": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1834 | return isinstance(data, list) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1835 | elif type == "dict": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1836 | return isinstance(data, dict) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1837 | elif type == "tuple": |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1838 | return isinstance(data, tuple) |
| 1839 | else: |
| 1840 | self.logger.info("\tInvalid data type requested: %s" % type) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 1841 | return "INVALID" |