xref: /openbmc/openbmc-test-automation/lib/pel_utils.py (revision 28cedb13edf062f67d9ecfb3b2fe009c7efc2794)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2faa5d20aSRahul Maheshwari
3faa5d20aSRahul Maheshwarir"""
4faa5d20aSRahul MaheshwariPEL functions.
5faa5d20aSRahul Maheshwari"""
6faa5d20aSRahul Maheshwari
7*28cedb13SSushil Singhfrom robot.libraries.BuiltIn import BuiltIn
8*28cedb13SSushil Singhfrom datetime import datetime
9faa5d20aSRahul Maheshwariimport json
1048ffa2c2SGeorge Keishingimport os
1148ffa2c2SGeorge Keishingimport sys
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
216e0e0919SSridevi Rameshclass peltool_exception(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):
52faa5d20aSRahul Maheshwari    option_string                   A string of options which are to be processed by the peltool command.
53a20876d3SMichael Walsh    parse_json                      Indicates that the raw JSON data should parsed into a list of
54a20876d3SMichael Walsh                                    dictionaries.
55faa5d20aSRahul Maheshwari    bsu_options                     Options to be passed directly to bmc_execute_command. See its prolog for
56faa5d20aSRahul Maheshwari                                    details.
57faa5d20aSRahul Maheshwari    """
58faa5d20aSRahul Maheshwari
59faa5d20aSRahul Maheshwari    bsu_options = fa.args_to_objects(bsu_options)
6020f38712SPatrick Williams    out_buf, stderr, rc = bsu.bmc_execute_command(
6120f38712SPatrick Williams        "peltool " + option_string, **bsu_options
6220f38712SPatrick Williams    )
63a20876d3SMichael Walsh    if parse_json:
64a20876d3SMichael Walsh        try:
65a20876d3SMichael Walsh            return json.loads(out_buf)
66c6dd799dSSridevi Ramesh        except ValueError:
67a20876d3SMichael Walsh            return {}
68faa5d20aSRahul Maheshwari    return out_buf
696e0e0919SSridevi Ramesh
706e0e0919SSridevi Ramesh
7120f38712SPatrick Williamsdef get_pel_data_from_bmc(
7220f38712SPatrick Williams    include_hidden_pels=False, include_informational_pels=False
7320f38712SPatrick Williams):
742930050aSSridevi Ramesh    r"""
752930050aSSridevi Ramesh    Returns PEL data from BMC else throws exception.
76d1cb3252SSridevi Ramesh
77d1cb3252SSridevi Ramesh    Description of arguments:
78d1cb3252SSridevi Ramesh    include_hidden_pels           True/False (default: False).
79d1cb3252SSridevi Ramesh                                  Set True to get hidden PELs else False.
80d1cb3252SSridevi Ramesh    include_informational_pels    True/False (default: False).
81d1cb3252SSridevi Ramesh                                  Set True to get informational PELs else False.
822930050aSSridevi Ramesh    """
832930050aSSridevi Ramesh    try:
84d1cb3252SSridevi Ramesh        pel_cmd = " -l"
85d1cb3252SSridevi Ramesh        if include_hidden_pels:
86d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -h"
87d1cb3252SSridevi Ramesh        if include_informational_pels:
88d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -f"
89d1cb3252SSridevi Ramesh        pel_data = peltool(pel_cmd)
902930050aSSridevi Ramesh        if not pel_data:
912930050aSSridevi Ramesh            print("No PEL data present in BMC ...")
922930050aSSridevi Ramesh    except Exception as e:
932930050aSSridevi Ramesh        raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
942930050aSSridevi Ramesh    return pel_data
952930050aSSridevi Ramesh
962930050aSSridevi Ramesh
97*28cedb13SSushil Singhdef verify_no_pel_exists_on_bmc():
98*28cedb13SSushil Singh    r"""
99*28cedb13SSushil Singh    Verify that no PEL exists in BMC. Raise an exception if it does.
100*28cedb13SSushil Singh    """
101*28cedb13SSushil Singh
102*28cedb13SSushil Singh    try:
103*28cedb13SSushil Singh        pel_data = get_pel_data_from_bmc()
104*28cedb13SSushil Singh
105*28cedb13SSushil Singh        if len(pel_data) == 0:
106*28cedb13SSushil Singh            return True
107*28cedb13SSushil Singh        else:
108*28cedb13SSushil Singh            print("PEL data present. \n", pel_data)
109*28cedb13SSushil Singh            raise peltool_exception("PEL data present in BMC")
110*28cedb13SSushil Singh    except Exception as e:
111*28cedb13SSushil Singh        raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
112*28cedb13SSushil Singh
113*28cedb13SSushil Singh
114*28cedb13SSushil Singhdef compare_pel_and_redfish_event_log(pel_record, event_record):
115*28cedb13SSushil Singh    r"""
116*28cedb13SSushil Singh    Compare PEL log attributes like "SRC", "Created at" with Redfish
117*28cedb13SSushil Singh    event log attributes like "EventId", "Created".
118*28cedb13SSushil Singh    Return False if they do not match.
119*28cedb13SSushil Singh
120*28cedb13SSushil Singh    Description of arguments:
121*28cedb13SSushil Singh    pel_record     PEL record.
122*28cedb13SSushil Singh    event_record   Redfish event which is equivalent of PEL record.
123*28cedb13SSushil Singh    """
124*28cedb13SSushil Singh
125*28cedb13SSushil Singh    try:
126*28cedb13SSushil Singh        # Below is format of PEL record / event record and following
127*28cedb13SSushil Singh        # i.e. "SRC", "Created at" from
128*28cedb13SSushil Singh        # PEL record is compared with "EventId", "Created" from event record.
129*28cedb13SSushil Singh
130*28cedb13SSushil Singh        # PEL Log attributes
131*28cedb13SSushil Singh        # SRC        : XXXXXXXX
132*28cedb13SSushil Singh        # Created at : 11/14/2022 12:38:04
133*28cedb13SSushil Singh
134*28cedb13SSushil Singh        # Event log attributes
135*28cedb13SSushil Singh        # EventId : XXXXXXXX XXXXXXXX XXXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
136*28cedb13SSushil Singh
137*28cedb13SSushil Singh        # Created : 2022-11-14T12:38:04+00:00
138*28cedb13SSushil Singh
139*28cedb13SSushil Singh        print("\nPEL records : {0}".format(pel_record))
140*28cedb13SSushil Singh        print("\nEvent records : {0}".format(event_record))
141*28cedb13SSushil Singh
142*28cedb13SSushil Singh        pel_src = pel_record['pel_data']['SRC']
143*28cedb13SSushil Singh        pel_created_time = \
144*28cedb13SSushil Singh            pel_record['pel_detail_data']['Private Header']['Created at']
145*28cedb13SSushil Singh
146*28cedb13SSushil Singh        event_ids = (event_record['EventId']).split(' ')
147*28cedb13SSushil Singh
148*28cedb13SSushil Singh        event_time_format = (event_record['Created']).split('T')
149*28cedb13SSushil Singh        event_date = (event_time_format[0]).split('-')
150*28cedb13SSushil Singh        event_date = \
151*28cedb13SSushil Singh            datetime(int(event_date[0]), int(event_date[1]), int(event_date[2]))
152*28cedb13SSushil Singh        event_date = event_date.strftime("%m/%d/%Y")
153*28cedb13SSushil Singh        event_sub_time_format = (event_time_format[1]).split('+')
154*28cedb13SSushil Singh        event_date_time = event_date + " " + event_sub_time_format[0]
155*28cedb13SSushil Singh
156*28cedb13SSushil Singh        event_created_time = event_date_time.replace('-', '/')
157*28cedb13SSushil Singh
158*28cedb13SSushil Singh        print("\nPEL SRC : {0} | PEL Created Time : {1}".
159*28cedb13SSushil Singh              format(pel_src, pel_created_time))
160*28cedb13SSushil Singh        print("\nError event ID : {0} | Error Log Created Time : {1}".
161*28cedb13SSushil Singh              format(event_ids[0], event_created_time))
162*28cedb13SSushil Singh
163*28cedb13SSushil Singh        if pel_src == event_ids[0] and pel_created_time == event_created_time:
164*28cedb13SSushil Singh            print("\nPEL SRC and created date time match with "
165*28cedb13SSushil Singh                  "event ID, created time")
166*28cedb13SSushil Singh        else:
167*28cedb13SSushil Singh            raise peltool_exception("\nPEL SRC and created date time did not "
168*28cedb13SSushil Singh                                    "match with event ID, created time")
169*28cedb13SSushil Singh    except Exception as e:
170*28cedb13SSushil Singh        raise peltool_exception("Exception occured during PEL and Event log "
171*28cedb13SSushil Singh                                "comparison for SRC or event ID and created "
172*28cedb13SSushil Singh                                "time : " + str(e))
173*28cedb13SSushil Singh
174*28cedb13SSushil Singh
175d1cb3252SSridevi Rameshdef fetch_all_pel_ids_for_src(src_id, severity, include_hidden_pels=False):
1766e0e0919SSridevi Ramesh    r"""
177c6dd799dSSridevi Ramesh    Fetch all PEL IDs for the input SRC ID based on the severity type
178c6dd799dSSridevi Ramesh    in the list format.
1796e0e0919SSridevi Ramesh
1806e0e0919SSridevi Ramesh    Description of arguments:
181d1cb3252SSridevi Ramesh    src_id                SRC ID (e.g. BCXXYYYY).
182c6dd799dSSridevi Ramesh    severity              PEL severity (e.g. "Predictive Error"
183c6dd799dSSridevi Ramesh                                             "Recovered Error").
184d1cb3252SSridevi Ramesh    include_hidden_pels   True/False (default: False).
185d1cb3252SSridevi Ramesh                          Set True to get hidden PELs else False.
1866e0e0919SSridevi Ramesh    """
1876e0e0919SSridevi Ramesh
1886e0e0919SSridevi Ramesh    try:
189c6dd799dSSridevi Ramesh        src_pel_ids = []
190d1cb3252SSridevi Ramesh        pel_data = get_pel_data_from_bmc(include_hidden_pels)
1916e0e0919SSridevi Ramesh        pel_id_list = pel_data.keys()
192c6dd799dSSridevi Ramesh        for pel_id in pel_id_list:
193c6dd799dSSridevi Ramesh            # Check if required SRC ID with severity is present
19420f38712SPatrick Williams            if (pel_data[pel_id]["SRC"] == src_id) and (
19520f38712SPatrick Williams                pel_data[pel_id]["Sev"] == severity
19620f38712SPatrick Williams            ):
197c6dd799dSSridevi Ramesh                src_pel_ids.append(pel_id)
198c6dd799dSSridevi Ramesh
199c6dd799dSSridevi Ramesh        if not src_pel_ids:
20020f38712SPatrick Williams            raise peltool_exception(
20120f38712SPatrick Williams                src_id + " with severity " + severity + " not present"
20220f38712SPatrick Williams            )
2036e0e0919SSridevi Ramesh    except Exception as e:
20420f38712SPatrick Williams        raise peltool_exception(
20520f38712SPatrick Williams            "Failed to fetch PEL ID for required SRC : " + str(e)
20620f38712SPatrick Williams        )
207c6dd799dSSridevi Ramesh    return src_pel_ids
2086e0e0919SSridevi Ramesh
2096e0e0919SSridevi Ramesh
210d1cb3252SSridevi Rameshdef fetch_all_src(include_hidden_pels=False):
2116e0e0919SSridevi Ramesh    r"""
212c6dd799dSSridevi Ramesh    Fetch all SRC IDs from peltool in the list format.
213d1cb3252SSridevi Ramesh
214d1cb3252SSridevi Ramesh    include_hidden_pels       True/False (default: False).
215d1cb3252SSridevi Ramesh                              Set True to get hidden PELs else False.
2166e0e0919SSridevi Ramesh    """
2176e0e0919SSridevi Ramesh    try:
218c6dd799dSSridevi Ramesh        src_id = []
219d1cb3252SSridevi Ramesh        pel_data = get_pel_data_from_bmc(include_hidden_pels)
220c6dd799dSSridevi Ramesh        if pel_data:
2216e0e0919SSridevi Ramesh            pel_id_list = pel_data.keys()
222c6dd799dSSridevi Ramesh            for pel_id in pel_id_list:
223c6dd799dSSridevi Ramesh                src_id.append(pel_data[pel_id]["SRC"])
2246bf23630SSridevi Ramesh        print("SRC IDs: " + str(src_id))
2256e0e0919SSridevi Ramesh    except Exception as e:
2266e0e0919SSridevi Ramesh        raise peltool_exception("Failed to fetch all SRCs : " + str(e))
2276e0e0919SSridevi Ramesh    return src_id
2286bf23630SSridevi Ramesh
2296bf23630SSridevi Ramesh
23020f38712SPatrick Williamsdef check_for_unexpected_src(
23120f38712SPatrick Williams    unexpected_src_list=[], include_hidden_pels=False
23220f38712SPatrick Williams):
2336bf23630SSridevi Ramesh    r"""
2346bf23630SSridevi Ramesh    From the given unexpected SRC list, check if any unexpected SRC created
2356bf23630SSridevi Ramesh    on the BMC. Returns 0 if no SRC found else throws exception.
2366bf23630SSridevi Ramesh
2376bf23630SSridevi Ramesh    Description of arguments:
2386bf23630SSridevi Ramesh    unexpected_src_list       Give unexpected SRCs in the list format.
2396bf23630SSridevi Ramesh                              e.g.: ["BBXXYYYY", "AAXXYYYY"].
240d1cb3252SSridevi Ramesh
241d1cb3252SSridevi Ramesh    include_hidden_pels       True/False (default: False).
242d1cb3252SSridevi Ramesh                              Set True to get hidden PELs else False.
2436bf23630SSridevi Ramesh    """
2446bf23630SSridevi Ramesh    try:
2456bf23630SSridevi Ramesh        unexpected_src_count = 0
2466bf23630SSridevi Ramesh        if not unexpected_src_list:
2476bf23630SSridevi Ramesh            print("Unexpected SRC list is empty.")
248d1cb3252SSridevi Ramesh        src_data = fetch_all_src(include_hidden_pels)
2496bf23630SSridevi Ramesh        for src in unexpected_src_list:
2506bf23630SSridevi Ramesh            if src in src_data:
2516bf23630SSridevi Ramesh                print("Found an unexpected SRC : " + src)
2526bf23630SSridevi Ramesh                unexpected_src_count = unexpected_src_count + 1
25320f38712SPatrick Williams        if unexpected_src_count >= 1:
2546bf23630SSridevi Ramesh            raise peltool_exception("Unexpected SRC found.")
2556bf23630SSridevi Ramesh
2566bf23630SSridevi Ramesh    except Exception as e:
25720f38712SPatrick Williams        raise peltool_exception(
25820f38712SPatrick Williams            "Failed to verify unexpected SRC list : " + str(e)
25920f38712SPatrick Williams        )
2606bf23630SSridevi Ramesh    return unexpected_src_count
261e8801a3fSAnusha Dathatri
262e8801a3fSAnusha Dathatri
263e8801a3fSAnusha Dathatridef filter_unexpected_srcs(expected_srcs=None):
264e8801a3fSAnusha Dathatri    r"""
265e8801a3fSAnusha Dathatri    Return list of SRCs found in BMC after filtering expected SRCs.
266e8801a3fSAnusha Dathatri    If expected_srcs is None then all SRCs found in system are returned.
267e8801a3fSAnusha Dathatri
268e8801a3fSAnusha Dathatri    Description of arguments:
269e8801a3fSAnusha Dathatri    expected_srcs       List of expected SRCs. E.g. ["BBXXYYYY", "AAXXYYYY"].
270e8801a3fSAnusha Dathatri    """
271e8801a3fSAnusha Dathatri
272e8801a3fSAnusha Dathatri    srcs_found = fetch_all_src()
273e8801a3fSAnusha Dathatri    if not expected_srcs:
274e8801a3fSAnusha Dathatri        expected_srcs = []
275e8801a3fSAnusha Dathatri    print(expected_srcs)
276e8801a3fSAnusha Dathatri    return list(set(srcs_found) - set(expected_srcs))
2775636ad12SAnusha Dathatri
2785636ad12SAnusha Dathatri
2795636ad12SAnusha Dathatridef get_bmc_event_log_id_for_pel(pel_id):
2805636ad12SAnusha Dathatri    r"""
2815636ad12SAnusha Dathatri    Return BMC event log ID for the given PEL ID.
2825636ad12SAnusha Dathatri
2835636ad12SAnusha Dathatri    Description of arguments:
2845636ad12SAnusha Dathatri    pel_id       PEL ID. E.g. 0x50000021.
2855636ad12SAnusha Dathatri    """
2865636ad12SAnusha Dathatri
2875636ad12SAnusha Dathatri    pel_data = peltool("-i " + pel_id)
2885636ad12SAnusha Dathatri    print(pel_data)
2895636ad12SAnusha Dathatri    bmc_id_for_pel = pel_data["Private Header"]["BMC Event Log Id"]
2905636ad12SAnusha Dathatri    return bmc_id_for_pel
291