blob: 50eefd35f19bc83988f39bfbb7a9ce38aa91da06 [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 Williams57318182022-12-08 06:18:26 -060010
11import bmc_ssh_utils as bsu
12import func_args as fa
13import pel_variables
George Keishing48ffa2c2022-02-17 01:42:41 -060014from robot.libraries.BuiltIn import BuiltIn
15
16base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17sys.path.append(base_path + "/data/")
18
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060019
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060020class peltool_exception(Exception):
21 r"""
22 Base class for peltool related exceptions.
23 """
George Keishing48ffa2c2022-02-17 01:42:41 -060024
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060025 def __init__(self, message):
26 self.message = message
27 super().__init__(self.message)
28
29
Michael Walsha20876d2020-03-18 16:32:37 -050030def peltool(option_string, parse_json=True, **bsu_options):
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060031 r"""
32 Run peltool on the BMC with the caller's option string and return the result.
33
34 Example:
35
36 ${pel_results}= Peltool -l
37 Rprint Vars pel_results
38
39 pel_results:
40 [0x50000031]:
41 [CompID]: 0x1000
42 [PLID]: 0x50000031
43 [Subsystem]: BMC Firmware
44 [Message]: An application had an internal failure
45 [SRC]: BD8D1002
46 [Commit Time]: 02/25/2020 04:51:31
47 [Sev]: Unrecoverable Error
48 [CreatorID]: BMC
49
50 Description of argument(s):
Michael Walsha20876d2020-03-18 16:32:37 -050051 option_string A string of options which are to be processed by the peltool command.
52 parse_json Indicates that the raw JSON data should parsed into a list of
53 dictionaries.
54 bsu_options Options to be passed directly to bmc_execute_command. See its prolog for
55 details.
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060056 """
57
58 bsu_options = fa.args_to_objects(bsu_options)
Patrick Williams57318182022-12-08 06:18:26 -060059 out_buf, stderr, rc = bsu.bmc_execute_command(
60 "peltool " + option_string, **bsu_options
61 )
Michael Walsha20876d2020-03-18 16:32:37 -050062 if parse_json:
63 try:
64 return json.loads(out_buf)
Sridevi Rameshc6dd7992022-02-10 01:06:47 -060065 except ValueError:
Michael Walsha20876d2020-03-18 16:32:37 -050066 return {}
Rahul Maheshwarifaa5d202020-02-24 23:32:57 -060067 return out_buf
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060068
69
Patrick Williams57318182022-12-08 06:18:26 -060070def get_pel_data_from_bmc(
71 include_hidden_pels=False, include_informational_pels=False
72):
Sridevi Ramesh29300502022-10-21 02:28:24 -050073 r"""
74 Returns PEL data from BMC else throws exception.
Sridevi Rameshd1cb3252022-11-28 10:04:05 -060075
76 Description of arguments:
77 include_hidden_pels True/False (default: False).
78 Set True to get hidden PELs else False.
79 include_informational_pels True/False (default: False).
80 Set True to get informational PELs else False.
Sridevi Ramesh29300502022-10-21 02:28:24 -050081 """
82 try:
Sridevi Rameshd1cb3252022-11-28 10:04:05 -060083 pel_cmd = " -l"
84 if include_hidden_pels:
85 pel_cmd = pel_cmd + " -h"
86 if include_informational_pels:
87 pel_cmd = pel_cmd + " -f"
88 pel_data = peltool(pel_cmd)
Sridevi Ramesh29300502022-10-21 02:28:24 -050089 if not pel_data:
90 print("No PEL data present in BMC ...")
91 except Exception as e:
92 raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
93 return pel_data
94
95
Sridevi Rameshd1cb3252022-11-28 10:04:05 -060096def fetch_all_pel_ids_for_src(src_id, severity, include_hidden_pels=False):
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -060097 r"""
Sridevi Rameshc6dd7992022-02-10 01:06:47 -060098 Fetch all PEL IDs for the input SRC ID based on the severity type
99 in the list format.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600100
101 Description of arguments:
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600102 src_id SRC ID (e.g. BCXXYYYY).
103 severity PEL severity (e.g. "Predictive Error"
104 "Recovered Error").
105 include_hidden_pels True/False (default: False).
106 Set True to get hidden PELs else False.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600107 """
108
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600109 try:
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600110 src_pel_ids = []
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600111 pel_data = get_pel_data_from_bmc(include_hidden_pels)
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600112 pel_id_list = pel_data.keys()
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600113 for pel_id in pel_id_list:
114 # Check if required SRC ID with severity is present
Patrick Williams57318182022-12-08 06:18:26 -0600115 if (pel_data[pel_id]["SRC"] == src_id) and (
116 pel_data[pel_id]["Sev"] == severity
117 ):
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600118 src_pel_ids.append(pel_id)
119
120 if not src_pel_ids:
Patrick Williams57318182022-12-08 06:18:26 -0600121 raise peltool_exception(
122 src_id + " with severity " + severity + " not present"
123 )
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600124 except Exception as e:
Patrick Williams57318182022-12-08 06:18:26 -0600125 raise peltool_exception(
126 "Failed to fetch PEL ID for required SRC : " + str(e)
127 )
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600128 return src_pel_ids
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600129
130
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600131def fetch_all_src(include_hidden_pels=False):
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600132 r"""
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600133 Fetch all SRC IDs from peltool in the list format.
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600134
135 include_hidden_pels True/False (default: False).
136 Set True to get hidden PELs else False.
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600137 """
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600138 try:
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600139 src_id = []
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600140 pel_data = get_pel_data_from_bmc(include_hidden_pels)
Sridevi Rameshc6dd7992022-02-10 01:06:47 -0600141 if pel_data:
142 pel_id_list = pel_data.keys()
143 for pel_id in pel_id_list:
144 src_id.append(pel_data[pel_id]["SRC"])
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600145 print("SRC IDs: " + str(src_id))
Sridevi Ramesh6e0e0912021-11-16 11:17:37 -0600146 except Exception as e:
147 raise peltool_exception("Failed to fetch all SRCs : " + str(e))
148 return src_id
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600149
150
Patrick Williams57318182022-12-08 06:18:26 -0600151def check_for_unexpected_src(
152 unexpected_src_list=[], include_hidden_pels=False
153):
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600154 r"""
155 From the given unexpected SRC list, check if any unexpected SRC created
156 on the BMC. Returns 0 if no SRC found else throws exception.
157
158 Description of arguments:
159 unexpected_src_list Give unexpected SRCs in the list format.
160 e.g.: ["BBXXYYYY", "AAXXYYYY"].
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600161
162 include_hidden_pels True/False (default: False).
163 Set True to get hidden PELs else False.
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600164 """
165 try:
166 unexpected_src_count = 0
167 if not unexpected_src_list:
168 print("Unexpected SRC list is empty.")
Sridevi Rameshd1cb3252022-11-28 10:04:05 -0600169 src_data = fetch_all_src(include_hidden_pels)
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600170 for src in unexpected_src_list:
171 if src in src_data:
172 print("Found an unexpected SRC : " + src)
173 unexpected_src_count = unexpected_src_count + 1
Patrick Williams57318182022-12-08 06:18:26 -0600174 if unexpected_src_count >= 1:
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600175 raise peltool_exception("Unexpected SRC found.")
176
177 except Exception as e:
Patrick Williams57318182022-12-08 06:18:26 -0600178 raise peltool_exception(
179 "Failed to verify unexpected SRC list : " + str(e)
180 )
Sridevi Ramesh6bf23632022-11-25 05:55:38 -0600181 return unexpected_src_count
Anusha Dathatrie8801a32022-11-28 04:56:31 -0600182
183
184def filter_unexpected_srcs(expected_srcs=None):
185 r"""
186 Return list of SRCs found in BMC after filtering expected SRCs.
187 If expected_srcs is None then all SRCs found in system are returned.
188
189 Description of arguments:
190 expected_srcs List of expected SRCs. E.g. ["BBXXYYYY", "AAXXYYYY"].
191 """
192
193 srcs_found = fetch_all_src()
194 if not expected_srcs:
195 expected_srcs = []
196 print(expected_srcs)
197 return list(set(srcs_found) - set(expected_srcs))
Anusha Dathatri5636ad12022-11-29 04:26:00 -0600198
199
200def get_bmc_event_log_id_for_pel(pel_id):
201 r"""
202 Return BMC event log ID for the given PEL ID.
203
204 Description of arguments:
205 pel_id PEL ID. E.g. 0x50000021.
206 """
207
208 pel_data = peltool("-i " + pel_id)
209 print(pel_data)
210 bmc_id_for_pel = pel_data["Private Header"]["BMC Event Log Id"]
211 return bmc_id_for_pel