blob: d52b1ecb74e03b306eff2af505a9605af10fa330 [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']))
George Keishinge33094d2021-07-22 12:59:03 -050025@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 Phan72ce6b82021-06-03 06:18:26 -050033@click.option('-l', '--location', default="/tmp",
George Keishinge33094d2021-07-22 12:59:03 -050034 show_default=True, help="Location to save logs")
35@click.option('-t', '--type',
George Keishing5b43c222021-07-09 01:10:00 -050036 help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, SLES, AIX")
George Keishinge33094d2021-07-22 12:59:03 -050037@click.option('-rp', '--protocol', default="ALL",
Peter D Phan0c669772021-06-24 13:52:42 -050038 show_default=True,
George Keishinge33094d2021-07-22 12:59:03 -050039 help="Select protocol to communicate with remote host.")
George Keishing4885b2f2021-07-21 15:22:45 -050040@click.option('-e', '--env_vars', show_default=True,
George Keishinge33094d2021-07-22 12:59:03 -050041 help="Environment variables e.g: {'var':value}")
Peter D Phane86d9a52021-07-15 10:42:25 -050042@click.option('--log_level', default="INFO",
43 show_default=True,
George Keishinge33094d2021-07-22 12:59:03 -050044 help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)")
George Keishing4885b2f2021-07-21 15:22:45 -050045def cli_ffdc(remote, username, password,
George Keishinge33094d2021-07-22 12:59:03 -050046 config, location, type,
47 protocol, env_vars, log_level):
Peter D Phan72ce6b82021-06-03 06:18:26 -050048 r"""
49 Stand alone CLI to generate and collect FFDC from the selected target.
Peter D Phan72ce6b82021-06-03 06:18:26 -050050 """
Peter D Phan8462faf2021-06-16 12:24:15 -050051
George Keishing772c9772021-06-16 23:23:42 -050052 click.echo("\n********** FFDC (First Failure Data Collection) Starts **********")
Peter D Phan72ce6b82021-06-03 06:18:26 -050053
George Keishinge33094d2021-07-22 12:59:03 -050054 if input_options_ok(remote, username, password, config, type):
Peter D Phan0c669772021-06-24 13:52:42 -050055 thisFFDC = FFDCCollector(remote, username, password,
George Keishinge33094d2021-07-22 12:59:03 -050056 config, location, type,
57 protocol, env_vars, log_level)
Peter D Phan8462faf2021-06-16 12:24:15 -050058 thisFFDC.collect_ffdc()
Peter D Phan72ce6b82021-06-03 06:18:26 -050059
Peter D Phan56429a62021-06-23 08:38:29 -050060 if len(os.listdir(thisFFDC.ffdc_dir_path)) == 0:
Peter D Phan733df632021-06-17 13:13:36 -050061 click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n")
Peter D Phan8462faf2021-06-16 12:24:15 -050062 else:
Peter D Phan0c669772021-06-24 13:52:42 -050063 click.echo(str("\n\t" + str(len(os.listdir(thisFFDC.ffdc_dir_path)))
George Keishing4885b2f2021-07-21 15:22:45 -050064 + " files were retrieved from " + remote))
Peter D Phan7610bc42021-07-06 06:31:05 -050065 click.echo("\tFiles are stored in " + thisFFDC.ffdc_dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -050066
Peter D Phan7610bc42021-07-06 06:31:05 -050067 click.echo("\tTotal elapsed time " + thisFFDC.elapsed_time + "\n\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050068 click.echo("\n********** FFDC Finishes **********\n\n")
69
70
George Keishinge33094d2021-07-22 12:59:03 -050071def input_options_ok(remote, username, password, config, type):
Peter D Phan8462faf2021-06-16 12:24:15 -050072 r"""
73 Verify script options exist via CLI options or environment variables.
74 """
75
76 all_options_ok = True
77
78 if not remote:
79 all_options_ok = False
80 print("\
George Keishing7bf55092021-07-22 12:33:34 -050081 \n\tERROR: Name/IP of the remote host is not specified in CLI options or env OPENBMC_HOST.")
Peter D Phan8462faf2021-06-16 12:24:15 -050082 if not username:
83 all_options_ok = False
84 print("\
George Keishing7bf55092021-07-22 12:33:34 -050085 \n\tERROR: User on the remote host is not specified in CLI options or env OPENBMC_USERNAME.")
Peter D Phan8462faf2021-06-16 12:24:15 -050086 if not password:
87 all_options_ok = False
88 print("\
George Keishing7bf55092021-07-22 12:33:34 -050089 \n\tERROR: Password for user on remote host is not specified in CLI options "
Peter D Phan8462faf2021-06-16 12:24:15 -050090 + "or env OPENBMC_PASSWORD.")
George Keishinge33094d2021-07-22 12:59:03 -050091 if not type:
Peter D Phan3beb02e2021-07-06 13:25:17 -050092 all_options_ok = False
93 print("\
George Keishing7bf55092021-07-22 12:33:34 -050094 \n\tERROR: Remote host os type is not specified in CLI options.")
George Keishinge33094d2021-07-22 12:59:03 -050095 if not os.path.isfile(config):
Peter D Phan8462faf2021-06-16 12:24:15 -050096 all_options_ok = False
97 print("\
George Keishinge33094d2021-07-22 12:59:03 -050098 \n\tERROR: Config file %s is not found. Please verify path and filename." % config)
Peter D Phan8462faf2021-06-16 12:24:15 -050099
100 return all_options_ok
101
102
Peter D Phan72ce6b82021-06-03 06:18:26 -0500103if __name__ == '__main__':
104 cli_ffdc()