blob: b5efa7a6706c8230a7266d2630393492816d3313 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walsh769c2a12016-12-13 15:45:17 -06002
3r"""
4This module is the python counterpart to openbmc_ffdc.robot..
5"""
6
7import os
8
Michael Walshe4a093d2017-10-30 15:03:18 -05009import gen_print as gp
George Keishinge635ddc2022-12-08 07:38:02 -060010import gen_robot_keyword as grk
Patrick Williams20f38712022-12-08 06:18:26 -060011import gen_valid as gv
Michael Walshe4a093d2017-10-30 15:03:18 -050012import state as st
Michael Walsh769c2a12016-12-13 15:45:17 -060013from robot.libraries.BuiltIn import BuiltIn
14
Patrick Williams20f38712022-12-08 06:18:26 -060015redfish_support_trans_state = int(
16 os.environ.get("REDFISH_SUPPORT_TRANS_STATE", 0)
17) or int(
18 BuiltIn().get_variable_value("${REDFISH_SUPPORT_TRANS_STATE}", default=0)
19)
Michael Shepose1775172021-02-11 16:59:08 -060020
Michael Walsh769c2a12016-12-13 15:45:17 -060021
Patrick Williams20f38712022-12-08 06:18:26 -060022def ffdc(
23 ffdc_dir_path=None,
24 ffdc_prefix=None,
25 ffdc_function_list="",
26 comm_check=True,
27):
Michael Walsh769c2a12016-12-13 15:45:17 -060028 r"""
29 Gather First Failure Data Capture (FFDC).
30
31 This includes:
32 - Set global FFDC_TIME.
33 - Create FFDC work space directory.
34 - Write test info details.
35 - Call BMC methods to write/collect FFDC data.
36
37 Description of arguments:
Michael Walshda8b3942020-03-23 10:39:52 -050038 ffdc_dir_path The dir path where FFDC data should be put.
39 ffdc_prefix The prefix to be given to each FFDC file name generated.
40 ffdc_function_list A colon-delimited list of all the types of FFDC data you wish to have
41 collected. A blank value means that all possible kinds of FFDC are to be
42 collected. See FFDC_METHOD_CALL object in lib/openbmc_ffdc_list.py for
43 possible choices.
44 comm_check Do a communications check prior to collecting FFDC. If commincation to
45 the BMC can't be established, abort the FFDC collection.
Michael Walsh769c2a12016-12-13 15:45:17 -060046 """
47
Michael Walshe4a093d2017-10-30 15:03:18 -050048 ffdc_file_list = []
49
George Keishing3f7f12c2017-03-02 06:14:41 -060050 # Check if Ping and SSH connection is alive
51 OPENBMC_HOST = BuiltIn().get_variable_value("${OPENBMC_HOST}")
George Keishing3f7f12c2017-03-02 06:14:41 -060052
Michael Walshda8b3942020-03-23 10:39:52 -050053 if comm_check:
Michael Shepose1775172021-02-11 16:59:08 -060054 if not redfish_support_trans_state:
Patrick Williams20f38712022-12-08 06:18:26 -060055 interface = "rest"
Michael Shepose1775172021-02-11 16:59:08 -060056 else:
Patrick Williams20f38712022-12-08 06:18:26 -060057 interface = "redfish"
Michael Shepose1775172021-02-11 16:59:08 -060058
Patrick Williams20f38712022-12-08 06:18:26 -060059 state = st.get_state(req_states=["ping", "uptime", interface])
Michael Walshda8b3942020-03-23 10:39:52 -050060 gp.qprint_var(state)
Patrick Williams20f38712022-12-08 06:18:26 -060061 if not int(state["ping"]):
62 gp.print_error(
63 "BMC is not ping-able. Terminating FFDC collection.\n"
64 )
Michael Walshda8b3942020-03-23 10:39:52 -050065 return ffdc_file_list
Michael Walshe4a093d2017-10-30 15:03:18 -050066
Michael Shepose1775172021-02-11 16:59:08 -060067 if not int(state[interface]):
George Keishingb1b6f132022-02-15 01:30:29 -060068 gp.print_error("%s commands to the BMC are failing." % interface)
Michael Walsh2bfc48e2018-10-24 15:23:17 -050069
Patrick Williams20f38712022-12-08 06:18:26 -060070 if state["uptime"] == "":
George Keishingb1b6f132022-02-15 01:30:29 -060071 gp.print_error("BMC is not communicating via ssh.\n")
72
73 # If SSH and Redfish connection doesn't works, abort.
Patrick Williams20f38712022-12-08 06:18:26 -060074 if not int(state[interface]) and state["uptime"] == "":
75 gp.print_error(
76 "BMC is not communicating via ssh or Redfish. Terminating"
77 " FFDC"
78 + " collection.\n"
79 )
Michael Walshda8b3942020-03-23 10:39:52 -050080 return ffdc_file_list
Michael Walshe4a093d2017-10-30 15:03:18 -050081
82 gp.qprint_timen("Collecting FFDC.")
Michael Walsh769c2a12016-12-13 15:45:17 -060083
Michael Walsh769c2a12016-12-13 15:45:17 -060084 # Get default values for arguments.
85 ffdc_dir_path, ffdc_prefix = set_ffdc_defaults(ffdc_dir_path, ffdc_prefix)
Michael Walshe4a093d2017-10-30 15:03:18 -050086 gp.qprint_var(ffdc_dir_path)
87 gp.qprint_var(ffdc_prefix)
Michael Walsh769c2a12016-12-13 15:45:17 -060088
89 # LOG_PREFIX is used by subordinate functions.
90 LOG_PREFIX = ffdc_dir_path + ffdc_prefix
91 BuiltIn().set_global_variable("${LOG_PREFIX}", LOG_PREFIX)
92
93 cmd_buf = ["Create Directory", ffdc_dir_path]
Michael Walshedb5c942019-03-28 12:40:50 -050094 gp.qprint_issuing(cmd_buf)
Michael Walsh769c2a12016-12-13 15:45:17 -060095 status, output = BuiltIn().run_keyword_and_ignore_error(*cmd_buf)
96 if status != "PASS":
Patrick Williams20f38712022-12-08 06:18:26 -060097 error_message = gp.sprint_error_report(
98 "Create Directory failed"
99 + " with the following"
100 + " error:\n"
101 + output
102 )
Michael Walsh769c2a12016-12-13 15:45:17 -0600103 BuiltIn().fail(error_message)
104
105 # FFDC_FILE_PATH is used by Header Message.
106 FFDC_FILE_PATH = ffdc_dir_path + ffdc_prefix + "BMC_general.txt"
107 BuiltIn().set_global_variable("${FFDC_FILE_PATH}", FFDC_FILE_PATH)
108
Michael Walshfdde2582019-04-18 11:05:11 -0500109 status, ffdc_file_list = grk.run_key_u("Header Message")
Patrick Williams20f38712022-12-08 06:18:26 -0600110 status, ffdc_file_sub_list = grk.run_key_u(
111 "Call FFDC Methods ffdc_function_list=" + ffdc_function_list
112 )
Michael Walsh769c2a12016-12-13 15:45:17 -0600113
Michael Walshe4a093d2017-10-30 15:03:18 -0500114 # Combine lists, remove duplicates and sort.
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500115 ffdc_file_list = sorted(set(ffdc_file_list + ffdc_file_sub_list))
Michael Walsh769c2a12016-12-13 15:45:17 -0600116
Michael Walshe4a093d2017-10-30 15:03:18 -0500117 gp.qprint_timen("Finished collecting FFDC.")
118
119 return ffdc_file_list
Michael Walsh769c2a12016-12-13 15:45:17 -0600120
Michael Walsh769c2a12016-12-13 15:45:17 -0600121
Patrick Williams20f38712022-12-08 06:18:26 -0600122def set_ffdc_defaults(ffdc_dir_path=None, ffdc_prefix=None):
Michael Walsh769c2a12016-12-13 15:45:17 -0600123 r"""
124 Set a default value for ffdc_dir_path and ffdc_prefix if they don't
125 already have values. Return both values.
126
127 Description of arguments:
128 ffdc_dir_path The dir path where FFDC data should be put.
129 ffdc_prefix The prefix to be given to each FFDC file name generated.
130
131 NOTE: If global variable ffdc_dir_path_style is set to ${1}, this function
132 will create default values in a newer way. Otherwise, its behavior
133 will remain unchanged.
134 """
135
Michael Walshfa9f70f2017-04-21 16:00:18 -0500136 # Note: Several subordinate functions like 'Get Test Dir and Name' and
137 # 'Header Message' expect global variable FFDC_TIME to be set.
138 cmd_buf = ["Get Current Time Stamp"]
Michael Walshedb5c942019-03-28 12:40:50 -0500139 gp.dprint_issuing(cmd_buf)
Michael Walshfa9f70f2017-04-21 16:00:18 -0500140 FFDC_TIME = BuiltIn().run_keyword(*cmd_buf)
141 BuiltIn().set_global_variable("${FFDC_TIME}", FFDC_TIME)
142
Michael Walsha0364ae2017-01-10 11:24:42 -0600143 ffdc_dir_path_style = BuiltIn().get_variable_value(
Patrick Williams20f38712022-12-08 06:18:26 -0600144 "${ffdc_dir_path_style}"
145 )
Michael Walsh769c2a12016-12-13 15:45:17 -0600146
147 if ffdc_dir_path is None:
148 if ffdc_dir_path_style:
149 try:
Patrick Williams20f38712022-12-08 06:18:26 -0600150 ffdc_dir_path = os.environ["FFDC_DIR_PATH"]
Michael Walsh769c2a12016-12-13 15:45:17 -0600151 except KeyError:
Patrick Williams20f38712022-12-08 06:18:26 -0600152 ffdc_dir_path = (
153 os.path.dirname(
154 BuiltIn().get_variable_value("${LOG_FILE}")
155 )
156 + "/"
157 )
Michael Walsh769c2a12016-12-13 15:45:17 -0600158 else:
George Keishing58a13f52017-06-21 14:04:42 -0500159 FFDC_LOG_PATH = os.getcwd() + "/logs/"
Michael Walsh769c2a12016-12-13 15:45:17 -0600160 if FFDC_LOG_PATH is None:
161 FFDC_LOG_PATH = ""
162 if FFDC_LOG_PATH == "":
Patrick Williams20f38712022-12-08 06:18:26 -0600163 FFDC_LOG_PATH = (
164 os.path.dirname(
165 BuiltIn().get_variable_value("${LOG_FILE}")
166 )
167 + "/"
168 )
169 error_message = gv.valid_value(
170 FFDC_LOG_PATH, var_name="FFDC_LOG_PATH"
171 )
Michael Walsh769c2a12016-12-13 15:45:17 -0600172 if error_message != "":
Michael Walshedb5c942019-03-28 12:40:50 -0500173 error_message = gp.sprint_error_report(error_message)
Michael Walsh769c2a12016-12-13 15:45:17 -0600174 BuiltIn().fail(error_message)
175 FFDC_LOG_PATH = os.path.normpath(FFDC_LOG_PATH) + os.sep
176
177 cmd_buf = ["Get Test Dir and Name"]
Michael Walshedb5c942019-03-28 12:40:50 -0500178 gp.print_issuing(cmd_buf)
Michael Walsh769c2a12016-12-13 15:45:17 -0600179 suitename, testname = BuiltIn().run_keyword(*cmd_buf)
180
181 ffdc_dir_path = FFDC_LOG_PATH + suitename + "/" + testname + "/"
182
183 # Add trailing slash.
184 ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep
185
186 if ffdc_prefix is None:
187 FFDC_TIME = BuiltIn().get_variable_value("${FFDC_TIME}")
188 if ffdc_prefix is None:
189 if ffdc_dir_path_style:
190 OPENBMC_HOST = BuiltIn().get_variable_value("${OPENBMC_HOST}")
Michael Walsha0364ae2017-01-10 11:24:42 -0600191 OPENBMC_NICKNAME = BuiltIn().get_variable_value(
Patrick Williams20f38712022-12-08 06:18:26 -0600192 "${OPENBMC_NICKNAME}", default=OPENBMC_HOST
193 )
194 ffdc_prefix = (
195 OPENBMC_NICKNAME
196 + "."
197 + FFDC_TIME[2:8]
198 + "."
199 + FFDC_TIME[8:14]
200 + "."
201 )
Michael Walsh769c2a12016-12-13 15:45:17 -0600202 else:
203 ffdc_prefix = FFDC_TIME + "_"
204
Michael Walshfa9f70f2017-04-21 16:00:18 -0500205 BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path)
206 BuiltIn().set_global_variable("${FFDC_PREFIX}", ffdc_prefix)
207
Michael Walsh769c2a12016-12-13 15:45:17 -0600208 return ffdc_dir_path, ffdc_prefix