blob: 98994782a4412ac8d1282c26f3d864290e935567 [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
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060021class peltool_exception(Exception):
22 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):
Michael Walsha20876d2020-03-18 16:32:37 -050052 option_string A string of options which are to be processed by the peltool command.
53 parse_json Indicates that the raw JSON data should parsed into a list of
54 dictionaries.
55 bsu_options Options to be passed directly to bmc_execute_command. See its prolog for
56 details.
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060057 """
58
59 bsu_options = fa.args_to_objects(bsu_options)
Patrick Williams20f38712022-12-08 06:18:26 -060060 out_buf, stderr, rc = bsu.bmc_execute_command(
61 "peltool " + option_string, **bsu_options
62 )
Michael Walsha20876d2020-03-18 16:32:37 -050063 if parse_json:
64 try:
65 return json.loads(out_buf)
Sridevi Rameshc6dd7992022-02-10 01:06:47 -060066 except ValueError:
Michael Walsha20876d2020-03-18 16:32:37 -050067 return {}
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060068 return out_buf
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060069
70
Patrick Williams20f38712022-12-08 06:18:26 -060071def get_pel_data_from_bmc(
72 include_hidden_pels=False, include_informational_pels=False
73):
Sridevi Ramesh29300502022-10-21 02:28:24 -050074 r"""
75 Returns PEL data from BMC else throws exception.
Sridevi Rameshd1cb3252022-11-28 10:04:05 -060076
77 Description of arguments:
78 include_hidden_pels True/False (default: False).
79 Set True to get hidden PELs else False.
80 include_informational_pels True/False (default: False).
81 Set True to get informational PELs else False.
Sridevi Ramesh29300502022-10-21 02:28:24 -050082 """
83 try:
Sridevi Rameshd1cb3252022-11-28 10:04:05 -060084 pel_cmd = " -l"
85 if include_hidden_pels:
86 pel_cmd = pel_cmd + " -h"
87 if include_informational_pels:
88 pel_cmd = pel_cmd + " -f"
89 pel_data = peltool(pel_cmd)
Sridevi Ramesh29300502022-10-21 02:28:24 -050090 if not pel_data:
91 print("No PEL data present in BMC ...")
92 except Exception as e:
93 raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
94 return pel_data
95
96
Sushil Singh28cedb12022-12-12 01:33:32 -060097def verify_no_pel_exists_on_bmc():
98 r"""
99 Verify that no PEL exists in BMC. Raise an exception if it does.
100 """
101
102 try:
103 pel_data = get_pel_data_from_bmc()
104
105 if len(pel_data) == 0:
106 return True
107 else:
108 print("PEL data present. \n", pel_data)
109 raise peltool_exception("PEL data present in BMC")
110 except Exception as e:
111 raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
112
113
114def compare_pel_and_redfish_event_log(pel_record, event_record):
115 r"""
116 Compare PEL log attributes like "SRC", "Created at" with Redfish
117 event log attributes like "EventId", "Created".
118 Return False if they do not match.
119
120 Description of arguments:
121 pel_record PEL record.
122 event_record Redfish event which is equivalent of PEL record.
123 """
124
125 try:
126 # Below is format of PEL record / event record and following
127 # i.e. "SRC", "Created at" from
128 # PEL record is compared with "EventId", "Created" from event record.
129
130 # PEL Log attributes
131 # SRC : XXXXXXXX
132 # Created at : 11/14/2022 12:38:04
133
134 # Event log attributes
135 # EventId : XXXXXXXX XXXXXXXX XXXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
136
137 # Created : 2022-11-14T12:38:04+00:00
138
139 print("\nPEL records : {0}".format(pel_record))
140 print("\nEvent records : {0}".format(event_record))
141
Patrick Williamsb813b552022-12-12 10:38:46 -0600142 pel_src = pel_record["pel_data"]["SRC"]
143 pel_created_time = pel_record["pel_detail_data"]["Private Header"][
144 "Created at"
145 ]
Sushil Singh28cedb12022-12-12 01:33:32 -0600146
Patrick Williamsb813b552022-12-12 10:38:46 -0600147 event_ids = (event_record["EventId"]).split(" ")
Sushil Singh28cedb12022-12-12 01:33:32 -0600148
Patrick Williamsb813b552022-12-12 10:38:46 -0600149 event_time_format = (event_record["Created"]).split("T")
150 event_date = (event_time_format[0]).split("-")
151 event_date = datetime(
152 int(event_date[0]), int(event_date[1]), int(event_date[2])
153 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600154 event_date = event_date.strftime("%m/%d/%Y")
Patrick Williamsb813b552022-12-12 10:38:46 -0600155 event_sub_time_format = (event_time_format[1]).split("+")
Sushil Singh28cedb12022-12-12 01:33:32 -0600156 event_date_time = event_date + " " + event_sub_time_format[0]
157
Patrick Williamsb813b552022-12-12 10:38:46 -0600158 event_created_time = event_date_time.replace("-", "/")
Sushil Singh28cedb12022-12-12 01:33:32 -0600159
Patrick Williamsb813b552022-12-12 10:38:46 -0600160 print(
161 "\nPEL SRC : {0} | PEL Created Time : {1}".format(
162 pel_src, pel_created_time
163 )
164 )
165 print(
166 "\nError event ID : {0} | Error Log Created Time : {1}".format(
167 event_ids[0], event_created_time
168 )
169 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600170
171 if pel_src == event_ids[0] and pel_created_time == event_created_time:
Patrick Williamsb813b552022-12-12 10:38:46 -0600172 print(
173 "\nPEL SRC and created date time match with "
174 "event ID, created time"
175 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600176 else:
Patrick Williamsb813b552022-12-12 10:38:46 -0600177 raise peltool_exception(
178 "\nPEL SRC and created date time did not "
179 "match with event ID, created time"
180 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600181 except Exception as e:
Patrick Williamsb813b552022-12-12 10:38:46 -0600182 raise peltool_exception(
George Keishinge16f1582022-12-15 07:32:21 -0600183 "Exception occurred during PEL and Event log "
Patrick Williamsb813b552022-12-12 10:38:46 -0600184 "comparison for SRC or event ID and created "
185 "time : "
186 + str(e)
187 )
Sushil Singh28cedb12022-12-12 01:33:32 -0600188
189
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600190def fetch_all_pel_ids_for_src(src_id, severity, include_hidden_pels=False):
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600191 r"""
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600192 Fetch all PEL IDs for the input SRC ID based on the severity type
193 in the list format.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600194
195 Description of arguments:
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600196 src_id SRC ID (e.g. BCXXYYYY).
197 severity PEL severity (e.g. "Predictive Error"
198 "Recovered Error").
199 include_hidden_pels True/False (default: False).
200 Set True to get hidden PELs else False.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600201 """
202
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600203 try:
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600204 src_pel_ids = []
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600205 pel_data = get_pel_data_from_bmc(include_hidden_pels)
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600206 pel_id_list = pel_data.keys()
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600207 for pel_id in pel_id_list:
208 # Check if required SRC ID with severity is present
Patrick Williams20f38712022-12-08 06:18:26 -0600209 if (pel_data[pel_id]["SRC"] == src_id) and (
210 pel_data[pel_id]["Sev"] == severity
211 ):
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600212 src_pel_ids.append(pel_id)
213
214 if not src_pel_ids:
Patrick Williams20f38712022-12-08 06:18:26 -0600215 raise peltool_exception(
216 src_id + " with severity " + severity + " not present"
217 )
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600218 except Exception as e:
Patrick Williams20f38712022-12-08 06:18:26 -0600219 raise peltool_exception(
220 "Failed to fetch PEL ID for required SRC : " + str(e)
221 )
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600222 return src_pel_ids
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600223
224
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600225def fetch_all_src(include_hidden_pels=False):
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600226 r"""
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600227 Fetch all SRC IDs from peltool in the list format.
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600228
229 include_hidden_pels True/False (default: False).
230 Set True to get hidden PELs else False.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600231 """
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600232 try:
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600233 src_id = []
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600234 pel_data = get_pel_data_from_bmc(include_hidden_pels)
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600235 if pel_data:
236 pel_id_list = pel_data.keys()
237 for pel_id in pel_id_list:
238 src_id.append(pel_data[pel_id]["SRC"])
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600239 print("SRC IDs: " + str(src_id))
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600240 except Exception as e:
241 raise peltool_exception("Failed to fetch all SRCs : " + str(e))
242 return src_id
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600243
244
Patrick Williams20f38712022-12-08 06:18:26 -0600245def check_for_unexpected_src(
246 unexpected_src_list=[], include_hidden_pels=False
247):
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600248 r"""
249 From the given unexpected SRC list, check if any unexpected SRC created
250 on the BMC. Returns 0 if no SRC found else throws exception.
251
252 Description of arguments:
253 unexpected_src_list Give unexpected SRCs in the list format.
254 e.g.: ["BBXXYYYY", "AAXXYYYY"].
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600255
256 include_hidden_pels True/False (default: False).
257 Set True to get hidden PELs else False.
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600258 """
259 try:
260 unexpected_src_count = 0
261 if not unexpected_src_list:
262 print("Unexpected SRC list is empty.")
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600263 src_data = fetch_all_src(include_hidden_pels)
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600264 for src in unexpected_src_list:
265 if src in src_data:
266 print("Found an unexpected SRC : " + src)
267 unexpected_src_count = unexpected_src_count + 1
Patrick Williams20f38712022-12-08 06:18:26 -0600268 if unexpected_src_count >= 1:
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600269 raise peltool_exception("Unexpected SRC found.")
270
271 except Exception as e:
Patrick Williams20f38712022-12-08 06:18:26 -0600272 raise peltool_exception(
273 "Failed to verify unexpected SRC list : " + str(e)
274 )
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600275 return unexpected_src_count
Anusha Dathatrie8801a32022-11-28 04:56:31 -0600276
277
278def filter_unexpected_srcs(expected_srcs=None):
279 r"""
280 Return list of SRCs found in BMC after filtering expected SRCs.
281 If expected_srcs is None then all SRCs found in system are returned.
282
283 Description of arguments:
284 expected_srcs List of expected SRCs. E.g. ["BBXXYYYY", "AAXXYYYY"].
285 """
286
287 srcs_found = fetch_all_src()
288 if not expected_srcs:
289 expected_srcs = []
290 print(expected_srcs)
291 return list(set(srcs_found) - set(expected_srcs))
Anusha Dathatri5636ad12022-11-29 04:26:00 -0600292
293
294def get_bmc_event_log_id_for_pel(pel_id):
295 r"""
296 Return BMC event log ID for the given PEL ID.
297
298 Description of arguments:
299 pel_id PEL ID. E.g. 0x50000021.
300 """
301
302 pel_data = peltool("-i " + pel_id)
303 print(pel_data)
304 bmc_id_for_pel = pel_data["Private Header"]["BMC Event Log Id"]
305 return bmc_id_for_pel