1#!/usr/bin/env python3
2
3from gen_arg import *
4from gen_call_robot import *
5from gen_cmd import *
6from gen_misc import *
7from gen_plug_in_utils import *
8from gen_print import *
9from gen_valid import *
10from var_funcs import *
11
12# Set exit_on_error for gen_valid functions.
13set_exit_on_error(True)
14ignore_err = 0
15
16
17parser = argparse.ArgumentParser(
18    usage="%(prog)s [OPTIONS]",
19    description="%(prog)s will calculate the value of num_err_logs and"
20    + " save it as a plug-in value for the benefit of the FFDC plug-in."
21    + "  The FFDC plug-in can use that data to decide whether to collect"
22    + " FFDC data.",
23    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
24    prefix_chars="-+",
25)
26
27# The stock_list will be passed to gen_get_options.  We populate it with the names of stock parm options we
28# want.  These stock parms are pre-defined by gen_get_options.
29stock_list = [
30    ("test_mode", 0),
31    ("quiet", get_plug_default("quiet", 0)),
32    ("debug", get_plug_default("debug", 0)),
33]
34
35
36def exit_function(signal_number=0, frame=None):
37    r"""
38    Execute whenever the program ends normally or with the signals that we catch (i.e. TERM, INT).
39
40    This function will be called by gen_exit_function().
41    """
42
43    process_robot_output_files()
44
45
46def validate_parms():
47    r"""
48    Validate program parameters, etc.
49
50    This function will be called by gen_setup().
51    """
52
53    get_plug_vars()
54
55    global AUTOSCRIPT_STATUS_FILE_PATH
56    # AUTOSCRIPT_STATUS_FILE_PATH is set when we're called by autoscript.  For this program to work
57    # correctly, it must be called with autoscript.
58    AUTOSCRIPT_STATUS_FILE_PATH = os.environ.get(
59        "AUTOSCRIPT_STATUS_FILE_PATH", ""
60    )
61    valid_value(AUTOSCRIPT_STATUS_FILE_PATH)
62    valid_value(AUTOBOOT_OPENBMC_HOST)
63
64
65def main():
66    gen_setup()
67
68    print_plug_in_header()
69
70    # Get the number of error logs from the BMC.
71    init_robot_out_parms(get_plug_in_package_name() + "." + pgm_name + ".")
72    high_sev_elogs_file_path = (
73        AUTOBOOT_FFDC_DIR_PATH
74        + AUTOBOOT_FFDC_PREFIX
75        + "high_severity_errorlog.json"
76    )
77    lib_file_path = init_robot_file_path("lib/logging_utils.robot")
78    lib_file_path += ":" + init_robot_file_path("lib/gen_robot_print.py")
79    set_mod_global(lib_file_path)
80
81    REDFISH_SUPPORT_TRANS_STATE = int(
82        os.environ.get("REDFISH_SUPPORT_TRANS_STATE", 0)
83    )
84    if not REDFISH_SUPPORT_TRANS_STATE:
85        try:
86            from robot.libraries.BuiltIn import BuiltIn
87
88            REDFISH_SUPPORT_TRANS_STATE = int(
89                BuiltIn().get_variable_value(
90                    "${REDFISH_SUPPORT_TRANS_STATE}", default=0
91                )
92            )
93        except Exception:
94            pass
95
96    keyword_redfish_strings = [
97        (
98            "${error_logs}=  Get Redfish Event Logs "
99            " &{filter_low_severity_errlogs}"
100        ),
101        "${num_error_logs}=  Get Length  ${error_logs}",
102        "Rprint Vars  num_error_logs",
103        (
104            "${json_string}=  Evaluate  json.dumps($error_logs, indent=4) "
105            " modules=json"
106        ),
107        "Append To File  " + high_sev_elogs_file_path + "  ${json_string}",
108    ]
109
110    keyword_strings = [
111        "${error_logs}=  Get Error Logs  &{filter_low_severity_errlogs}",
112        "${num_error_logs}=  Get Length  ${error_logs}",
113        "Rprint Vars  num_error_logs",
114        (
115            "${json_string}=  Evaluate  json.dumps($error_logs, indent=4) "
116            " modules=json"
117        ),
118        "Append To File  " + high_sev_elogs_file_path + "  ${json_string}",
119    ]
120
121    if REDFISH_SUPPORT_TRANS_STATE:
122        keyword_string = " ; ".join(keyword_redfish_strings)
123    else:
124        keyword_string = " ; ".join(keyword_strings)
125
126    set_mod_global(keyword_string)
127    cmd_buf = create_robot_cmd_string(
128        "extended/run_keyword.robot",
129        OPENBMC_HOST,
130        SSH_PORT,
131        HTTPS_PORT,
132        OPENBMC_USERNAME,
133        OPENBMC_PASSWORD,
134        keyword_string,
135        lib_file_path,
136        quiet,
137        test_mode,
138        debug,
139        outputdir,
140        output,
141        log,
142        report,
143    )
144    if not robot_cmd_fnc(cmd_buf):
145        exit(1)
146    # The output contains the num_error_logs value which we will isolate with egrep.
147    rc, out_buf = shell_cmd(
148        "egrep '^num_error_logs:[ ]' " + AUTOSCRIPT_STATUS_FILE_PATH,
149        quiet=1,
150        print_output=0,
151    )
152    result = key_value_outbuf_to_dict(out_buf)
153    num_error_logs = int(result["num_error_logs"])
154    save_plug_in_value(num_error_logs)
155    if num_error_logs > 0:
156        qprint_timen(
157            "Adding the name of our high severity error logs FFDC file "
158            + "to the appropriate file list."
159        )
160        shell_cmd(
161            "echo "
162            + high_sev_elogs_file_path
163            + " > "
164            + AUTOBOOT_FFDC_LIST_FILE_PATH
165        )
166    else:
167        os.remove(high_sev_elogs_file_path)
168
169
170main()
171