xref: /openbmc/openbmc-test-automation/lib/pel_utils.py (revision 37c58c8c6340dfa25ae94eccf528751ee97f811a)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2faa5d20aSRahul Maheshwari
3faa5d20aSRahul Maheshwarir"""
4faa5d20aSRahul MaheshwariPEL functions.
5faa5d20aSRahul Maheshwari"""
6faa5d20aSRahul Maheshwari
7e635ddc0SGeorge Keishingimport func_args as fa
8e635ddc0SGeorge Keishingimport bmc_ssh_utils as bsu
9faa5d20aSRahul Maheshwariimport json
1048ffa2c2SGeorge Keishingimport os
1148ffa2c2SGeorge Keishingimport sys
1248ffa2c2SGeorge Keishingfrom robot.libraries.BuiltIn import BuiltIn
1348ffa2c2SGeorge Keishing
1448ffa2c2SGeorge Keishingbase_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1548ffa2c2SGeorge Keishingsys.path.append(base_path + "/data/")
1648ffa2c2SGeorge Keishing
17*37c58c8cSGeorge Keishingimport pel_variables
18*37c58c8cSGeorge Keishing
19faa5d20aSRahul Maheshwari
206e0e0919SSridevi Rameshclass peltool_exception(Exception):
216e0e0919SSridevi Ramesh    r"""
226e0e0919SSridevi Ramesh    Base class for peltool related exceptions.
236e0e0919SSridevi Ramesh    """
2448ffa2c2SGeorge Keishing
256e0e0919SSridevi Ramesh    def __init__(self, message):
266e0e0919SSridevi Ramesh        self.message = message
276e0e0919SSridevi Ramesh        super().__init__(self.message)
286e0e0919SSridevi Ramesh
296e0e0919SSridevi Ramesh
30a20876d3SMichael Walshdef peltool(option_string, parse_json=True, **bsu_options):
31faa5d20aSRahul Maheshwari    r"""
32faa5d20aSRahul Maheshwari    Run peltool on the BMC with the caller's option string and return the result.
33faa5d20aSRahul Maheshwari
34faa5d20aSRahul Maheshwari    Example:
35faa5d20aSRahul Maheshwari
36faa5d20aSRahul Maheshwari    ${pel_results}=  Peltool  -l
37faa5d20aSRahul Maheshwari    Rprint Vars  pel_results
38faa5d20aSRahul Maheshwari
39faa5d20aSRahul Maheshwari    pel_results:
40faa5d20aSRahul Maheshwari      [0x50000031]:
41faa5d20aSRahul Maheshwari        [CompID]:                       0x1000
42faa5d20aSRahul Maheshwari        [PLID]:                         0x50000031
43faa5d20aSRahul Maheshwari        [Subsystem]:                    BMC Firmware
44faa5d20aSRahul Maheshwari        [Message]:                      An application had an internal failure
45faa5d20aSRahul Maheshwari        [SRC]:                          BD8D1002
46faa5d20aSRahul Maheshwari        [Commit Time]:                  02/25/2020  04:51:31
47faa5d20aSRahul Maheshwari        [Sev]:                          Unrecoverable Error
48faa5d20aSRahul Maheshwari        [CreatorID]:                    BMC
49faa5d20aSRahul Maheshwari
50faa5d20aSRahul Maheshwari    Description of argument(s):
51faa5d20aSRahul Maheshwari    option_string                   A string of options which are to be processed by the peltool command.
52a20876d3SMichael Walsh    parse_json                      Indicates that the raw JSON data should parsed into a list of
53a20876d3SMichael Walsh                                    dictionaries.
54faa5d20aSRahul Maheshwari    bsu_options                     Options to be passed directly to bmc_execute_command. See its prolog for
55faa5d20aSRahul Maheshwari                                    details.
56faa5d20aSRahul Maheshwari    """
57faa5d20aSRahul Maheshwari
58faa5d20aSRahul Maheshwari    bsu_options = fa.args_to_objects(bsu_options)
59e635ddc0SGeorge Keishing    out_buf, stderr, rc = bsu.bmc_execute_command('peltool ' + option_string, **bsu_options)
60a20876d3SMichael Walsh    if parse_json:
61a20876d3SMichael Walsh        try:
62a20876d3SMichael Walsh            return json.loads(out_buf)
63c6dd799dSSridevi Ramesh        except ValueError:
64a20876d3SMichael Walsh            return {}
65faa5d20aSRahul Maheshwari    return out_buf
666e0e0919SSridevi Ramesh
676e0e0919SSridevi Ramesh
68e635ddc0SGeorge Keishingdef get_pel_data_from_bmc(include_hidden_pels=False,
69e635ddc0SGeorge Keishing                          include_informational_pels=False):
702930050aSSridevi Ramesh    r"""
712930050aSSridevi Ramesh    Returns PEL data from BMC else throws exception.
72d1cb3252SSridevi Ramesh
73d1cb3252SSridevi Ramesh    Description of arguments:
74d1cb3252SSridevi Ramesh    include_hidden_pels           True/False (default: False).
75d1cb3252SSridevi Ramesh                                  Set True to get hidden PELs else False.
76d1cb3252SSridevi Ramesh    include_informational_pels    True/False (default: False).
77d1cb3252SSridevi Ramesh                                  Set True to get informational PELs else False.
782930050aSSridevi Ramesh    """
792930050aSSridevi Ramesh    try:
80d1cb3252SSridevi Ramesh        pel_cmd = " -l"
81d1cb3252SSridevi Ramesh        if include_hidden_pels:
82d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -h"
83d1cb3252SSridevi Ramesh        if include_informational_pels:
84d1cb3252SSridevi Ramesh            pel_cmd = pel_cmd + " -f"
85d1cb3252SSridevi Ramesh        pel_data = peltool(pel_cmd)
862930050aSSridevi Ramesh        if not pel_data:
872930050aSSridevi Ramesh            print("No PEL data present in BMC ...")
882930050aSSridevi Ramesh    except Exception as e:
892930050aSSridevi Ramesh        raise peltool_exception("Failed to get PEL data from BMC : " + str(e))
902930050aSSridevi Ramesh    return pel_data
912930050aSSridevi Ramesh
922930050aSSridevi Ramesh
93d1cb3252SSridevi Rameshdef fetch_all_pel_ids_for_src(src_id, severity, include_hidden_pels=False):
946e0e0919SSridevi Ramesh    r"""
95c6dd799dSSridevi Ramesh    Fetch all PEL IDs for the input SRC ID based on the severity type
96c6dd799dSSridevi Ramesh    in the list format.
976e0e0919SSridevi Ramesh
986e0e0919SSridevi Ramesh    Description of arguments:
99d1cb3252SSridevi Ramesh    src_id                SRC ID (e.g. BCXXYYYY).
100c6dd799dSSridevi Ramesh    severity              PEL severity (e.g. "Predictive Error"
101c6dd799dSSridevi Ramesh                                             "Recovered Error").
102d1cb3252SSridevi Ramesh    include_hidden_pels   True/False (default: False).
103d1cb3252SSridevi Ramesh                          Set True to get hidden PELs else False.
1046e0e0919SSridevi Ramesh    """
1056e0e0919SSridevi Ramesh
1066e0e0919SSridevi Ramesh    try:
107c6dd799dSSridevi Ramesh        src_pel_ids = []
108d1cb3252SSridevi Ramesh        pel_data = get_pel_data_from_bmc(include_hidden_pels)
1096e0e0919SSridevi Ramesh        pel_id_list = pel_data.keys()
110c6dd799dSSridevi Ramesh        for pel_id in pel_id_list:
111c6dd799dSSridevi Ramesh            # Check if required SRC ID with severity is present
112e635ddc0SGeorge Keishing            if ((pel_data[pel_id]["SRC"] == src_id) and (pel_data[pel_id]["Sev"] == severity)):
113c6dd799dSSridevi Ramesh                src_pel_ids.append(pel_id)
114c6dd799dSSridevi Ramesh
115c6dd799dSSridevi Ramesh        if not src_pel_ids:
116e635ddc0SGeorge Keishing            raise peltool_exception(src_id + " with severity " + severity + " not present")
1176e0e0919SSridevi Ramesh    except Exception as e:
118e635ddc0SGeorge Keishing        raise peltool_exception("Failed to fetch PEL ID for required SRC : " + str(e))
119c6dd799dSSridevi Ramesh    return src_pel_ids
1206e0e0919SSridevi Ramesh
1216e0e0919SSridevi Ramesh
122d1cb3252SSridevi Rameshdef fetch_all_src(include_hidden_pels=False):
1236e0e0919SSridevi Ramesh    r"""
124c6dd799dSSridevi Ramesh    Fetch all SRC IDs from peltool in the list format.
125d1cb3252SSridevi Ramesh
126d1cb3252SSridevi Ramesh    include_hidden_pels       True/False (default: False).
127d1cb3252SSridevi Ramesh                              Set True to get hidden PELs else False.
1286e0e0919SSridevi Ramesh    """
1296e0e0919SSridevi Ramesh    try:
130c6dd799dSSridevi Ramesh        src_id = []
131d1cb3252SSridevi Ramesh        pel_data = get_pel_data_from_bmc(include_hidden_pels)
132c6dd799dSSridevi Ramesh        if pel_data:
1336e0e0919SSridevi Ramesh            pel_id_list = pel_data.keys()
134c6dd799dSSridevi Ramesh            for pel_id in pel_id_list:
135c6dd799dSSridevi Ramesh                src_id.append(pel_data[pel_id]["SRC"])
1366bf23630SSridevi Ramesh        print("SRC IDs: " + str(src_id))
1376e0e0919SSridevi Ramesh    except Exception as e:
1386e0e0919SSridevi Ramesh        raise peltool_exception("Failed to fetch all SRCs : " + str(e))
1396e0e0919SSridevi Ramesh    return src_id
1406bf23630SSridevi Ramesh
1416bf23630SSridevi Ramesh
142e635ddc0SGeorge Keishingdef check_for_unexpected_src(unexpected_src_list=[], include_hidden_pels=False):
1436bf23630SSridevi Ramesh    r"""
1446bf23630SSridevi Ramesh    From the given unexpected SRC list, check if any unexpected SRC created
1456bf23630SSridevi Ramesh    on the BMC. Returns 0 if no SRC found else throws exception.
1466bf23630SSridevi Ramesh
1476bf23630SSridevi Ramesh    Description of arguments:
1486bf23630SSridevi Ramesh    unexpected_src_list       Give unexpected SRCs in the list format.
1496bf23630SSridevi Ramesh                              e.g.: ["BBXXYYYY", "AAXXYYYY"].
150d1cb3252SSridevi Ramesh
151d1cb3252SSridevi Ramesh    include_hidden_pels       True/False (default: False).
152d1cb3252SSridevi Ramesh                              Set True to get hidden PELs else False.
1536bf23630SSridevi Ramesh    """
1546bf23630SSridevi Ramesh    try:
1556bf23630SSridevi Ramesh        unexpected_src_count = 0
1566bf23630SSridevi Ramesh        if not unexpected_src_list:
1576bf23630SSridevi Ramesh            print("Unexpected SRC list is empty.")
158d1cb3252SSridevi Ramesh        src_data = fetch_all_src(include_hidden_pels)
1596bf23630SSridevi Ramesh        for src in unexpected_src_list:
1606bf23630SSridevi Ramesh            if src in src_data:
1616bf23630SSridevi Ramesh                print("Found an unexpected SRC : " + src)
1626bf23630SSridevi Ramesh                unexpected_src_count = unexpected_src_count + 1
163e635ddc0SGeorge Keishing        if (unexpected_src_count >= 1):
1646bf23630SSridevi Ramesh            raise peltool_exception("Unexpected SRC found.")
1656bf23630SSridevi Ramesh
1666bf23630SSridevi Ramesh    except Exception as e:
167e635ddc0SGeorge Keishing        raise peltool_exception("Failed to verify unexpected SRC list : " + str(e))
1686bf23630SSridevi Ramesh    return unexpected_src_count
169e8801a3fSAnusha Dathatri
170e8801a3fSAnusha Dathatri
171e8801a3fSAnusha Dathatridef filter_unexpected_srcs(expected_srcs=None):
172e8801a3fSAnusha Dathatri    r"""
173e8801a3fSAnusha Dathatri    Return list of SRCs found in BMC after filtering expected SRCs.
174e8801a3fSAnusha Dathatri    If expected_srcs is None then all SRCs found in system are returned.
175e8801a3fSAnusha Dathatri
176e8801a3fSAnusha Dathatri    Description of arguments:
177e8801a3fSAnusha Dathatri    expected_srcs       List of expected SRCs. E.g. ["BBXXYYYY", "AAXXYYYY"].
178e8801a3fSAnusha Dathatri    """
179e8801a3fSAnusha Dathatri
180e8801a3fSAnusha Dathatri    srcs_found = fetch_all_src()
181e8801a3fSAnusha Dathatri    if not expected_srcs:
182e8801a3fSAnusha Dathatri        expected_srcs = []
183e8801a3fSAnusha Dathatri    print(expected_srcs)
184e8801a3fSAnusha Dathatri    return list(set(srcs_found) - set(expected_srcs))
1855636ad12SAnusha Dathatri
1865636ad12SAnusha Dathatri
1875636ad12SAnusha Dathatridef get_bmc_event_log_id_for_pel(pel_id):
1885636ad12SAnusha Dathatri    r"""
1895636ad12SAnusha Dathatri    Return BMC event log ID for the given PEL ID.
1905636ad12SAnusha Dathatri
1915636ad12SAnusha Dathatri    Description of arguments:
1925636ad12SAnusha Dathatri    pel_id       PEL ID. E.g. 0x50000021.
1935636ad12SAnusha Dathatri    """
1945636ad12SAnusha Dathatri
1955636ad12SAnusha Dathatri    pel_data = peltool("-i " + pel_id)
1965636ad12SAnusha Dathatri    print(pel_data)
1975636ad12SAnusha Dathatri    bmc_id_for_pel = pel_data["Private Header"]["BMC Event Log Id"]
1985636ad12SAnusha Dathatri    return bmc_id_for_pel
199