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