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 | |
Sridevi Ramesh | 47375aa | 2022-12-08 05:05:31 -0600 | [diff] [blame] | 7 | from ffdc_collector import ffdc_collector |
| 8 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 9 | import os |
| 10 | import sys |
| 11 | import click |
| 12 | |
| 13 | # ---------Set sys.path for cli command execution--------------------------------------- |
| 14 | # Absolute path to openbmc-test-automation/ffdc |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 15 | abs_path = os.path.abspath(os.path.dirname(sys.argv[0])) |
| 16 | full_path = abs_path.split('ffdc')[0] |
Sridevi Ramesh | 47375aa | 2022-12-08 05:05:31 -0600 | [diff] [blame] | 17 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 18 | sys.path.append(full_path) |
| 19 | # Walk path and append to sys.path |
| 20 | for root, dirs, files in os.walk(full_path): |
| 21 | for found_dir in dirs: |
| 22 | sys.path.append(os.path.join(root, found_dir)) |
| 23 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 24 | |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 25 | @click.command(context_settings=dict(help_option_names=['-h', '--help'])) |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 26 | @click.option('-r', '--remote', |
| 27 | help="Hostname/IP of the remote host") |
| 28 | @click.option('-u', '--username', |
| 29 | help="Username of the remote host.") |
| 30 | @click.option('-p', '--password', |
| 31 | help="Password of the remote host.") |
| 32 | @click.option('-c', '--config', default=abs_path + "/ffdc_config.yaml", |
| 33 | show_default=True, help="YAML Configuration file for log collection.") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 34 | @click.option('-l', '--location', default="/tmp", |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 35 | show_default=True, help="Location to save logs") |
| 36 | @click.option('-t', '--type', |
George Keishing | 5b43c22 | 2021-07-09 01:10:00 -0500 | [diff] [blame] | 37 | help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, SLES, AIX") |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 38 | @click.option('-rp', '--protocol', default="ALL", |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 39 | show_default=True, |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 40 | help="Select protocol to communicate with remote host.") |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 41 | @click.option('-e', '--env_vars', show_default=True, |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 42 | help="Environment variables e.g: {'var':value}") |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 43 | @click.option('-ec', '--econfig', show_default=True, |
| 44 | help="Predefine environment variables, refer en_vars_template.yaml ") |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 45 | @click.option('--log_level', default="INFO", |
| 46 | show_default=True, |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 47 | help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)") |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 48 | def cli_ffdc(remote, |
| 49 | username, |
| 50 | password, |
| 51 | config, |
| 52 | location, |
| 53 | type, |
| 54 | protocol, |
| 55 | env_vars, |
| 56 | econfig, |
| 57 | log_level): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 58 | r""" |
| 59 | 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] | 60 | """ |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 61 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 62 | click.echo("\n********** FFDC (First Failure Data Collection) Starts **********") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 63 | |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 64 | if input_options_ok(remote, username, password, config, type): |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 65 | this_ffdc = ffdc_collector(remote, |
| 66 | username, |
| 67 | password, |
| 68 | config, |
| 69 | location, |
| 70 | type, |
| 71 | protocol, |
| 72 | env_vars, |
| 73 | econfig, |
| 74 | log_level) |
| 75 | this_ffdc.collect_ffdc() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 76 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 77 | if len(os.listdir(this_ffdc.ffdc_dir_path)) == 0: |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 78 | click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n") |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 79 | else: |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 80 | click.echo(str("\n\t" + str(len(os.listdir(this_ffdc.ffdc_dir_path))) |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 81 | + " files were retrieved from " + remote)) |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 82 | click.echo("\tFiles are stored in " + this_ffdc.ffdc_dir_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 83 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 84 | click.echo("\tTotal elapsed time " + this_ffdc.elapsed_time + "\n\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 85 | click.echo("\n********** FFDC Finishes **********\n\n") |
| 86 | |
| 87 | |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 88 | def input_options_ok(remote, username, password, config, type): |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 89 | r""" |
| 90 | Verify script options exist via CLI options or environment variables. |
| 91 | """ |
| 92 | |
| 93 | all_options_ok = True |
| 94 | |
| 95 | if not remote: |
| 96 | all_options_ok = False |
| 97 | print("\ |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 98 | \n\tERROR: Name/IP of the remote host is not specified in CLI options.") |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 99 | if not username: |
| 100 | all_options_ok = False |
| 101 | print("\ |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 102 | \n\tERROR: User of the remote host is not specified in CLI options.") |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 103 | if not password: |
| 104 | all_options_ok = False |
| 105 | print("\ |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 106 | \n\tERROR: Password of the user remote host is not specified in CLI options.") |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 107 | if not type: |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 108 | all_options_ok = False |
| 109 | print("\ |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 110 | \n\tERROR: Remote host os type is not specified in CLI options.") |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 111 | if not os.path.isfile(config): |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 112 | all_options_ok = False |
| 113 | print("\ |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 114 | \n\tERROR: Config file %s is not found. Please verify path and filename." % config) |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 115 | |
| 116 | return all_options_ok |
| 117 | |
| 118 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 119 | if __name__ == '__main__': |
| 120 | cli_ffdc() |