xref: /openbmc/openbmc-test-automation/lib/logging_utils.py (revision 139f1da69baea53bab830baafcbc75d59f51459b)
1#!/usr/bin/env python3
2
3r"""
4Provide useful error log utility keywords.
5"""
6
7import importlib.util
8import os
9import sys
10
11import gen_print as gp
12from robot.libraries.BuiltIn import BuiltIn
13
14base_path = (
15    os.path.dirname(
16        os.path.dirname(importlib.util.find_spec("gen_robot_print").origin)
17    )
18    + os.sep
19)
20sys.path.append(base_path + "data/")
21import gen_robot_utils as gru  # NOQA
22import variables as var  # NOQA
23
24gru.my_import_resource("logging_utils.robot")
25
26
27redfish_support_trans_state = int(
28    os.environ.get("REDFISH_SUPPORT_TRANS_STATE", 0)
29) or int(
30    BuiltIn().get_variable_value("${REDFISH_SUPPORT_TRANS_STATE}", default=0)
31)
32
33
34def print_error_logs(error_logs, key_list=None):
35    r"""
36    Print the error logs to the console screen.
37
38    This function provides the following benefits:
39    - It will specify print_var parms for the caller (e.g. hex=1).
40    - It is much easier to call this function than to generate the desired code
41      directly from a robot script.
42
43    Description of argument(s):
44    error_logs                      An error log dictionary such as the one
45                                    returned by the 'Get Error Logs' keyword.
46    key_list                        The list of keys to be printed.  This may
47                                    be specified as either a python list
48                                    or a space-delimited string.  In the
49                                    latter case, this function will convert
50                                    it to a python list. See the sprint_varx
51                                    function prolog for additionatl details.
52
53    Example use from a python script:
54
55    ${error_logs}=  Get Error Logs
56    Print Error Logs  ${error_logs}  Message Timestamp
57
58    Sample output:
59
60    error_logs:
61      [/xyz/openbmc_project/logging/entry/3]:
62        [Timestamp]:                                  1521738335735
63        [Message]:
64        xyz.openbmc_project.Inventory.Error.Nonfunctional
65      [/xyz/openbmc_project/logging/entry/2]:
66        [Timestamp]:                                  1521738334637
67        [Message]:
68        xyz.openbmc_project.Inventory.Error.Nonfunctional
69      [/xyz/openbmc_project/logging/entry/1]:
70        [Timestamp]:                                  1521738300696
71        [Message]:
72        xyz.openbmc_project.Inventory.Error.Nonfunctional
73      [/xyz/openbmc_project/logging/entry/4]:
74        [Timestamp]:                                  1521738337915
75        [Message]:
76        xyz.openbmc_project.Inventory.Error.Nonfunctional
77
78    Another example call using a robot list:
79    ${error_logs}=  Get Error Logs
80    ${key_list}=  Create List  Message  Timestamp  Severity
81    Print Error Logs  ${error_logs}  ${key_list}
82    """
83
84    if key_list is not None:
85        try:
86            key_list = key_list.split(" ")
87        except AttributeError:
88            pass
89        if redfish_support_trans_state:
90            key_list.insert(0, var.REDFISH_BMC_LOGGING_ENTRY + ".*")
91        else:
92            key_list.insert(0, var.BMC_LOGGING_ENTRY + ".*")
93
94    gp.print_var(error_logs, key_list=key_list)
95
96
97def get_esels(error_logs=None):
98    r"""
99    Get all available extended Service Event Logs (eSELs) and return as a list.
100
101    Example robot code:
102    ${esels}=  Get Esels
103    Rprint Vars  esels
104
105    Example output (excerpt):
106    esels:
107      esels[0]:                  ESEL=00 00 df 00 00...
108      esels[1]:                  ESEL=00 00 df 00 00...
109
110    Description of argument(s):
111    error_logs                      The error_log data, which can be obtained
112                                    from 'Get Error Logs'.  If this value is
113                                    None, then this function will call 'Get
114                                    Error Logs' on the caller's behalf.
115    """
116
117    if error_logs is None:
118        error_logs = BuiltIn().run_keyword("Get Error Logs")
119
120    # Look for any error log entries containing the 'AdditionalData' field
121    # which in turn has an entry starting with "ESEL=".  Here is an excerpt of
122    # the error_logs that contains such an entry.
123    # error_logs:
124    #   [/xyz/openbmc_project/logging/entry/1]:
125    #     [AdditionalData]:
126    #       [AdditionalData][0]:   CALLOUT_INVENTORY_PATH=/xyz/openbmc_project/inventory/system/chassis/mot...
127    #       [AdditionalData][1]:   ESEL=00 00 df 00 00 00 00 20 00 04...
128    esels = []
129    for error_log in error_logs.values():
130        if "AdditionalData" in error_log:
131            for additional_data in error_log["AdditionalData"]:
132                if additional_data.startswith("ESEL="):
133                    esels.append(additional_data)
134
135    return esels
136