blob: d709a96651ad80b7a53d0069cd7a9931c4cb71cc [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Peter D Phan72ce6b82021-06-03 06:18:26 -05002
3r"""
Peter D Phan72ce6b82021-06-03 06:18:26 -05004CLI FFDC Collector.
5"""
6
7import os
8import sys
9import click
10
11# ---------Set sys.path for cli command execution---------------------------------------
12# Absolute path to openbmc-test-automation/ffdc
Peter D Phan8462faf2021-06-16 12:24:15 -050013abs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
George Keishinge635ddc2022-12-08 07:38:02 -060014full_path = abs_path.split('ffdc')[0]
Peter D Phan72ce6b82021-06-03 06:18:26 -050015sys.path.append(full_path)
16# Walk path and append to sys.path
17for 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
George Keishing09679892022-12-08 08:21:52 -060021from ffdc_collector import ffdc_collector # NOQA
George Keishing37c58c82022-12-08 07:42:54 -060022
Peter D Phan72ce6b82021-06-03 06:18:26 -050023
George Keishinge635ddc2022-12-08 07:38:02 -060024@click.command(context_settings=dict(help_option_names=['-h', '--help']))
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.")
33@click.option('-l', '--location', default="/tmp",
34 show_default=True, help="Location to save logs")
35@click.option('-t', '--type',
36 help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, SLES, AIX")
37@click.option('-rp', '--protocol', default="ALL",
38 show_default=True,
39 help="Select protocol to communicate with remote host.")
40@click.option('-e', '--env_vars', show_default=True,
41 help="Environment variables e.g: {'var':value}")
42@click.option('-ec', '--econfig', show_default=True,
43 help="Predefine environment variables, refer en_vars_template.yaml ")
44@click.option('--log_level', default="INFO",
45 show_default=True,
46 help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)")
47def 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 Phan72ce6b82021-06-03 06:18:26 -050057 r"""
58 Stand alone CLI to generate and collect FFDC from the selected target.
Peter D Phan72ce6b82021-06-03 06:18:26 -050059 """
Peter D Phan8462faf2021-06-16 12:24:15 -050060
George Keishinge635ddc2022-12-08 07:38:02 -060061 click.echo("\n********** FFDC (First Failure Data Collection) Starts **********")
Peter D Phan72ce6b82021-06-03 06:18:26 -050062
George Keishinge33094d2021-07-22 12:59:03 -050063 if input_options_ok(remote, username, password, config, type):
George Keishinge635ddc2022-12-08 07:38:02 -060064 this_ffdc = ffdc_collector(remote,
65 username,
66 password,
67 config,
68 location,
69 type,
70 protocol,
71 env_vars,
72 econfig,
73 log_level)
Peter D Phan5e56f522021-12-20 13:19:41 -060074 this_ffdc.collect_ffdc()
Peter D Phan72ce6b82021-06-03 06:18:26 -050075
Peter D Phan5e56f522021-12-20 13:19:41 -060076 if len(os.listdir(this_ffdc.ffdc_dir_path)) == 0:
George Keishinge635ddc2022-12-08 07:38:02 -060077 click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n")
Peter D Phan8462faf2021-06-16 12:24:15 -050078 else:
George Keishinge635ddc2022-12-08 07:38:02 -060079 click.echo(str("\n\t" + str(len(os.listdir(this_ffdc.ffdc_dir_path)))
80 + " files were retrieved from " + remote))
Peter D Phan5e56f522021-12-20 13:19:41 -060081 click.echo("\tFiles are stored in " + this_ffdc.ffdc_dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -050082
Peter D Phan5e56f522021-12-20 13:19:41 -060083 click.echo("\tTotal elapsed time " + this_ffdc.elapsed_time + "\n\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050084 click.echo("\n********** FFDC Finishes **********\n\n")
85
86
George Keishinge33094d2021-07-22 12:59:03 -050087def input_options_ok(remote, username, password, config, type):
Peter D Phan8462faf2021-06-16 12:24:15 -050088 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
George Keishinge635ddc2022-12-08 07:38:02 -060096 print("\
97 \n\tERROR: Name/IP of the remote host is not specified in CLI options.")
Peter D Phan8462faf2021-06-16 12:24:15 -050098 if not username:
99 all_options_ok = False
George Keishinge635ddc2022-12-08 07:38:02 -0600100 print("\
101 \n\tERROR: User of the remote host is not specified in CLI options.")
Peter D Phan8462faf2021-06-16 12:24:15 -0500102 if not password:
103 all_options_ok = False
George Keishinge635ddc2022-12-08 07:38:02 -0600104 print("\
105 \n\tERROR: Password of the user remote host is not specified in CLI options.")
George Keishinge33094d2021-07-22 12:59:03 -0500106 if not type:
Peter D Phan3beb02e2021-07-06 13:25:17 -0500107 all_options_ok = False
George Keishinge635ddc2022-12-08 07:38:02 -0600108 print("\
109 \n\tERROR: Remote host os type is not specified in CLI options.")
George Keishinge33094d2021-07-22 12:59:03 -0500110 if not os.path.isfile(config):
Peter D Phan8462faf2021-06-16 12:24:15 -0500111 all_options_ok = False
George Keishinge635ddc2022-12-08 07:38:02 -0600112 print("\
113 \n\tERROR: Config file %s is not found. Please verify path and filename." % config)
Peter D Phan8462faf2021-06-16 12:24:15 -0500114
115 return all_options_ok
116
117
George Keishinge635ddc2022-12-08 07:38:02 -0600118if __name__ == '__main__':
Peter D Phan72ce6b82021-06-03 06:18:26 -0500119 cli_ffdc()