blob: ca232f132e19ddb98a874dca243718d62636855a [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
Sridevi Ramesh47375aa2022-12-08 05:05:31 -06007from ffdc_collector import ffdc_collector
8
Peter D Phan72ce6b82021-06-03 06:18:26 -05009import os
10import sys
11import click
12
13# ---------Set sys.path for cli command execution---------------------------------------
14# Absolute path to openbmc-test-automation/ffdc
Peter D Phan8462faf2021-06-16 12:24:15 -050015abs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
16full_path = abs_path.split('ffdc')[0]
Sridevi Ramesh47375aa2022-12-08 05:05:31 -060017
Peter D Phan72ce6b82021-06-03 06:18:26 -050018sys.path.append(full_path)
19# Walk path and append to sys.path
20for 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 Phan72ce6b82021-06-03 06:18:26 -050024
Peter D Phan8462faf2021-06-16 12:24:15 -050025@click.command(context_settings=dict(help_option_names=['-h', '--help']))
George Keishinge33094d2021-07-22 12:59:03 -050026@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 Phan72ce6b82021-06-03 06:18:26 -050034@click.option('-l', '--location', default="/tmp",
George Keishinge33094d2021-07-22 12:59:03 -050035 show_default=True, help="Location to save logs")
36@click.option('-t', '--type',
George Keishing5b43c222021-07-09 01:10:00 -050037 help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, SLES, AIX")
George Keishinge33094d2021-07-22 12:59:03 -050038@click.option('-rp', '--protocol', default="ALL",
Peter D Phan0c669772021-06-24 13:52:42 -050039 show_default=True,
George Keishinge33094d2021-07-22 12:59:03 -050040 help="Select protocol to communicate with remote host.")
George Keishing4885b2f2021-07-21 15:22:45 -050041@click.option('-e', '--env_vars', show_default=True,
George Keishinge33094d2021-07-22 12:59:03 -050042 help="Environment variables e.g: {'var':value}")
George Keishing8e94f8c2021-07-23 15:06:32 -050043@click.option('-ec', '--econfig', show_default=True,
44 help="Predefine environment variables, refer en_vars_template.yaml ")
Peter D Phane86d9a52021-07-15 10:42:25 -050045@click.option('--log_level', default="INFO",
46 show_default=True,
George Keishinge33094d2021-07-22 12:59:03 -050047 help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)")
George Keishing8e94f8c2021-07-23 15:06:32 -050048def 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 Phan72ce6b82021-06-03 06:18:26 -050058 r"""
59 Stand alone CLI to generate and collect FFDC from the selected target.
Peter D Phan72ce6b82021-06-03 06:18:26 -050060 """
Peter D Phan8462faf2021-06-16 12:24:15 -050061
George Keishing772c9772021-06-16 23:23:42 -050062 click.echo("\n********** FFDC (First Failure Data Collection) Starts **********")
Peter D Phan72ce6b82021-06-03 06:18:26 -050063
George Keishinge33094d2021-07-22 12:59:03 -050064 if input_options_ok(remote, username, password, config, type):
Peter D Phan5e56f522021-12-20 13:19:41 -060065 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 Phan72ce6b82021-06-03 06:18:26 -050076
Peter D Phan5e56f522021-12-20 13:19:41 -060077 if len(os.listdir(this_ffdc.ffdc_dir_path)) == 0:
Peter D Phan733df632021-06-17 13:13:36 -050078 click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n")
Peter D Phan8462faf2021-06-16 12:24:15 -050079 else:
Peter D Phan5e56f522021-12-20 13:19:41 -060080 click.echo(str("\n\t" + str(len(os.listdir(this_ffdc.ffdc_dir_path)))
George Keishing4885b2f2021-07-21 15:22:45 -050081 + " files were retrieved from " + remote))
Peter D Phan5e56f522021-12-20 13:19:41 -060082 click.echo("\tFiles are stored in " + this_ffdc.ffdc_dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -050083
Peter D Phan5e56f522021-12-20 13:19:41 -060084 click.echo("\tTotal elapsed time " + this_ffdc.elapsed_time + "\n\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050085 click.echo("\n********** FFDC Finishes **********\n\n")
86
87
George Keishinge33094d2021-07-22 12:59:03 -050088def input_options_ok(remote, username, password, config, type):
Peter D Phan8462faf2021-06-16 12:24:15 -050089 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 Keishing8e94f8c2021-07-23 15:06:32 -050098 \n\tERROR: Name/IP of the remote host is not specified in CLI options.")
Peter D Phan8462faf2021-06-16 12:24:15 -050099 if not username:
100 all_options_ok = False
101 print("\
George Keishing8e94f8c2021-07-23 15:06:32 -0500102 \n\tERROR: User of the remote host is not specified in CLI options.")
Peter D Phan8462faf2021-06-16 12:24:15 -0500103 if not password:
104 all_options_ok = False
105 print("\
George Keishing8e94f8c2021-07-23 15:06:32 -0500106 \n\tERROR: Password of the user remote host is not specified in CLI options.")
George Keishinge33094d2021-07-22 12:59:03 -0500107 if not type:
Peter D Phan3beb02e2021-07-06 13:25:17 -0500108 all_options_ok = False
109 print("\
George Keishing7bf55092021-07-22 12:33:34 -0500110 \n\tERROR: Remote host os type is not specified in CLI options.")
George Keishinge33094d2021-07-22 12:59:03 -0500111 if not os.path.isfile(config):
Peter D Phan8462faf2021-06-16 12:24:15 -0500112 all_options_ok = False
113 print("\
George Keishinge33094d2021-07-22 12:59:03 -0500114 \n\tERROR: Config file %s is not found. Please verify path and filename." % config)
Peter D Phan8462faf2021-06-16 12:24:15 -0500115
116 return all_options_ok
117
118
Peter D Phan72ce6b82021-06-03 06:18:26 -0500119if __name__ == '__main__':
120 cli_ffdc()