blob: 698b87b61b73a531943ae8cc314e04b838445980 [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
Patrick Williams20f38712022-12-08 06:18:26 -06009
Peter D Phan72ce6b82021-06-03 06:18:26 -050010import click
11
12# ---------Set sys.path for cli command execution---------------------------------------
13# Absolute path to openbmc-test-automation/ffdc
Peter D Phan8462faf2021-06-16 12:24:15 -050014abs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
Patrick Williams20f38712022-12-08 06:18:26 -060015full_path = abs_path.split("ffdc")[0]
Peter D Phan72ce6b82021-06-03 06:18:26 -050016sys.path.append(full_path)
17# Walk path and append to sys.path
18for root, dirs, files in os.walk(full_path):
19 for found_dir in dirs:
20 sys.path.append(os.path.join(root, found_dir))
21
George Keishing09679892022-12-08 08:21:52 -060022from ffdc_collector import ffdc_collector # NOQA
George Keishing37c58c82022-12-08 07:42:54 -060023
Peter D Phan72ce6b82021-06-03 06:18:26 -050024
Patrick Williams20f38712022-12-08 06:18:26 -060025@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
26@click.option("-r", "--remote", help="Hostname/IP of the remote host")
27@click.option("-u", "--username", help="Username of the remote host.")
28@click.option("-p", "--password", help="Password of the remote host.")
29@click.option(
George Keishinge8a41752023-06-22 21:42:47 +053030 "-port_https", default=443, show_default=True, help="HTTPS port value."
31)
32@click.option(
33 "-port_ipmi", default=623, show_default=True, help="IPMI port value."
34)
35@click.option(
Patrick Williams20f38712022-12-08 06:18:26 -060036 "-c",
37 "--config",
38 default=abs_path + "/ffdc_config.yaml",
39 show_default=True,
40 help="YAML Configuration file for log collection.",
41)
42@click.option(
43 "-l",
44 "--location",
45 default="/tmp",
46 show_default=True,
47 help="Location to save logs",
48)
49@click.option(
50 "-t",
51 "--type",
52 help=(
53 "OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU,"
54 " SLES, AIX"
55 ),
56)
57@click.option(
58 "-rp",
59 "--protocol",
60 default="ALL",
61 show_default=True,
62 help="Select protocol to communicate with remote host.",
63)
64@click.option(
65 "-e",
66 "--env_vars",
67 show_default=True,
68 help="Environment variables e.g: {'var':value}",
69)
70@click.option(
71 "-ec",
72 "--econfig",
73 show_default=True,
74 help="Predefine environment variables, refer en_vars_template.yaml ",
75)
76@click.option(
77 "--log_level",
78 default="INFO",
79 show_default=True,
80 help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)",
81)
82def cli_ffdc(
83 remote,
84 username,
85 password,
George Keishinge8a41752023-06-22 21:42:47 +053086 port_https,
87 port_ipmi,
Patrick Williams20f38712022-12-08 06:18:26 -060088 config,
89 location,
90 type,
91 protocol,
92 env_vars,
93 econfig,
94 log_level,
95):
Peter D Phan72ce6b82021-06-03 06:18:26 -050096 r"""
97 Stand alone CLI to generate and collect FFDC from the selected target.
Peter D Phan72ce6b82021-06-03 06:18:26 -050098 """
Peter D Phan8462faf2021-06-16 12:24:15 -050099
Patrick Williams20f38712022-12-08 06:18:26 -0600100 click.echo(
101 "\n********** FFDC (First Failure Data Collection) Starts **********"
102 )
Peter D Phan72ce6b82021-06-03 06:18:26 -0500103
George Keishinge8a41752023-06-22 21:42:47 +0530104 if input_options_ok(
105 remote, username, password, port_https, port_ipmi, config, type
106 ):
Patrick Williams20f38712022-12-08 06:18:26 -0600107 this_ffdc = ffdc_collector(
108 remote,
109 username,
110 password,
George Keishinge8a41752023-06-22 21:42:47 +0530111 port_https,
112 port_ipmi,
Patrick Williams20f38712022-12-08 06:18:26 -0600113 config,
114 location,
115 type,
116 protocol,
117 env_vars,
118 econfig,
119 log_level,
120 )
Peter D Phan5e56f522021-12-20 13:19:41 -0600121 this_ffdc.collect_ffdc()
Peter D Phan72ce6b82021-06-03 06:18:26 -0500122
Peter D Phan5e56f522021-12-20 13:19:41 -0600123 if len(os.listdir(this_ffdc.ffdc_dir_path)) == 0:
Patrick Williams20f38712022-12-08 06:18:26 -0600124 click.echo(
125 "\n\tFFDC Collection from " + remote + " has failed.\n\n"
126 )
Peter D Phan8462faf2021-06-16 12:24:15 -0500127 else:
Patrick Williams20f38712022-12-08 06:18:26 -0600128 click.echo(
129 str(
130 "\n\t"
131 + str(len(os.listdir(this_ffdc.ffdc_dir_path)))
132 + " files were retrieved from "
133 + remote
134 )
135 )
Peter D Phan5e56f522021-12-20 13:19:41 -0600136 click.echo("\tFiles are stored in " + this_ffdc.ffdc_dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500137
Peter D Phan5e56f522021-12-20 13:19:41 -0600138 click.echo("\tTotal elapsed time " + this_ffdc.elapsed_time + "\n\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500139 click.echo("\n********** FFDC Finishes **********\n\n")
140
141
George Keishinge8a41752023-06-22 21:42:47 +0530142def input_options_ok(
143 remote, username, password, port_https, port_ipmi, config, type
144):
Peter D Phan8462faf2021-06-16 12:24:15 -0500145 r"""
146 Verify script options exist via CLI options or environment variables.
147 """
148
149 all_options_ok = True
150
151 if not remote:
152 all_options_ok = False
Patrick Williams20f38712022-12-08 06:18:26 -0600153 print(
154 " \n\tERROR: Name/IP of the remote host is not specified in"
155 " CLI options."
156 )
Peter D Phan8462faf2021-06-16 12:24:15 -0500157 if not username:
158 all_options_ok = False
Patrick Williams20f38712022-12-08 06:18:26 -0600159 print(
160 " \n\tERROR: User of the remote host is not specified in"
161 " CLI options."
162 )
Peter D Phan8462faf2021-06-16 12:24:15 -0500163 if not password:
164 all_options_ok = False
Patrick Williams20f38712022-12-08 06:18:26 -0600165 print(
166 " \n\tERROR: Password of the user remote host is not"
167 " specified in CLI options."
168 )
George Keishinge33094d2021-07-22 12:59:03 -0500169 if not type:
Peter D Phan3beb02e2021-07-06 13:25:17 -0500170 all_options_ok = False
Patrick Williams20f38712022-12-08 06:18:26 -0600171 print(
172 " \n\tERROR: Remote host os type is not specified in CLI"
173 " options."
174 )
George Keishinge33094d2021-07-22 12:59:03 -0500175 if not os.path.isfile(config):
Peter D Phan8462faf2021-06-16 12:24:15 -0500176 all_options_ok = False
Patrick Williams20f38712022-12-08 06:18:26 -0600177 print(
178 " \n\tERROR: Config file %s is not found. Please verify"
179 " path and filename." % config
180 )
Peter D Phan8462faf2021-06-16 12:24:15 -0500181
182 return all_options_ok
183
184
Patrick Williams20f38712022-12-08 06:18:26 -0600185if __name__ == "__main__":
Peter D Phan72ce6b82021-06-03 06:18:26 -0500186 cli_ffdc()