xref: /openbmc/openbmc-test-automation/lib/pel_utils.py (revision b813b55a308291c336930adcb104cbb4865dde28)
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
10*b813b55aSPatrick Williamsfrom datetime import datetime
11*b813b55aSPatrick Williams
1220f38712SPatrick Williamsimport bmc_ssh_utils as bsu
1320f38712SPatrick Williamsimport func_args as fa
14*b813b55aSPatrick Williamsfrom robot.libraries.BuiltIn import BuiltIn
1548ffa2c2SGeorge Keishing
1648ffa2c2SGeorge Keishingbase_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1748ffa2c2SGeorge Keishingsys.path.append(base_path + "/data/")
1848ffa2c2SGeorge Keishing
1909679890SGeorge Keishingimport pel_variables  # NOQA
2037c58c8cSGeorge Keishing
21faa5d20aSRahul Maheshwari
226e0e0919SSridevi Rameshclass peltool_exception(Exception):
236e0e0919SSridevi Ramesh    r"""
246e0e0919SSridevi Ramesh    Base class for peltool related exceptions.
256e0e0919SSridevi Ramesh    """
2648ffa2c2SGeorge Keishing
276e0e0919SSridevi Ramesh    def __init__(self, message):
286e0e0919SSridevi Ramesh        self.message = message
296e0e0919SSridevi Ramesh        super().__init__(self.message)
306e0e0919SSridevi Ramesh
316e0e0919SSridevi Ramesh
32a20876d3SMichael Walshdef peltool(option_string, parse_json=True, **bsu_options):
33faa5d20aSRahul Maheshwari    r"""
34faa5d20aSRahul Maheshwari    Run peltool on the BMC with the caller's option string and return the result.
35faa5d20aSRahul Maheshwari
36faa5d20aSRahul Maheshwari    Example:
37faa5d20aSRahul Maheshwari
38faa5d20aSRahul Maheshwari    ${pel_results}=  Peltool  -l
39faa5d20aSRahul Maheshwari    Rprint Vars  pel_results
40faa5d20aSRahul Maheshwari
41faa5d20aSRahul Maheshwari    pel_results:
42faa5d20aSRahul Maheshwari      [0x50000031]:
43faa5d20aSRahul Maheshwari        [CompID]:                       0x1000
44faa5d20aSRahul Maheshwari        [PLID]:                         0x50000031
45faa5d20aSRahul Maheshwari        [Subsystem]:                    BMC Firmware
46faa5d20aSRahul Maheshwari        [Message]:                      An application had an internal failure
47faa5d20aSRahul Maheshwari        [SRC]:                          BD8D1002
48faa5d20aSRahul Maheshwari        [Commit Time]:                  02/25/2020  04:51:31
49faa5d20aSRahul Maheshwari        [Sev]:                          Unrecoverable Error
50faa5d20aSRahul Maheshwari        [CreatorID]:                    BMC
51faa5d20aSRahul Maheshwari
52faa5d20aSRahul Maheshwari    Description of argument(s):
53faa5d20aSRahul Maheshwari    option_string                   A string of options which are to be processed by the peltool command.
54a20876d3SMichael Walsh    parse_json                      Indicates that the raw JSON data should parsed into a list of
55a20876d3SMichael Walsh                                    dictionaries.
56faa5d20aSRahul Maheshwari    bsu_options                     Options to be passed directly to bmc_execute_command. See its prolog for
57faa5d20aSRahul Maheshwari                                    details.
58faa5d20aSRahul Maheshwari    """
59faa5d20aSRahul Maheshwari
60faa5d20aSRahul Maheshwari    bsu_options = fa.args_to_objects(bsu_options)
6120f38712SPatrick Williams    out_buf, stderr, rc = bsu.bmc_execute_command(
6220f38712SPatrick Williams        "peltool " + option_string, **bsu_options
6320f38712SPatrick Williams    )
64a20876d3SMichael Walsh    if parse_json:
65a20876d3SMichael Walsh        try:
66a20876d3SMichael Walsh            return json.loads(out_buf)
67c6dd799dSSridevi Ramesh        except ValueError:
68a20876d3SMichael Walsh            return {}
69faa5d20aSRahul Maheshwari    return out_buf
706e0e0919SSridevi Ramesh
716e0e0919SSridevi Ramesh
7220f38712SPatrick Williamsdef get_pel_data_from_bmc(
7320f38712SPatrick Williams    include_hidden_pels=False, include_informational_pels=False
7420f38712SPatrick Williams):
752930050aSSridevi Ramesh    r"""
762930050aSSridevi Ramesh    Returns PEL data from BMC else throws exception.
77d1cb3252SSridevi Ramesh
78d1cb3252SSridevi Ramesh    Description of arguments:
79d1cb3252SSridevi Ramesh    include_hidden_pels           True/False (default: False).
80d1cb3252SSridevi Ramesh                                  Set True to get hidden PELs else False.
81d1cb3252SSridevi Ramesh    include_informational_pels    True/False (default: False).
82d1cb3252SSridevi Ramesh                                  Set True to get informational PELs else False.
832930050aSSridevi Ramesh    """
842930050aSSridevi Ramesh    try:
85d1cb3252SSridevi Ramesh        pel_cmd = " -l"
86d1cb3252SSridevi Ramesh        if include_hidden_pels:
87d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -h"
88d1cb3252SSridevi Ramesh        if include_informational_pels:
89d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -f"
90d1cb3252SSridevi Ramesh        pel_data = peltool(pel_cmd)
912930050aSSridevi Ramesh        if not pel_data:
922930050aSSridevi Ramesh            print("No PEL data present in BMC ...")
932930050aSSridevi Ramesh    except Exception as e:
942930050aSSridevi Ramesh        raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
952930050aSSridevi Ramesh    return pel_data
962930050aSSridevi Ramesh
972930050aSSridevi Ramesh
9828cedb13SSushil Singhdef verify_no_pel_exists_on_bmc():
9928cedb13SSushil Singh    r"""
10028cedb13SSushil Singh    Verify that no PEL exists in BMC. Raise an exception if it does.
10128cedb13SSushil Singh    """
10228cedb13SSushil Singh
10328cedb13SSushil Singh    try:
10428cedb13SSushil Singh        pel_data = get_pel_data_from_bmc()
10528cedb13SSushil Singh
10628cedb13SSushil Singh        if len(pel_data) == 0:
10728cedb13SSushil Singh            return True
10828cedb13SSushil Singh        else:
10928cedb13SSushil Singh            print("PEL data present. \n", pel_data)
11028cedb13SSushil Singh            raise peltool_exception("PEL data present in BMC")
11128cedb13SSushil Singh    except Exception as e:
11228cedb13SSushil Singh        raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
11328cedb13SSushil Singh
11428cedb13SSushil Singh
11528cedb13SSushil Singhdef compare_pel_and_redfish_event_log(pel_record, event_record):
11628cedb13SSushil Singh    r"""
11728cedb13SSushil Singh    Compare PEL log attributes like "SRC", "Created at" with Redfish
11828cedb13SSushil Singh    event log attributes like "EventId", "Created".
11928cedb13SSushil Singh    Return False if they do not match.
12028cedb13SSushil Singh
12128cedb13SSushil Singh    Description of arguments:
12228cedb13SSushil Singh    pel_record     PEL record.
12328cedb13SSushil Singh    event_record   Redfish event which is equivalent of PEL record.
12428cedb13SSushil Singh    """
12528cedb13SSushil Singh
12628cedb13SSushil Singh    try:
12728cedb13SSushil Singh        # Below is format of PEL record / event record and following
12828cedb13SSushil Singh        # i.e. "SRC", "Created at" from
12928cedb13SSushil Singh        # PEL record is compared with "EventId", "Created" from event record.
13028cedb13SSushil Singh
13128cedb13SSushil Singh        # PEL Log attributes
13228cedb13SSushil Singh        # SRC        : XXXXXXXX
13328cedb13SSushil Singh        # Created at : 11/14/2022 12:38:04
13428cedb13SSushil Singh
13528cedb13SSushil Singh        # Event log attributes
13628cedb13SSushil Singh        # EventId : XXXXXXXX XXXXXXXX XXXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
13728cedb13SSushil Singh
13828cedb13SSushil Singh        # Created : 2022-11-14T12:38:04+00:00
13928cedb13SSushil Singh
14028cedb13SSushil Singh        print("\nPEL records : {0}".format(pel_record))
14128cedb13SSushil Singh        print("\nEvent records : {0}".format(event_record))
14228cedb13SSushil Singh
143*b813b55aSPatrick Williams        pel_src = pel_record["pel_data"]["SRC"]
144*b813b55aSPatrick Williams        pel_created_time = pel_record["pel_detail_data"]["Private Header"][
145*b813b55aSPatrick Williams            "Created at"
146*b813b55aSPatrick Williams        ]
14728cedb13SSushil Singh
148*b813b55aSPatrick Williams        event_ids = (event_record["EventId"]).split(" ")
14928cedb13SSushil Singh
150*b813b55aSPatrick Williams        event_time_format = (event_record["Created"]).split("T")
151*b813b55aSPatrick Williams        event_date = (event_time_format[0]).split("-")
152*b813b55aSPatrick Williams        event_date = datetime(
153*b813b55aSPatrick Williams            int(event_date[0]), int(event_date[1]), int(event_date[2])
154*b813b55aSPatrick Williams        )
15528cedb13SSushil Singh        event_date = event_date.strftime("%m/%d/%Y")
156*b813b55aSPatrick Williams        event_sub_time_format = (event_time_format[1]).split("+")
15728cedb13SSushil Singh        event_date_time = event_date + " " + event_sub_time_format[0]
15828cedb13SSushil Singh
159*b813b55aSPatrick Williams        event_created_time = event_date_time.replace("-", "/")
16028cedb13SSushil Singh
161*b813b55aSPatrick Williams        print(
162*b813b55aSPatrick Williams            "\nPEL SRC : {0} | PEL Created Time : {1}".format(
163*b813b55aSPatrick Williams                pel_src, pel_created_time
164*b813b55aSPatrick Williams            )
165*b813b55aSPatrick Williams        )
166*b813b55aSPatrick Williams        print(
167*b813b55aSPatrick Williams            "\nError event ID : {0} | Error Log Created Time : {1}".format(
168*b813b55aSPatrick Williams                event_ids[0], event_created_time
169*b813b55aSPatrick Williams            )
170*b813b55aSPatrick Williams        )
17128cedb13SSushil Singh
17228cedb13SSushil Singh        if pel_src == event_ids[0] and pel_created_time == event_created_time:
173*b813b55aSPatrick Williams            print(
174*b813b55aSPatrick Williams                "\nPEL SRC and created date time match with "
175*b813b55aSPatrick Williams                "event ID, created time"
176*b813b55aSPatrick Williams            )
17728cedb13SSushil Singh        else:
178*b813b55aSPatrick Williams            raise peltool_exception(
179*b813b55aSPatrick Williams                "\nPEL SRC and created date time did not "
180*b813b55aSPatrick Williams                "match with event ID, created time"
181*b813b55aSPatrick Williams            )
18228cedb13SSushil Singh    except Exception as e:
183*b813b55aSPatrick Williams        raise peltool_exception(
184*b813b55aSPatrick Williams            "Exception occured during PEL and Event log "
18528cedb13SSushil Singh            "comparison for SRC or event ID and created "
186*b813b55aSPatrick Williams            "time : "
187*b813b55aSPatrick Williams            + str(e)
188*b813b55aSPatrick Williams        )
18928cedb13SSushil Singh
19028cedb13SSushil Singh
191d1cb3252SSridevi Rameshdef fetch_all_pel_ids_for_src(src_id, severity, include_hidden_pels=False):
1926e0e0919SSridevi Ramesh    r"""
193c6dd799dSSridevi Ramesh    Fetch all PEL IDs for the input SRC ID based on the severity type
194c6dd799dSSridevi Ramesh    in the list format.
1956e0e0919SSridevi Ramesh
1966e0e0919SSridevi Ramesh    Description of arguments:
197d1cb3252SSridevi Ramesh    src_id                SRC ID (e.g. BCXXYYYY).
198c6dd799dSSridevi Ramesh    severity              PEL severity (e.g. "Predictive Error"
199c6dd799dSSridevi Ramesh                                             "Recovered Error").
200d1cb3252SSridevi Ramesh    include_hidden_pels   True/False (default: False).
201d1cb3252SSridevi Ramesh                          Set True to get hidden PELs else False.
2026e0e0919SSridevi Ramesh    """
2036e0e0919SSridevi Ramesh
2046e0e0919SSridevi Ramesh    try:
205c6dd799dSSridevi Ramesh        src_pel_ids = []
206d1cb3252SSridevi Ramesh        pel_data = get_pel_data_from_bmc(include_hidden_pels)
2076e0e0919SSridevi Ramesh        pel_id_list = pel_data.keys()
208c6dd799dSSridevi Ramesh        for pel_id in pel_id_list:
209c6dd799dSSridevi Ramesh            # Check if required SRC ID with severity is present
21020f38712SPatrick Williams            if (pel_data[pel_id]["SRC"] == src_id) and (
21120f38712SPatrick Williams                pel_data[pel_id]["Sev"] == severity
21220f38712SPatrick Williams            ):
213c6dd799dSSridevi Ramesh                src_pel_ids.append(pel_id)
214c6dd799dSSridevi Ramesh
215c6dd799dSSridevi Ramesh        if not src_pel_ids:
21620f38712SPatrick Williams            raise peltool_exception(
21720f38712SPatrick Williams                src_id + " with severity " + severity + " not present"
21820f38712SPatrick Williams            )
2196e0e0919SSridevi Ramesh    except Exception as e:
22020f38712SPatrick Williams        raise peltool_exception(
22120f38712SPatrick Williams            "Failed to fetch PEL ID for required SRC : " + str(e)
22220f38712SPatrick Williams        )
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))
2416e0e0919SSridevi Ramesh    except Exception as e:
2426e0e0919SSridevi Ramesh        raise peltool_exception("Failed to fetch all SRCs : " + str(e))
2436e0e0919SSridevi Ramesh    return src_id
2446bf23630SSridevi Ramesh
2456bf23630SSridevi Ramesh
24620f38712SPatrick Williamsdef check_for_unexpected_src(
24720f38712SPatrick Williams    unexpected_src_list=[], include_hidden_pels=False
24820f38712SPatrick Williams):
2496bf23630SSridevi Ramesh    r"""
2506bf23630SSridevi Ramesh    From the given unexpected SRC list, check if any unexpected SRC created
2516bf23630SSridevi Ramesh    on the BMC. Returns 0 if no SRC found else throws exception.
2526bf23630SSridevi Ramesh
2536bf23630SSridevi Ramesh    Description of arguments:
2546bf23630SSridevi Ramesh    unexpected_src_list       Give unexpected SRCs in the list format.
2556bf23630SSridevi Ramesh                              e.g.: ["BBXXYYYY", "AAXXYYYY"].
256d1cb3252SSridevi Ramesh
257d1cb3252SSridevi Ramesh    include_hidden_pels       True/False (default: False).
258d1cb3252SSridevi Ramesh                              Set True to get hidden PELs else False.
2596bf23630SSridevi Ramesh    """
2606bf23630SSridevi Ramesh    try:
2616bf23630SSridevi Ramesh        unexpected_src_count = 0
2626bf23630SSridevi Ramesh        if not unexpected_src_list:
2636bf23630SSridevi Ramesh            print("Unexpected SRC list is empty.")
264d1cb3252SSridevi Ramesh        src_data = fetch_all_src(include_hidden_pels)
2656bf23630SSridevi Ramesh        for src in unexpected_src_list:
2666bf23630SSridevi Ramesh            if src in src_data:
2676bf23630SSridevi Ramesh                print("Found an unexpected SRC : " + src)
2686bf23630SSridevi Ramesh                unexpected_src_count = unexpected_src_count + 1
26920f38712SPatrick Williams        if unexpected_src_count >= 1:
2706bf23630SSridevi Ramesh            raise peltool_exception("Unexpected SRC found.")
2716bf23630SSridevi Ramesh
2726bf23630SSridevi Ramesh    except Exception as e:
27320f38712SPatrick Williams        raise peltool_exception(
27420f38712SPatrick Williams            "Failed to verify unexpected SRC list : " + str(e)
27520f38712SPatrick Williams        )
2766bf23630SSridevi Ramesh    return unexpected_src_count
277e8801a3fSAnusha Dathatri
278e8801a3fSAnusha Dathatri
279e8801a3fSAnusha Dathatridef filter_unexpected_srcs(expected_srcs=None):
280e8801a3fSAnusha Dathatri    r"""
281e8801a3fSAnusha Dathatri    Return list of SRCs found in BMC after filtering expected SRCs.
282e8801a3fSAnusha Dathatri    If expected_srcs is None then all SRCs found in system are returned.
283e8801a3fSAnusha Dathatri
284e8801a3fSAnusha Dathatri    Description of arguments:
285e8801a3fSAnusha Dathatri    expected_srcs       List of expected SRCs. E.g. ["BBXXYYYY", "AAXXYYYY"].
286e8801a3fSAnusha Dathatri    """
287e8801a3fSAnusha Dathatri
288e8801a3fSAnusha Dathatri    srcs_found = fetch_all_src()
289e8801a3fSAnusha Dathatri    if not expected_srcs:
290e8801a3fSAnusha Dathatri        expected_srcs = []
291e8801a3fSAnusha Dathatri    print(expected_srcs)
292e8801a3fSAnusha Dathatri    return list(set(srcs_found) - set(expected_srcs))
2935636ad12SAnusha Dathatri
2945636ad12SAnusha Dathatri
2955636ad12SAnusha Dathatridef get_bmc_event_log_id_for_pel(pel_id):
2965636ad12SAnusha Dathatri    r"""
2975636ad12SAnusha Dathatri    Return BMC event log ID for the given PEL ID.
2985636ad12SAnusha Dathatri
2995636ad12SAnusha Dathatri    Description of arguments:
3005636ad12SAnusha Dathatri    pel_id       PEL ID. E.g. 0x50000021.
3015636ad12SAnusha Dathatri    """
3025636ad12SAnusha Dathatri
3035636ad12SAnusha Dathatri    pel_data = peltool("-i " + pel_id)
3045636ad12SAnusha Dathatri    print(pel_data)
3055636ad12SAnusha Dathatri    bmc_id_for_pel = pel_data["Private Header"]["BMC Event Log Id"]
3065636ad12SAnusha Dathatri    return bmc_id_for_pel
307