| 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""" | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 4 | CLI FFDC Collector. | 
|  | 5 | """ | 
|  | 6 |  | 
|  | 7 | import os | 
|  | 8 | import sys | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 9 |  | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 10 | import click | 
|  | 11 |  | 
|  | 12 | # ---------Set sys.path for cli command execution--------------------------------------- | 
|  | 13 | # Absolute path to openbmc-test-automation/ffdc | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 14 | abs_path = os.path.abspath(os.path.dirname(sys.argv[0])) | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 15 | full_path = abs_path.split("ffdc")[0] | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 16 | sys.path.append(full_path) | 
|  | 17 | # Walk path and append to sys.path | 
|  | 18 | for root, dirs, files in os.walk(full_path): | 
|  | 19 | for found_dir in dirs: | 
|  | 20 | sys.path.append(os.path.join(root, found_dir)) | 
|  | 21 |  | 
| George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 22 | from ffdc_collector import ffdc_collector  # NOQA | 
| George Keishing | 37c58c8 | 2022-12-08 07:42:54 -0600 | [diff] [blame] | 23 |  | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 24 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 25 | @click.command(context_settings=dict(help_option_names=["-h", "--help"])) | 
|  | 26 | @click.option("-r", "--remote", help="Hostname/IP of the remote host") | 
|  | 27 | @click.option("-u", "--username", help="Username of the remote host.") | 
|  | 28 | @click.option("-p", "--password", help="Password of the remote host.") | 
|  | 29 | @click.option( | 
| George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame^] | 30 | "-port_https", default=443, show_default=True, help="HTTPS port value." | 
|  | 31 | ) | 
|  | 32 | @click.option( | 
|  | 33 | "-port_ipmi", default=623, show_default=True, help="IPMI port value." | 
|  | 34 | ) | 
|  | 35 | @click.option( | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 36 | "-c", | 
|  | 37 | "--config", | 
|  | 38 | default=abs_path + "/ffdc_config.yaml", | 
|  | 39 | show_default=True, | 
|  | 40 | help="YAML Configuration file for log collection.", | 
|  | 41 | ) | 
|  | 42 | @click.option( | 
|  | 43 | "-l", | 
|  | 44 | "--location", | 
|  | 45 | default="/tmp", | 
|  | 46 | show_default=True, | 
|  | 47 | help="Location to save logs", | 
|  | 48 | ) | 
|  | 49 | @click.option( | 
|  | 50 | "-t", | 
|  | 51 | "--type", | 
|  | 52 | help=( | 
|  | 53 | "OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU," | 
|  | 54 | " SLES, AIX" | 
|  | 55 | ), | 
|  | 56 | ) | 
|  | 57 | @click.option( | 
|  | 58 | "-rp", | 
|  | 59 | "--protocol", | 
|  | 60 | default="ALL", | 
|  | 61 | show_default=True, | 
|  | 62 | help="Select protocol to communicate with remote host.", | 
|  | 63 | ) | 
|  | 64 | @click.option( | 
|  | 65 | "-e", | 
|  | 66 | "--env_vars", | 
|  | 67 | show_default=True, | 
|  | 68 | help="Environment variables e.g: {'var':value}", | 
|  | 69 | ) | 
|  | 70 | @click.option( | 
|  | 71 | "-ec", | 
|  | 72 | "--econfig", | 
|  | 73 | show_default=True, | 
|  | 74 | help="Predefine environment variables, refer en_vars_template.yaml ", | 
|  | 75 | ) | 
|  | 76 | @click.option( | 
|  | 77 | "--log_level", | 
|  | 78 | default="INFO", | 
|  | 79 | show_default=True, | 
|  | 80 | help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)", | 
|  | 81 | ) | 
|  | 82 | def cli_ffdc( | 
|  | 83 | remote, | 
|  | 84 | username, | 
|  | 85 | password, | 
| George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame^] | 86 | port_https, | 
|  | 87 | port_ipmi, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 88 | config, | 
|  | 89 | location, | 
|  | 90 | type, | 
|  | 91 | protocol, | 
|  | 92 | env_vars, | 
|  | 93 | econfig, | 
|  | 94 | log_level, | 
|  | 95 | ): | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 96 | r""" | 
|  | 97 | Stand alone CLI to generate and collect FFDC from the selected target. | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 98 | """ | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 99 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 100 | click.echo( | 
|  | 101 | "\n********** FFDC (First Failure Data Collection) Starts **********" | 
|  | 102 | ) | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 103 |  | 
| George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame^] | 104 | if input_options_ok( | 
|  | 105 | remote, username, password, port_https, port_ipmi, config, type | 
|  | 106 | ): | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 107 | this_ffdc = ffdc_collector( | 
|  | 108 | remote, | 
|  | 109 | username, | 
|  | 110 | password, | 
| George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame^] | 111 | port_https, | 
|  | 112 | port_ipmi, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 113 | config, | 
|  | 114 | location, | 
|  | 115 | type, | 
|  | 116 | protocol, | 
|  | 117 | env_vars, | 
|  | 118 | econfig, | 
|  | 119 | log_level, | 
|  | 120 | ) | 
| Peter D  Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 121 | this_ffdc.collect_ffdc() | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 122 |  | 
| Peter D  Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 123 | if len(os.listdir(this_ffdc.ffdc_dir_path)) == 0: | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 124 | click.echo( | 
|  | 125 | "\n\tFFDC Collection from " + remote + " has failed.\n\n" | 
|  | 126 | ) | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 127 | else: | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 128 | click.echo( | 
|  | 129 | str( | 
|  | 130 | "\n\t" | 
|  | 131 | + str(len(os.listdir(this_ffdc.ffdc_dir_path))) | 
|  | 132 | + " files were retrieved from " | 
|  | 133 | + remote | 
|  | 134 | ) | 
|  | 135 | ) | 
| Peter D  Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 136 | click.echo("\tFiles are stored in " + this_ffdc.ffdc_dir_path) | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 137 |  | 
| Peter D  Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 138 | click.echo("\tTotal elapsed time " + this_ffdc.elapsed_time + "\n\n") | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 139 | click.echo("\n********** FFDC Finishes **********\n\n") | 
|  | 140 |  | 
|  | 141 |  | 
| George Keishing | e8a4175 | 2023-06-22 21:42:47 +0530 | [diff] [blame^] | 142 | def input_options_ok( | 
|  | 143 | remote, username, password, port_https, port_ipmi, config, type | 
|  | 144 | ): | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 145 | r""" | 
|  | 146 | Verify script options exist via CLI options or environment variables. | 
|  | 147 | """ | 
|  | 148 |  | 
|  | 149 | all_options_ok = True | 
|  | 150 |  | 
|  | 151 | if not remote: | 
|  | 152 | all_options_ok = False | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 153 | print( | 
|  | 154 | "        \n\tERROR: Name/IP of the remote host is not specified in" | 
|  | 155 | " CLI options." | 
|  | 156 | ) | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 157 | if not username: | 
|  | 158 | all_options_ok = False | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 159 | print( | 
|  | 160 | "        \n\tERROR: User of the remote host is not specified in" | 
|  | 161 | " CLI options." | 
|  | 162 | ) | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 163 | if not password: | 
|  | 164 | all_options_ok = False | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 165 | print( | 
|  | 166 | "        \n\tERROR: Password of the user remote host is not" | 
|  | 167 | " specified in CLI options." | 
|  | 168 | ) | 
| George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 169 | if not type: | 
| Peter D  Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 170 | all_options_ok = False | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 171 | print( | 
|  | 172 | "        \n\tERROR: Remote host os type is not specified in CLI" | 
|  | 173 | " options." | 
|  | 174 | ) | 
| George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 175 | if not os.path.isfile(config): | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 176 | all_options_ok = False | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 177 | print( | 
|  | 178 | "        \n\tERROR: Config file %s is not found.  Please verify" | 
|  | 179 | " path and filename." % config | 
|  | 180 | ) | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 181 |  | 
|  | 182 | return all_options_ok | 
|  | 183 |  | 
|  | 184 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 185 | if __name__ == "__main__": | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 186 | cli_ffdc() |