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
720f38712SPatrick Williamsimport imp
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 = (
1820f38712SPatrick Williams    os.path.dirname(os.path.dirname(imp.find_module("gen_robot_print")[1]))
1920f38712SPatrick Williams    + os.sep
2020f38712SPatrick Williams)
21e11a136bSMichael Walshsys.path.append(base_path + "data/")
2209679890SGeorge Keishingimport variables as var  # NOQA
23e11a136bSMichael Walsh
24e11a136bSMichael Walsh
25e11a136bSMichael Walshdef get_dump_dict(quiet=None):
26e11a136bSMichael Walsh    r"""
27e11a136bSMichael Walsh    Get dump information and return as an ordered dictionary where the keys
28e11a136bSMichael Walsh    are the dump IDs and the values are the full path names of the dumps.
29e11a136bSMichael Walsh
30e11a136bSMichael Walsh    Example robot program call:
31e11a136bSMichael Walsh
32e11a136bSMichael Walsh    ${dump_dict}=  Get Dump Dict
3339c00518SMichael Walsh    Rprint Vars  dump_dict
34e11a136bSMichael Walsh
35e11a136bSMichael Walsh    Example output:
36e11a136bSMichael Walsh
37e11a136bSMichael Walsh    dump_dict:
38004ad3c9SJoy Onyerikwu      [1]:
39004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/1/obmcdump_1_1508255216.tar.xz
40004ad3c9SJoy Onyerikwu      [2]:
41004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/2/obmcdump_2_1508255245.tar.xz
42004ad3c9SJoy Onyerikwu      [3]:
43004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/3/obmcdump_3_1508255267.tar.xz
44004ad3c9SJoy Onyerikwu      [4]:
45004ad3c9SJoy Onyerikwu      /var/lib/phosphor-debug-collector/dumps/4/obmcdump_4_1508255283.tar.xz
46e11a136bSMichael Walsh
47e11a136bSMichael Walsh    Description of argument(s):
48e11a136bSMichael Walsh    quiet                           If quiet is set to 1, this function will
49e11a136bSMichael Walsh                                    NOT write status messages to stdout.
50e11a136bSMichael Walsh    """
51e11a136bSMichael Walsh
52e11a136bSMichael Walsh    quiet = int(gp.get_var_value(quiet, 1))
53bd8ec926SGeorge Keishing    cmd_buf = "find /var/lib/phosphor-debug-collector/ -maxdepth 4 -type f"
54e11a136bSMichael Walsh    output, stderr, rc = bsu.bmc_execute_command(cmd_buf, quiet=quiet)
55e11a136bSMichael Walsh
56bd8ec926SGeorge Keishing    BuiltIn().log_to_console(output)
57bd8ec926SGeorge Keishing    return output.split("\n")
58e11a136bSMichael Walsh
59e11a136bSMichael Walsh
6020f38712SPatrick Williamsdef valid_dump(dump_id, dump_dict=None, quiet=None):
61e11a136bSMichael Walsh    r"""
62e11a136bSMichael Walsh    Verify that dump_id is a valid.  If it is not valid, issue robot failure
63e11a136bSMichael Walsh    message.
64e11a136bSMichael Walsh
65e11a136bSMichael Walsh    A dump is valid if the indicated dump_id refers to an existing dump with a
66e11a136bSMichael Walsh    valid associated dump file.
67e11a136bSMichael Walsh
68e11a136bSMichael Walsh    Description of argument(s):
69e11a136bSMichael Walsh    dump_id                         A dump ID (e.g. "1", "2", etc.)
70e11a136bSMichael Walsh    dump_dict                       A dump dictionary such as the one returned
71e11a136bSMichael Walsh                                    by get_dump_dict.  If this value is None,
72e11a136bSMichael Walsh                                    this function will call get_dump_dict on
73e11a136bSMichael Walsh                                    the caller's behalf.
74e11a136bSMichael Walsh    quiet                           If quiet is set to 1, this function will
75e11a136bSMichael Walsh                                    NOT write status messages to stdout.
76e11a136bSMichael Walsh    """
77e11a136bSMichael Walsh
78e11a136bSMichael Walsh    if dump_dict is None:
79e11a136bSMichael Walsh        dump_dict = get_dump_dict(quiet=quiet)
80e11a136bSMichael Walsh
81e11a136bSMichael Walsh    if dump_id not in dump_dict:
8220f38712SPatrick Williams        message = (
8320f38712SPatrick Williams            "The specified dump ID was not found among the existing"
84e11a136bSMichael Walsh            + " dumps:\n"
8520f38712SPatrick Williams        )
86e11a136bSMichael Walsh        message += gp.sprint_var(dump_id)
87e11a136bSMichael Walsh        message += gp.sprint_var(dump_dict)
88e11a136bSMichael Walsh        BuiltIn().fail(gp.sprint_error(message))
89e11a136bSMichael Walsh
90e11a136bSMichael Walsh    if not dump_dict[dump_id].endswith("tar.xz"):
9120f38712SPatrick Williams        message = (
9220f38712SPatrick Williams            'There is no "tar.xz" file associated with the given'
93e11a136bSMichael Walsh            + " dump_id:\n"
9420f38712SPatrick Williams        )
95e11a136bSMichael Walsh        message += gp.sprint_var(dump_id)
96e11a136bSMichael Walsh        dump_file_path = dump_dict[dump_id]
97e11a136bSMichael Walsh        message += gp.sprint_var(dump_file_path)
98e11a136bSMichael Walsh        BuiltIn().fail(gp.sprint_error(message))
994f3ce17dSMichael Walsh
1004f3ce17dSMichael Walsh
10120f38712SPatrick Williamsdef scp_dumps(targ_dir_path, targ_file_prefix="", dump_dict=None, quiet=None):
1024f3ce17dSMichael Walsh    r"""
1034f3ce17dSMichael Walsh    SCP all dumps from the BMC to the indicated directory on the local system
1044f3ce17dSMichael Walsh    and return a list of the new files.
1054f3ce17dSMichael Walsh
1064f3ce17dSMichael Walsh    Description of argument(s):
1074f3ce17dSMichael Walsh    targ_dir_path                   The path of the directory to receive the
1084f3ce17dSMichael Walsh                                    dump files.
109*e16f158fSGeorge Keishing    targ_file_prefix                Prefix which will be prepended to each
1104f3ce17dSMichael Walsh                                    target file's name.
1114f3ce17dSMichael Walsh    dump_dict                       A dump dictionary such as the one returned
1124f3ce17dSMichael Walsh                                    by get_dump_dict.  If this value is None,
1134f3ce17dSMichael Walsh                                    this function will call get_dump_dict on
1144f3ce17dSMichael Walsh                                    the caller's behalf.
1154f3ce17dSMichael Walsh    quiet                           If quiet is set to 1, this function will
1164f3ce17dSMichael Walsh                                    NOT write status messages to stdout.
1174f3ce17dSMichael Walsh    """
1184f3ce17dSMichael Walsh
1194f3ce17dSMichael Walsh    targ_dir_path = gm.add_trailing_slash(targ_dir_path)
1204f3ce17dSMichael Walsh
1214f3ce17dSMichael Walsh    if dump_dict is None:
122bd8ec926SGeorge Keishing        dump_list = get_dump_dict(quiet=quiet)
1234f3ce17dSMichael Walsh
1244f3ce17dSMichael Walsh    status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)
1254f3ce17dSMichael Walsh
1264f3ce17dSMichael Walsh    dump_file_list = []
127bd8ec926SGeorge Keishing    for file_path in dump_list:
12820f38712SPatrick Williams        targ_file_path = (
12920f38712SPatrick Williams            targ_dir_path + targ_file_prefix + os.path.basename(file_path)
13020f38712SPatrick Williams        )
13120f38712SPatrick Williams        status, ret_values = grk.run_key(
13220f38712SPatrick Williams            "scp.Get File  " + file_path + "  " + targ_file_path, quiet=quiet
13320f38712SPatrick Williams        )
1341165a022SGeorge Keishing        dump_file_list.append(targ_file_path)
1351165a022SGeorge Keishing
1361165a022SGeorge Keishing    return dump_file_list
137