blob: 53dd8d726c2657b2c9fd8dd72e8ac01257d45bbd [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -06002
3r"""
4PEL functions.
5"""
6
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -06007import json
George Keishing48ffa2c2022-02-17 01:42:41 -06008import os
9import sys
Patrick Williamsb813b552022-12-12 10:38:46 -060010from datetime import datetime
11
Patrick Williams20f38712022-12-08 06:18:26 -060012import bmc_ssh_utils as bsu
13import func_args as fa
George Keishing48ffa2c2022-02-17 01:42:41 -060014
15base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16sys.path.append(base_path + "/data/")
17
George Keishing09679892022-12-08 08:21:52 -060018import pel_variables # NOQA
George Keishing37c58c82022-12-08 07:42:54 -060019
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060020
Anusha Dathatri3914da72023-02-09 06:01:05 -060021class PeltoolException(Exception):
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060022 r"""
23 Base class for peltool related exceptions.
24 """
George Keishing48ffa2c2022-02-17 01:42:41 -060025
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060026 def __init__(self, message):
27 self.message = message
28 super().__init__(self.message)
29
30
Michael Walsha20876d2020-03-18 16:32:37 -050031def peltool(option_string, parse_json=True, **bsu_options):
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060032 r"""
33 Run peltool on the BMC with the caller's option string and return the result.
34
35 Example:
36
37 ${pel_results}= Peltool -l
38 Rprint Vars pel_results
39
40 pel_results:
41 [0x50000031]:
42 [CompID]: 0x1000
43 [PLID]: 0x50000031
44 [Subsystem]: BMC Firmware
45 [Message]: An application had an internal failure
46 [SRC]: BD8D1002
47 [Commit Time]: 02/25/2020 04:51:31
48 [Sev]: Unrecoverable Error
49 [CreatorID]: BMC
50
51 Description of argument(s):
Anusha Dathatri3914da72023-02-09 06:01:05 -060052 option_string A string of options which are to be
53 processed by the peltool command.
54 parse_json Indicates that the raw JSON data should
55 parsed into a list of dictionaries.
56 bsu_options Options to be passed directly to
57 bmc_execute_command. See its prolog for
Michael Walsha20876d2020-03-18 16:32:37 -050058 details.
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060059 """
60
61 bsu_options = fa.args_to_objects(bsu_options)
Anusha Dathatri3914da72023-02-09 06:01:05 -060062 out_buf, _, _ = bsu.bmc_execute_command(
Patrick Williams20f38712022-12-08 06:18:26 -060063 "peltool " + option_string, **bsu_options
64 )
Michael Walsha20876d2020-03-18 16:32:37 -050065 if parse_json:
66 try:
67 return json.loads(out_buf)
Sridevi Rameshc6dd7992022-02-10 01:06:47 -060068 except ValueError:
Michael Walsha20876d2020-03-18 16:32:37 -050069 return {}
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060070 return out_buf
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060071
72
Patrick Williams20f38712022-12-08 06:18:26 -060073def get_pel_data_from_bmc(
74 include_hidden_pels=False, include_informational_pels=False
75):
Sridevi Ramesh29300502022-10-21 02:28:24 -050076 r"""
77 Returns PEL data from BMC else throws exception.
Sridevi Rameshd1cb3252022-11-28 10:04:05 -060078
79 Description of arguments:
80 include_hidden_pels True/False (default: False).
81 Set True to get hidden PELs else False.
82 include_informational_pels True/False (default: False).
83 Set True to get informational PELs else False.
Sridevi Ramesh29300502022-10-21 02:28:24 -050084 """
85 try:
Sridevi Rameshd1cb3252022-11-28 10:04:05 -060086 pel_cmd = " -l"
87 if include_hidden_pels:
88 pel_cmd = pel_cmd + " -h"
89 if include_informational_pels:
90 pel_cmd = pel_cmd + " -f"
91 pel_data = peltool(pel_cmd)
Sridevi Ramesh29300502022-10-21 02:28:24 -050092 if not pel_data:
93 print("No PEL data present in BMC ...")
Anusha Dathatri3914da72023-02-09 06:01:05 -060094 except Exception as exception:
95 raise PeltoolException(
96 "Failed to get PEL data from BMC : " + str(exception)
97 ) from exception
Sridevi Ramesh29300502022-10-21 02:28:24 -050098 return pel_data
99
100
Sushil Singh28cedb12022-12-12 01:33:32 -0600101def verify_no_pel_exists_on_bmc():
102 r"""
103 Verify that no PEL exists in BMC. Raise an exception if it does.
104 """
105
106 try:
107 pel_data = get_pel_data_from_bmc()
108
109 if len(pel_data) == 0:
110 return True
Anusha Dathatri3914da72023-02-09 06:01:05 -0600111
112 print("PEL data present. \n", pel_data)
113 raise PeltoolException("PEL data present in BMC")
114 except Exception as exception:
115 raise PeltoolException(
116 "Failed to get PEL data from BMC : " + str(exception)
117 ) from exception
Sushil Singh28cedb12022-12-12 01:33:32 -0600118
119
120def compare_pel_and_redfish_event_log(pel_record, event_record):
121 r"""
122 Compare PEL log attributes like "SRC", "Created at" with Redfish
123 event log attributes like "EventId", "Created".
124 Return False if they do not match.
125
126 Description of arguments:
127 pel_record PEL record.
128 event_record Redfish event which is equivalent of PEL record.
129 """
130
131 try:
132 # Below is format of PEL record / event record and following
133 # i.e. "SRC", "Created at" from
134 # PEL record is compared with "EventId", "Created" from event record.
135
136 # PEL Log attributes
137 # SRC : XXXXXXXX
138 # Created at : 11/14/2022 12:38:04
139
140 # Event log attributes
141 # EventId : XXXXXXXX XXXXXXXX XXXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
142
143 # Created : 2022-11-14T12:38:04+00:00
144
Anusha Dathatri3914da72023-02-09 06:01:05 -0600145 print(f"\nPEL records : {pel_record}")
146 print(f"\nEvent records : {event_record}")
Sushil Singh28cedb12022-12-12 01:33:32 -0600147
Patrick Williamsb813b552022-12-12 10:38:46 -0600148 pel_src = pel_record["pel_data"]["SRC"]
149 pel_created_time = pel_record["pel_detail_data"]["Private Header"][
150 "Created at"
151 ]
Sushil Singh28cedb12022-12-12 01:33:32 -0600152
Patrick Williamsb813b552022-12-12 10:38:46 -0600153 event_ids = (event_record["EventId"]).split(" ")
Sushil Singh28cedb12022-12-12 01:33:32 -0600154
Patrick Williamsb813b552022-12-12 10:38:46 -0600155 event_time_format = (event_record["Created"]).split("T")
156 event_date = (event_time_format[0]).split("-")
157 event_date = datetime(
158 int(event_date[0]), int(event_date[1]), int(event_date[2])
159 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600160 event_date = event_date.strftime("%m/%d/%Y")
Patrick Williamsb813b552022-12-12 10:38:46 -0600161 event_sub_time_format = (event_time_format[1]).split("+")
Sushil Singh28cedb12022-12-12 01:33:32 -0600162 event_date_time = event_date + " " + event_sub_time_format[0]
163
Patrick Williamsb813b552022-12-12 10:38:46 -0600164 event_created_time = event_date_time.replace("-", "/")
Sushil Singh28cedb12022-12-12 01:33:32 -0600165
Anusha Dathatri3914da72023-02-09 06:01:05 -0600166 print(f"\nPEL SRC : {pel_src} | PEL Created Time : {pel_created_time}")
167
Patrick Williamsb813b552022-12-12 10:38:46 -0600168 print(
Anusha Dathatri3914da72023-02-09 06:01:05 -0600169 f"\nError event ID : {event_ids[0]} | Error Log Created Time "
170 + ": {event_created_time}"
Patrick Williamsb813b552022-12-12 10:38:46 -0600171 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600172
173 if pel_src == event_ids[0] and pel_created_time == event_created_time:
Patrick Williamsb813b552022-12-12 10:38:46 -0600174 print(
175 "\nPEL SRC and created date time match with "
176 "event ID, created time"
177 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600178 else:
Anusha Dathatri3914da72023-02-09 06:01:05 -0600179 raise PeltoolException(
Patrick Williamsb813b552022-12-12 10:38:46 -0600180 "\nPEL SRC and created date time did not "
181 "match with event ID, created time"
182 )
Anusha Dathatri3914da72023-02-09 06:01:05 -0600183 except Exception as exception:
184 raise PeltoolException(
George Keishinge16f1582022-12-15 07:32:21 -0600185 "Exception occurred during PEL and Event log "
Patrick Williamsb813b552022-12-12 10:38:46 -0600186 "comparison for SRC or event ID and created "
187 "time : "
Anusha Dathatri3914da72023-02-09 06:01:05 -0600188 + str(exception)
189 ) from exception
Sushil Singh28cedb12022-12-12 01:33:32 -0600190
191
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600192def fetch_all_pel_ids_for_src(src_id, severity, include_hidden_pels=False):
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600193 r"""
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600194 Fetch all PEL IDs for the input SRC ID based on the severity type
195 in the list format.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600196
197 Description of arguments:
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600198 src_id SRC ID (e.g. BCXXYYYY).
199 severity PEL severity (e.g. "Predictive Error"
200 "Recovered Error").
201 include_hidden_pels True/False (default: False).
202 Set True to get hidden PELs else False.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600203 """
204
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600205 try:
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600206 src_pel_ids = []
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600207 pel_data = get_pel_data_from_bmc(include_hidden_pels)
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600208 pel_id_list = pel_data.keys()
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600209 for pel_id in pel_id_list:
210 # Check if required SRC ID with severity is present
Sridevi Rameshf3299972022-12-18 03:27:22 -0600211 if src_id in pel_data[pel_id]["SRC"]:
212 if pel_data[pel_id]["Sev"] == severity:
213 src_pel_ids.append(pel_id)
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600214
215 if not src_pel_ids:
Anusha Dathatri3914da72023-02-09 06:01:05 -0600216 raise PeltoolException(
Patrick Williams20f38712022-12-08 06:18:26 -0600217 src_id + " with severity " + severity + " not present"
218 )
Anusha Dathatri3914da72023-02-09 06:01:05 -0600219 except Exception as exception:
220 raise PeltoolException(
221 "Failed to fetch PEL ID for required SRC : " + str(exception)
222 ) from exception
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600223 return src_pel_ids
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600224
225
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600226def fetch_all_src(include_hidden_pels=False):
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600227 r"""
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600228 Fetch all SRC IDs from peltool in the list format.
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600229
230 include_hidden_pels True/False (default: False).
231 Set True to get hidden PELs else False.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600232 """
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600233 try:
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600234 src_id = []
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600235 pel_data = get_pel_data_from_bmc(include_hidden_pels)
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600236 if pel_data:
237 pel_id_list = pel_data.keys()
238 for pel_id in pel_id_list:
239 src_id.append(pel_data[pel_id]["SRC"])
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600240 print("SRC IDs: " + str(src_id))
Anusha Dathatri3914da72023-02-09 06:01:05 -0600241 except Exception as exception:
242 raise PeltoolException(
243 "Failed to fetch all SRCs : " + str(exception)
244 ) from exception
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600245 return src_id
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600246
247
Patrick Williams20f38712022-12-08 06:18:26 -0600248def check_for_unexpected_src(
Anusha Dathatri3914da72023-02-09 06:01:05 -0600249 unexpected_src_list=None, include_hidden_pels=False
Patrick Williams20f38712022-12-08 06:18:26 -0600250):
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600251 r"""
252 From the given unexpected SRC list, check if any unexpected SRC created
253 on the BMC. Returns 0 if no SRC found else throws exception.
254
255 Description of arguments:
256 unexpected_src_list Give unexpected SRCs in the list format.
257 e.g.: ["BBXXYYYY", "AAXXYYYY"].
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600258
259 include_hidden_pels True/False (default: False).
260 Set True to get hidden PELs else False.
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600261 """
262 try:
263 unexpected_src_count = 0
264 if not unexpected_src_list:
265 print("Unexpected SRC list is empty.")
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600266 src_data = fetch_all_src(include_hidden_pels)
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600267 for src in unexpected_src_list:
268 if src in src_data:
269 print("Found an unexpected SRC : " + src)
270 unexpected_src_count = unexpected_src_count + 1
Patrick Williams20f38712022-12-08 06:18:26 -0600271 if unexpected_src_count >= 1:
Anusha Dathatri3914da72023-02-09 06:01:05 -0600272 raise PeltoolException("Unexpected SRC found.")
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600273
Anusha Dathatri3914da72023-02-09 06:01:05 -0600274 except Exception as exception:
275 raise PeltoolException(
276 "Failed to verify unexpected SRC list : " + str(exception)
277 ) from exception
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600278 return unexpected_src_count
Anusha Dathatrie8801a32022-11-28 04:56:31 -0600279
280
281def filter_unexpected_srcs(expected_srcs=None):
282 r"""
283 Return list of SRCs found in BMC after filtering expected SRCs.
284 If expected_srcs is None then all SRCs found in system are returned.
285
286 Description of arguments:
287 expected_srcs List of expected SRCs. E.g. ["BBXXYYYY", "AAXXYYYY"].
288 """
289
290 srcs_found = fetch_all_src()
291 if not expected_srcs:
292 expected_srcs = []
293 print(expected_srcs)
294 return list(set(srcs_found) - set(expected_srcs))
Anusha Dathatri5636ad12022-11-29 04:26:00 -0600295
296
297def get_bmc_event_log_id_for_pel(pel_id):
298 r"""
299 Return BMC event log ID for the given PEL ID.
300
301 Description of arguments:
302 pel_id PEL ID. E.g. 0x50000021.
303 """
304
305 pel_data = peltool("-i " + pel_id)
306 print(pel_data)
307 bmc_id_for_pel = pel_data["Private Header"]["BMC Event Log Id"]
308 return bmc_id_for_pel
Anusha Dathatri3914da72023-02-09 06:01:05 -0600309
310
311def get_latest_pels(number_of_pels=1):
312 r"""
313 Return latest PEL IDs.
314
315 Description of arguments:
316 number_of_pels Number of PELS to be returned.
317 """
318
319 pel_data = peltool("-lr")
320 pel_ids = list(pel_data.keys())
321 return pel_ids[:number_of_pels]