xref: /openbmc/openbmc-test-automation/lib/dump_utils.py (revision 139f1da69baea53bab830baafcbc75d59f51459b)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2e11a136bSMichael Walsh
3e11a136bSMichael Walshr"""
4e11a136bSMichael WalshThis file contains functions which are useful for processing BMC dumps.
5e11a136bSMichael Walsh"""
6e11a136bSMichael Walsh
7*139f1da6SBrian Maimport importlib.util
84f3ce17dSMichael Walshimport os
9e11a136bSMichael Walshimport sys
1020f38712SPatrick Williams
1120f38712SPatrick Williamsimport bmc_ssh_utils as bsu
1220f38712SPatrick Williamsimport gen_misc as gm
1320f38712SPatrick Williamsimport gen_print as gp
1420f38712SPatrick Williamsimport gen_robot_keyword as grk
1520f38712SPatrick Williamsfrom robot.libraries.BuiltIn import BuiltIn
1620f38712SPatrick Williams
1720f38712SPatrick Williamsbase_path = (
18*139f1da6SBrian Ma    os.path.dirname(
19*139f1da6SBrian Ma        os.path.dirname(importlib.util.find_spec("gen_robot_print").origin)
20*139f1da6SBrian Ma    )
2120f38712SPatrick Williams    + os.sep
2220f38712SPatrick Williams)
23e11a136bSMichael Walshsys.path.append(base_path + "data/")
2409679890SGeorge Keishingimport variables as var  # NOQA
25e11a136bSMichael Walsh
26e11a136bSMichael Walsh
27e11a136bSMichael Walshdef get_dump_dict(quiet=None):
28e11a136bSMichael Walsh    r"""
29e11a136bSMichael Walsh    Get dump information and return as an ordered dictionary where the keys
30e11a136bSMichael Walsh    are the dump IDs and the values are the full path names of the dumps.
31e11a136bSMichael Walsh
32e11a136bSMichael Walsh    Example robot program call:
33e11a136bSMichael Walsh
34e11a136bSMichael Walsh    ${dump_dict}=  Get Dump Dict
3539c00518SMichael Walsh    Rprint Vars  dump_dict
36e11a136bSMichael Walsh
37e11a136bSMichael Walsh    Example output:
38e11a136bSMichael Walsh
39e11a136bSMichael Walsh    dump_dict:
40004ad3c9SJoy Onyerikwu      [1]:
41004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/1/obmcdump_1_1508255216.tar.xz
42004ad3c9SJoy Onyerikwu      [2]:
43004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/2/obmcdump_2_1508255245.tar.xz
44004ad3c9SJoy Onyerikwu      [3]:
45004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/3/obmcdump_3_1508255267.tar.xz
46004ad3c9SJoy Onyerikwu      [4]:
47004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/4/obmcdump_4_1508255283.tar.xz
48e11a136bSMichael Walsh
49e11a136bSMichael Walsh    Description of argument(s):
50e11a136bSMichael Walsh    quiet                           If quiet is set to 1, this function will
51e11a136bSMichael Walsh                                    NOT write status messages to stdout.
52e11a136bSMichael Walsh    """
53e11a136bSMichael Walsh
54e11a136bSMichael Walsh    quiet = int(gp.get_var_value(quiet, 1))
55bd8ec926SGeorge Keishing    cmd_buf = "find /var/lib/phosphor-debug-collector/ -maxdepth 4 -type f"
56e11a136bSMichael Walsh    output, stderr, rc = bsu.bmc_execute_command(cmd_buf, quiet=quiet)
57e11a136bSMichael Walsh
58bd8ec926SGeorge Keishing    BuiltIn().log_to_console(output)
59bd8ec926SGeorge Keishing    return output.split("\n")
60e11a136bSMichael Walsh
61e11a136bSMichael Walsh
6220f38712SPatrick Williamsdef valid_dump(dump_id, dump_dict=None, quiet=None):
63e11a136bSMichael Walsh    r"""
64e11a136bSMichael Walsh    Verify that dump_id is a valid.  If it is not valid, issue robot failure
65e11a136bSMichael Walsh    message.
66e11a136bSMichael Walsh
67e11a136bSMichael Walsh    A dump is valid if the indicated dump_id refers to an existing dump with a
68e11a136bSMichael Walsh    valid associated dump file.
69e11a136bSMichael Walsh
70e11a136bSMichael Walsh    Description of argument(s):
71e11a136bSMichael Walsh    dump_id                         A dump ID (e.g. "1", "2", etc.)
72e11a136bSMichael Walsh    dump_dict                       A dump dictionary such as the one returned
73e11a136bSMichael Walsh                                    by get_dump_dict.  If this value is None,
74e11a136bSMichael Walsh                                    this function will call get_dump_dict on
75e11a136bSMichael Walsh                                    the caller's behalf.
76e11a136bSMichael Walsh    quiet                           If quiet is set to 1, this function will
77e11a136bSMichael Walsh                                    NOT write status messages to stdout.
78e11a136bSMichael Walsh    """
79e11a136bSMichael Walsh
80e11a136bSMichael Walsh    if dump_dict is None:
81e11a136bSMichael Walsh        dump_dict = get_dump_dict(quiet=quiet)
82e11a136bSMichael Walsh
83e11a136bSMichael Walsh    if dump_id not in dump_dict:
8420f38712SPatrick Williams        message = (
8520f38712SPatrick Williams            "The specified dump ID was not found among the existing"
86e11a136bSMichael Walsh            + " dumps:\n"
8720f38712SPatrick Williams        )
88e11a136bSMichael Walsh        message += gp.sprint_var(dump_id)
89e11a136bSMichael Walsh        message += gp.sprint_var(dump_dict)
90e11a136bSMichael Walsh        BuiltIn().fail(gp.sprint_error(message))
91e11a136bSMichael Walsh
92e11a136bSMichael Walsh    if not dump_dict[dump_id].endswith("tar.xz"):
9320f38712SPatrick Williams        message = (
9420f38712SPatrick Williams            'There is no "tar.xz" file associated with the given'
95e11a136bSMichael Walsh            + " dump_id:\n"
9620f38712SPatrick Williams        )
97e11a136bSMichael Walsh        message += gp.sprint_var(dump_id)
98e11a136bSMichael Walsh        dump_file_path = dump_dict[dump_id]
99e11a136bSMichael Walsh        message += gp.sprint_var(dump_file_path)
100e11a136bSMichael Walsh        BuiltIn().fail(gp.sprint_error(message))
1014f3ce17dSMichael Walsh
1024f3ce17dSMichael Walsh
10320f38712SPatrick Williamsdef scp_dumps(targ_dir_path, targ_file_prefix="", dump_dict=None, quiet=None):
1044f3ce17dSMichael Walsh    r"""
1054f3ce17dSMichael Walsh    SCP all dumps from the BMC to the indicated directory on the local system
1064f3ce17dSMichael Walsh    and return a list of the new files.
1074f3ce17dSMichael Walsh
1084f3ce17dSMichael Walsh    Description of argument(s):
1094f3ce17dSMichael Walsh    targ_dir_path                   The path of the directory to receive the
1104f3ce17dSMichael Walsh                                    dump files.
111e16f158fSGeorge Keishing    targ_file_prefix                Prefix which will be prepended to each
1124f3ce17dSMichael Walsh                                    target file's name.
1134f3ce17dSMichael Walsh    dump_dict                       A dump dictionary such as the one returned
1144f3ce17dSMichael Walsh                                    by get_dump_dict.  If this value is None,
1154f3ce17dSMichael Walsh                                    this function will call get_dump_dict on
1164f3ce17dSMichael Walsh                                    the caller's behalf.
1174f3ce17dSMichael Walsh    quiet                           If quiet is set to 1, this function will
1184f3ce17dSMichael Walsh                                    NOT write status messages to stdout.
1194f3ce17dSMichael Walsh    """
1204f3ce17dSMichael Walsh
1214f3ce17dSMichael Walsh    targ_dir_path = gm.add_trailing_slash(targ_dir_path)
1224f3ce17dSMichael Walsh
1234f3ce17dSMichael Walsh    if dump_dict is None:
124bd8ec926SGeorge Keishing        dump_list = get_dump_dict(quiet=quiet)
1254f3ce17dSMichael Walsh
1264f3ce17dSMichael Walsh    status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)
1274f3ce17dSMichael Walsh
1284f3ce17dSMichael Walsh    dump_file_list = []
129bd8ec926SGeorge Keishing    for file_path in dump_list:
13020f38712SPatrick Williams        targ_file_path = (
13120f38712SPatrick Williams            targ_dir_path + targ_file_prefix + os.path.basename(file_path)
13220f38712SPatrick Williams        )
13320f38712SPatrick Williams        status, ret_values = grk.run_key(
13420f38712SPatrick Williams            "scp.Get File  " + file_path + "  " + targ_file_path, quiet=quiet
13520f38712SPatrick Williams        )
1361165a022SGeorge Keishing        dump_file_list.append(targ_file_path)
1371165a022SGeorge Keishing
1381165a022SGeorge Keishing    return dump_file_list
139