blob: e753857d91a94de7943d18a10d99d8b86ed915f6 [file] [log] [blame]
Peter D Phan72ce6b82021-06-03 06:18:26 -05001#!/usr/bin/env python
2
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]))
14full_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
21from ffdc_collector import FFDCCollector
22
23
Peter D Phan8462faf2021-06-16 12:24:15 -050024@click.command(context_settings=dict(help_option_names=['-h', '--help']))
25@click.option('-r', '--remote', envvar='OPENBMC_HOST',
26 help="Name/IP of the remote (targeting) host. [default: OPENBMC_HOST]")
Peter D Phan72ce6b82021-06-03 06:18:26 -050027@click.option('-u', '--username', envvar='OPENBMC_USERNAME',
Peter D Phan8462faf2021-06-16 12:24:15 -050028 help="User on the remote host with access to FFDC files.[default: OPENBMC_USERNAME]")
Peter D Phan72ce6b82021-06-03 06:18:26 -050029@click.option('-p', '--password', envvar='OPENBMC_PASSWORD',
Peter D Phan8462faf2021-06-16 12:24:15 -050030 help="Password for user on remote host. [default: OPENBMC_PASSWORD]")
Peter D Phan0c42a942021-06-29 09:17:27 -050031@click.option('-c', '--ffdc_config', default=abs_path + "/ffdc_config.yaml",
Peter D Phan8462faf2021-06-16 12:24:15 -050032 show_default=True, help="YAML Configuration file listing commands and files for FFDC.")
Peter D Phan72ce6b82021-06-03 06:18:26 -050033@click.option('-l', '--location', default="/tmp",
34 show_default=True, help="Location to store collected FFDC data")
Peter D Phan3beb02e2021-07-06 13:25:17 -050035@click.option('-t', '--remote_type',
George Keishing5b43c222021-07-09 01:10:00 -050036 help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, SLES, AIX")
Peter D Phan0c669772021-06-24 13:52:42 -050037@click.option('-rp', '--remote_protocol', default="ALL",
38 show_default=True,
39 help="Select protocol (SSH, SCP, REDFISH) to communicate with remote host. \
40 Default: all available.")
Peter D Phane86d9a52021-07-15 10:42:25 -050041@click.option('--log_level', default="INFO",
42 show_default=True,
43 help="python logging level (CRITICAL, ERROR, WARNING, INFO, DEBUG)")
44def cli_ffdc(remote, username, password, ffdc_config, location, remote_type, remote_protocol, log_level):
Peter D Phan72ce6b82021-06-03 06:18:26 -050045 r"""
46 Stand alone CLI to generate and collect FFDC from the selected target.
Peter D Phan72ce6b82021-06-03 06:18:26 -050047 """
Peter D Phan8462faf2021-06-16 12:24:15 -050048
George Keishing772c9772021-06-16 23:23:42 -050049 click.echo("\n********** FFDC (First Failure Data Collection) Starts **********")
Peter D Phan72ce6b82021-06-03 06:18:26 -050050
Peter D Phan3beb02e2021-07-06 13:25:17 -050051 if input_options_ok(remote, username, password, ffdc_config, remote_type):
Peter D Phan0c669772021-06-24 13:52:42 -050052 thisFFDC = FFDCCollector(remote, username, password,
Peter D Phane86d9a52021-07-15 10:42:25 -050053 ffdc_config, location,
54 remote_type, remote_protocol, log_level)
Peter D Phan8462faf2021-06-16 12:24:15 -050055 thisFFDC.collect_ffdc()
Peter D Phan72ce6b82021-06-03 06:18:26 -050056
Peter D Phan56429a62021-06-23 08:38:29 -050057 if len(os.listdir(thisFFDC.ffdc_dir_path)) == 0:
Peter D Phan733df632021-06-17 13:13:36 -050058 click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n")
Peter D Phan8462faf2021-06-16 12:24:15 -050059 else:
Peter D Phan0c669772021-06-24 13:52:42 -050060 click.echo(str("\n\t" + str(len(os.listdir(thisFFDC.ffdc_dir_path)))
Peter D Phan56429a62021-06-23 08:38:29 -050061 + " files were retrieved from " + remote))
Peter D Phan7610bc42021-07-06 06:31:05 -050062 click.echo("\tFiles are stored in " + thisFFDC.ffdc_dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -050063
Peter D Phan7610bc42021-07-06 06:31:05 -050064 click.echo("\tTotal elapsed time " + thisFFDC.elapsed_time + "\n\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050065 click.echo("\n********** FFDC Finishes **********\n\n")
66
67
Peter D Phan3beb02e2021-07-06 13:25:17 -050068def input_options_ok(remote, username, password, ffdc_config, remote_type):
Peter D Phan8462faf2021-06-16 12:24:15 -050069 r"""
70 Verify script options exist via CLI options or environment variables.
71 """
72
73 all_options_ok = True
74
75 if not remote:
76 all_options_ok = False
77 print("\
78 \n>>>>>\tERROR: Name/IP of the remote host is not specified in CLI options or env OPENBMC_HOST.")
79 if not username:
80 all_options_ok = False
81 print("\
82 \n>>>>>\tERROR: User on the remote host is not specified in CLI options or env OPENBMC_USERNAME.")
83 if not password:
84 all_options_ok = False
85 print("\
86 \n>>>>>\tERROR: Password for user on remote host is not specified in CLI options "
87 + "or env OPENBMC_PASSWORD.")
Peter D Phan3beb02e2021-07-06 13:25:17 -050088 if not remote_type:
89 all_options_ok = False
90 print("\
91 \n>>>>>\tERROR: Remote host os type is not specified in CLI options.")
Peter D Phan8462faf2021-06-16 12:24:15 -050092 if not os.path.isfile(ffdc_config):
93 all_options_ok = False
94 print("\
95 \n>>>>>\tERROR: Config file %s is not found. Please verify path and filename." % ffdc_config)
96
97 return all_options_ok
98
99
Peter D Phan72ce6b82021-06-03 06:18:26 -0500100if __name__ == '__main__':
101 cli_ffdc()