xref: /openbmc/openbmc-test-automation/lib/pel_utils.py (revision 3914da7d0a7e574372765e1a52933b06fe6fa898)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2faa5d20aSRahul Maheshwari
3faa5d20aSRahul Maheshwarir"""
4faa5d20aSRahul MaheshwariPEL functions.
5faa5d20aSRahul Maheshwari"""
6faa5d20aSRahul Maheshwari
7faa5d20aSRahul Maheshwariimport json
848ffa2c2SGeorge Keishingimport os
948ffa2c2SGeorge Keishingimport sys
10b813b55aSPatrick Williamsfrom datetime import datetime
11b813b55aSPatrick Williams
1220f38712SPatrick Williamsimport bmc_ssh_utils as bsu
1320f38712SPatrick Williamsimport func_args as fa
1448ffa2c2SGeorge Keishing
1548ffa2c2SGeorge Keishingbase_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1648ffa2c2SGeorge Keishingsys.path.append(base_path + "/data/")
1748ffa2c2SGeorge Keishing
1809679890SGeorge Keishingimport pel_variables  # NOQA
1937c58c8cSGeorge Keishing
20faa5d20aSRahul Maheshwari
21*3914da7dSAnusha Dathatriclass PeltoolException(Exception):
226e0e0919SSridevi Ramesh    r"""
236e0e0919SSridevi Ramesh    Base class for peltool related exceptions.
246e0e0919SSridevi Ramesh    """
2548ffa2c2SGeorge Keishing
266e0e0919SSridevi Ramesh    def __init__(self, message):
276e0e0919SSridevi Ramesh        self.message = message
286e0e0919SSridevi Ramesh        super().__init__(self.message)
296e0e0919SSridevi Ramesh
306e0e0919SSridevi Ramesh
31a20876d3SMichael Walshdef peltool(option_string, parse_json=True, **bsu_options):
32faa5d20aSRahul Maheshwari    r"""
33faa5d20aSRahul Maheshwari    Run peltool on the BMC with the caller's option string and return the result.
34faa5d20aSRahul Maheshwari
35faa5d20aSRahul Maheshwari    Example:
36faa5d20aSRahul Maheshwari
37faa5d20aSRahul Maheshwari    ${pel_results}=  Peltool  -l
38faa5d20aSRahul Maheshwari    Rprint Vars  pel_results
39faa5d20aSRahul Maheshwari
40faa5d20aSRahul Maheshwari    pel_results:
41faa5d20aSRahul Maheshwari      [0x50000031]:
42faa5d20aSRahul Maheshwari        [CompID]:                       0x1000
43faa5d20aSRahul Maheshwari        [PLID]:                         0x50000031
44faa5d20aSRahul Maheshwari        [Subsystem]:                    BMC Firmware
45faa5d20aSRahul Maheshwari        [Message]:                      An application had an internal failure
46faa5d20aSRahul Maheshwari        [SRC]:                          BD8D1002
47faa5d20aSRahul Maheshwari        [Commit Time]:                  02/25/2020  04:51:31
48faa5d20aSRahul Maheshwari        [Sev]:                          Unrecoverable Error
49faa5d20aSRahul Maheshwari        [CreatorID]:                    BMC
50faa5d20aSRahul Maheshwari
51faa5d20aSRahul Maheshwari    Description of argument(s):
52*3914da7dSAnusha Dathatri    option_string                   A string of options which are to be
53*3914da7dSAnusha Dathatri                                    processed by the peltool command.
54*3914da7dSAnusha Dathatri    parse_json                      Indicates that the raw JSON data should
55*3914da7dSAnusha Dathatri                                    parsed into a list of dictionaries.
56*3914da7dSAnusha Dathatri    bsu_options                     Options to be passed directly to
57*3914da7dSAnusha Dathatri                                    bmc_execute_command. See its prolog for
58faa5d20aSRahul Maheshwari                                    details.
59faa5d20aSRahul Maheshwari    """
60faa5d20aSRahul Maheshwari
61faa5d20aSRahul Maheshwari    bsu_options = fa.args_to_objects(bsu_options)
62*3914da7dSAnusha Dathatri    out_buf, _, _ = bsu.bmc_execute_command(
6320f38712SPatrick Williams        "peltool " + option_string, **bsu_options
6420f38712SPatrick Williams    )
65a20876d3SMichael Walsh    if parse_json:
66a20876d3SMichael Walsh        try:
67a20876d3SMichael Walsh            return json.loads(out_buf)
68c6dd799dSSridevi Ramesh        except ValueError:
69a20876d3SMichael Walsh            return {}
70faa5d20aSRahul Maheshwari    return out_buf
716e0e0919SSridevi Ramesh
726e0e0919SSridevi Ramesh
7320f38712SPatrick Williamsdef get_pel_data_from_bmc(
7420f38712SPatrick Williams    include_hidden_pels=False, include_informational_pels=False
7520f38712SPatrick Williams):
762930050aSSridevi Ramesh    r"""
772930050aSSridevi Ramesh    Returns PEL data from BMC else throws exception.
78d1cb3252SSridevi Ramesh
79d1cb3252SSridevi Ramesh    Description of arguments:
80d1cb3252SSridevi Ramesh    include_hidden_pels           True/False (default: False).
81d1cb3252SSridevi Ramesh                                  Set True to get hidden PELs else False.
82d1cb3252SSridevi Ramesh    include_informational_pels    True/False (default: False).
83d1cb3252SSridevi Ramesh                                  Set True to get informational PELs else False.
842930050aSSridevi Ramesh    """
852930050aSSridevi Ramesh    try:
86d1cb3252SSridevi Ramesh        pel_cmd = " -l"
87d1cb3252SSridevi Ramesh        if include_hidden_pels:
88d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -h"
89d1cb3252SSridevi Ramesh        if include_informational_pels:
90d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -f"
91d1cb3252SSridevi Ramesh        pel_data = peltool(pel_cmd)
922930050aSSridevi Ramesh        if not pel_data:
932930050aSSridevi Ramesh            print("No PEL data present in BMC ...")
94*3914da7dSAnusha Dathatri    except Exception as exception:
95*3914da7dSAnusha Dathatri        raise PeltoolException(
96*3914da7dSAnusha Dathatri            "Failed to get PEL data from BMC : " + str(exception)
97*3914da7dSAnusha Dathatri        ) from exception
982930050aSSridevi Ramesh    return pel_data
992930050aSSridevi Ramesh
1002930050aSSridevi Ramesh
10128cedb13SSushil Singhdef verify_no_pel_exists_on_bmc():
10228cedb13SSushil Singh    r"""
10328cedb13SSushil Singh    Verify that no PEL exists in BMC. Raise an exception if it does.
10428cedb13SSushil Singh    """
10528cedb13SSushil Singh
10628cedb13SSushil Singh    try:
10728cedb13SSushil Singh        pel_data = get_pel_data_from_bmc()
10828cedb13SSushil Singh
10928cedb13SSushil Singh        if len(pel_data) == 0:
11028cedb13SSushil Singh            return True
111*3914da7dSAnusha Dathatri
11228cedb13SSushil Singh        print("PEL data present. \n", pel_data)
113*3914da7dSAnusha Dathatri        raise PeltoolException("PEL data present in BMC")
114*3914da7dSAnusha Dathatri    except Exception as exception:
115*3914da7dSAnusha Dathatri        raise PeltoolException(
116*3914da7dSAnusha Dathatri            "Failed to get PEL data from BMC : " + str(exception)
117*3914da7dSAnusha Dathatri        ) from exception
11828cedb13SSushil Singh
11928cedb13SSushil Singh
12028cedb13SSushil Singhdef compare_pel_and_redfish_event_log(pel_record, event_record):
12128cedb13SSushil Singh    r"""
12228cedb13SSushil Singh    Compare PEL log attributes like "SRC", "Created at" with Redfish
12328cedb13SSushil Singh    event log attributes like "EventId", "Created".
12428cedb13SSushil Singh    Return False if they do not match.
12528cedb13SSushil Singh
12628cedb13SSushil Singh    Description of arguments:
12728cedb13SSushil Singh    pel_record     PEL record.
12828cedb13SSushil Singh    event_record   Redfish event which is equivalent of PEL record.
12928cedb13SSushil Singh    """
13028cedb13SSushil Singh
13128cedb13SSushil Singh    try:
13228cedb13SSushil Singh        # Below is format of PEL record / event record and following
13328cedb13SSushil Singh        # i.e. "SRC", "Created at" from
13428cedb13SSushil Singh        # PEL record is compared with "EventId", "Created" from event record.
13528cedb13SSushil Singh
13628cedb13SSushil Singh        # PEL Log attributes
13728cedb13SSushil Singh        # SRC        : XXXXXXXX
13828cedb13SSushil Singh        # Created at : 11/14/2022 12:38:04
13928cedb13SSushil Singh
14028cedb13SSushil Singh        # Event log attributes
14128cedb13SSushil Singh        # EventId : XXXXXXXX XXXXXXXX XXXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
14228cedb13SSushil Singh
14328cedb13SSushil Singh        # Created : 2022-11-14T12:38:04+00:00
14428cedb13SSushil Singh
145*3914da7dSAnusha Dathatri        print(f"\nPEL records : {pel_record}")
146*3914da7dSAnusha Dathatri        print(f"\nEvent records : {event_record}")
14728cedb13SSushil Singh
148b813b55aSPatrick Williams        pel_src = pel_record["pel_data"]["SRC"]
149b813b55aSPatrick Williams        pel_created_time = pel_record["pel_detail_data"]["Private Header"][
150b813b55aSPatrick Williams            "Created at"
151b813b55aSPatrick Williams        ]
15228cedb13SSushil Singh
153b813b55aSPatrick Williams        event_ids = (event_record["EventId"]).split(" ")
15428cedb13SSushil Singh
155b813b55aSPatrick Williams        event_time_format = (event_record["Created"]).split("T")
156b813b55aSPatrick Williams        event_date = (event_time_format[0]).split("-")
157b813b55aSPatrick Williams        event_date = datetime(
158b813b55aSPatrick Williams            int(event_date[0]), int(event_date[1]), int(event_date[2])
159b813b55aSPatrick Williams        )
16028cedb13SSushil Singh        event_date = event_date.strftime("%m/%d/%Y")
161b813b55aSPatrick Williams        event_sub_time_format = (event_time_format[1]).split("+")
16228cedb13SSushil Singh        event_date_time = event_date + " " + event_sub_time_format[0]
16328cedb13SSushil Singh
164b813b55aSPatrick Williams        event_created_time = event_date_time.replace("-", "/")
16528cedb13SSushil Singh
166*3914da7dSAnusha Dathatri        print(f"\nPEL SRC : {pel_src} | PEL Created Time : {pel_created_time}")
167*3914da7dSAnusha Dathatri
168b813b55aSPatrick Williams        print(
169*3914da7dSAnusha Dathatri            f"\nError event ID : {event_ids[0]} | Error Log Created Time "
170*3914da7dSAnusha Dathatri            + ": {event_created_time}"
171b813b55aSPatrick Williams        )
17228cedb13SSushil Singh
17328cedb13SSushil Singh        if pel_src == event_ids[0] and pel_created_time == event_created_time:
174b813b55aSPatrick Williams            print(
175b813b55aSPatrick Williams                "\nPEL SRC and created date time match with "
176b813b55aSPatrick Williams                "event ID, created time"
177b813b55aSPatrick Williams            )
17828cedb13SSushil Singh        else:
179*3914da7dSAnusha Dathatri            raise PeltoolException(
180b813b55aSPatrick Williams                "\nPEL SRC and created date time did not "
181b813b55aSPatrick Williams                "match with event ID, created time"
182b813b55aSPatrick Williams            )
183*3914da7dSAnusha Dathatri    except Exception as exception:
184*3914da7dSAnusha Dathatri        raise PeltoolException(
185e16f158fSGeorge Keishing            "Exception occurred during PEL and Event log "
18628cedb13SSushil Singh            "comparison for SRC or event ID and created "
187b813b55aSPatrick Williams            "time : "
188*3914da7dSAnusha Dathatri            + str(exception)
189*3914da7dSAnusha Dathatri        ) from exception
19028cedb13SSushil Singh
19128cedb13SSushil Singh
192d1cb3252SSridevi Rameshdef fetch_all_pel_ids_for_src(src_id, severity, include_hidden_pels=False):
1936e0e0919SSridevi Ramesh    r"""
194c6dd799dSSridevi Ramesh    Fetch all PEL IDs for the input SRC ID based on the severity type
195c6dd799dSSridevi Ramesh    in the list format.
1966e0e0919SSridevi Ramesh
1976e0e0919SSridevi Ramesh    Description of arguments:
198d1cb3252SSridevi Ramesh    src_id                SRC ID (e.g. BCXXYYYY).
199c6dd799dSSridevi Ramesh    severity              PEL severity (e.g. "Predictive Error"
200c6dd799dSSridevi Ramesh                                             "Recovered Error").
201d1cb3252SSridevi Ramesh    include_hidden_pels   True/False (default: False).
202d1cb3252SSridevi Ramesh                          Set True to get hidden PELs else False.
2036e0e0919SSridevi Ramesh    """
2046e0e0919SSridevi Ramesh
2056e0e0919SSridevi Ramesh    try:
206c6dd799dSSridevi Ramesh        src_pel_ids = []
207d1cb3252SSridevi Ramesh        pel_data = get_pel_data_from_bmc(include_hidden_pels)
2086e0e0919SSridevi Ramesh        pel_id_list = pel_data.keys()
209c6dd799dSSridevi Ramesh        for pel_id in pel_id_list:
210c6dd799dSSridevi Ramesh            # Check if required SRC ID with severity is present
211f3299971SSridevi Ramesh            if src_id in pel_data[pel_id]["SRC"]:
212f3299971SSridevi Ramesh                if pel_data[pel_id]["Sev"] == severity:
213c6dd799dSSridevi Ramesh                    src_pel_ids.append(pel_id)
214c6dd799dSSridevi Ramesh
215c6dd799dSSridevi Ramesh        if not src_pel_ids:
216*3914da7dSAnusha Dathatri            raise PeltoolException(
21720f38712SPatrick Williams                src_id + " with severity " + severity + " not present"
21820f38712SPatrick Williams            )
219*3914da7dSAnusha Dathatri    except Exception as exception:
220*3914da7dSAnusha Dathatri        raise PeltoolException(
221*3914da7dSAnusha Dathatri            "Failed to fetch PEL ID for required SRC : " + str(exception)
222*3914da7dSAnusha Dathatri        ) from exception
223c6dd799dSSridevi Ramesh    return src_pel_ids
2246e0e0919SSridevi Ramesh
2256e0e0919SSridevi Ramesh
226d1cb3252SSridevi Rameshdef fetch_all_src(include_hidden_pels=False):
2276e0e0919SSridevi Ramesh    r"""
228c6dd799dSSridevi Ramesh    Fetch all SRC IDs from peltool in the list format.
229d1cb3252SSridevi Ramesh
230d1cb3252SSridevi Ramesh    include_hidden_pels       True/False (default: False).
231d1cb3252SSridevi Ramesh                              Set True to get hidden PELs else False.
2326e0e0919SSridevi Ramesh    """
2336e0e0919SSridevi Ramesh    try:
234c6dd799dSSridevi Ramesh        src_id = []
235d1cb3252SSridevi Ramesh        pel_data = get_pel_data_from_bmc(include_hidden_pels)
236c6dd799dSSridevi Ramesh        if pel_data:
2376e0e0919SSridevi Ramesh            pel_id_list = pel_data.keys()
238c6dd799dSSridevi Ramesh            for pel_id in pel_id_list:
239c6dd799dSSridevi Ramesh                src_id.append(pel_data[pel_id]["SRC"])
2406bf23630SSridevi Ramesh        print("SRC IDs: " + str(src_id))
241*3914da7dSAnusha Dathatri    except Exception as exception:
242*3914da7dSAnusha Dathatri        raise PeltoolException(
243*3914da7dSAnusha Dathatri            "Failed to fetch all SRCs : " + str(exception)
244*3914da7dSAnusha Dathatri        ) from exception
2456e0e0919SSridevi Ramesh    return src_id
2466bf23630SSridevi Ramesh
2476bf23630SSridevi Ramesh
24820f38712SPatrick Williamsdef check_for_unexpected_src(
249*3914da7dSAnusha Dathatri    unexpected_src_list=None, include_hidden_pels=False
25020f38712SPatrick Williams):
2516bf23630SSridevi Ramesh    r"""
2526bf23630SSridevi Ramesh    From the given unexpected SRC list, check if any unexpected SRC created
2536bf23630SSridevi Ramesh    on the BMC. Returns 0 if no SRC found else throws exception.
2546bf23630SSridevi Ramesh
2556bf23630SSridevi Ramesh    Description of arguments:
2566bf23630SSridevi Ramesh    unexpected_src_list       Give unexpected SRCs in the list format.
2576bf23630SSridevi Ramesh                              e.g.: ["BBXXYYYY", "AAXXYYYY"].
258d1cb3252SSridevi Ramesh
259d1cb3252SSridevi Ramesh    include_hidden_pels       True/False (default: False).
260d1cb3252SSridevi Ramesh                              Set True to get hidden PELs else False.
2616bf23630SSridevi Ramesh    """
2626bf23630SSridevi Ramesh    try:
2636bf23630SSridevi Ramesh        unexpected_src_count = 0
2646bf23630SSridevi Ramesh        if not unexpected_src_list:
2656bf23630SSridevi Ramesh            print("Unexpected SRC list is empty.")
266d1cb3252SSridevi Ramesh        src_data = fetch_all_src(include_hidden_pels)
2676bf23630SSridevi Ramesh        for src in unexpected_src_list:
2686bf23630SSridevi Ramesh            if src in src_data:
2696bf23630SSridevi Ramesh                print("Found an unexpected SRC : " + src)
2706bf23630SSridevi Ramesh                unexpected_src_count = unexpected_src_count + 1
27120f38712SPatrick Williams        if unexpected_src_count >= 1:
272*3914da7dSAnusha Dathatri            raise PeltoolException("Unexpected SRC found.")
2736bf23630SSridevi Ramesh
274*3914da7dSAnusha Dathatri    except Exception as exception:
275*3914da7dSAnusha Dathatri        raise PeltoolException(
276*3914da7dSAnusha Dathatri            "Failed to verify unexpected SRC list : " + str(exception)
277*3914da7dSAnusha Dathatri        ) from exception
2786bf23630SSridevi Ramesh    return unexpected_src_count
279e8801a3fSAnusha Dathatri
280e8801a3fSAnusha Dathatri
281e8801a3fSAnusha Dathatridef filter_unexpected_srcs(expected_srcs=None):
282e8801a3fSAnusha Dathatri    r"""
283e8801a3fSAnusha Dathatri    Return list of SRCs found in BMC after filtering expected SRCs.
284e8801a3fSAnusha Dathatri    If expected_srcs is None then all SRCs found in system are returned.
285e8801a3fSAnusha Dathatri
286e8801a3fSAnusha Dathatri    Description of arguments:
287e8801a3fSAnusha Dathatri    expected_srcs       List of expected SRCs. E.g. ["BBXXYYYY", "AAXXYYYY"].
288e8801a3fSAnusha Dathatri    """
289e8801a3fSAnusha Dathatri
290e8801a3fSAnusha Dathatri    srcs_found = fetch_all_src()
291e8801a3fSAnusha Dathatri    if not expected_srcs:
292e8801a3fSAnusha Dathatri        expected_srcs = []
293e8801a3fSAnusha Dathatri    print(expected_srcs)
294e8801a3fSAnusha Dathatri    return list(set(srcs_found) - set(expected_srcs))
2955636ad12SAnusha Dathatri
2965636ad12SAnusha Dathatri
2975636ad12SAnusha Dathatridef get_bmc_event_log_id_for_pel(pel_id):
2985636ad12SAnusha Dathatri    r"""
2995636ad12SAnusha Dathatri    Return BMC event log ID for the given PEL ID.
3005636ad12SAnusha Dathatri
3015636ad12SAnusha Dathatri    Description of arguments:
3025636ad12SAnusha Dathatri    pel_id       PEL ID. E.g. 0x50000021.
3035636ad12SAnusha Dathatri    """
3045636ad12SAnusha Dathatri
3055636ad12SAnusha Dathatri    pel_data = peltool("-i " + pel_id)
3065636ad12SAnusha Dathatri    print(pel_data)
3075636ad12SAnusha Dathatri    bmc_id_for_pel = pel_data["Private Header"]["BMC Event Log Id"]
3085636ad12SAnusha Dathatri    return bmc_id_for_pel
309*3914da7dSAnusha Dathatri
310*3914da7dSAnusha Dathatri
311*3914da7dSAnusha Dathatridef get_latest_pels(number_of_pels=1):
312*3914da7dSAnusha Dathatri    r"""
313*3914da7dSAnusha Dathatri    Return latest PEL IDs.
314*3914da7dSAnusha Dathatri
315*3914da7dSAnusha Dathatri    Description of arguments:
316*3914da7dSAnusha Dathatri    number_of_pels       Number of PELS to be returned.
317*3914da7dSAnusha Dathatri    """
318*3914da7dSAnusha Dathatri
319*3914da7dSAnusha Dathatri    pel_data = peltool("-lr")
320*3914da7dSAnusha Dathatri    pel_ids = list(pel_data.keys())
321*3914da7dSAnusha Dathatri    return pel_ids[:number_of_pels]
322