blob: d0b01081b15c354aae7dd3db2b3be6f4f4e26a7a [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walshe11a1362017-10-19 15:35:26 -05002
3r"""
4This file contains functions which are useful for processing BMC dumps.
5"""
6
7import gen_print as gp
Michael Walsh4f3ce172017-10-19 17:28:49 -05008import gen_misc as gm
9import gen_robot_keyword as grk
Michael Walshe11a1362017-10-19 15:35:26 -050010import bmc_ssh_utils as bsu
11import var_funcs as vf
Michael Walsh4f3ce172017-10-19 17:28:49 -050012import os
Michael Walshe11a1362017-10-19 15:35:26 -050013from robot.libraries.BuiltIn import BuiltIn
14import sys
15import os
16import imp
17base_path = os.path.dirname(os.path.dirname(
18 imp.find_module("gen_robot_print")[1])) + os.sep
19sys.path.append(base_path + "data/")
20import variables as var
21
22
23def get_dump_dict(quiet=None):
Michael Walshe11a1362017-10-19 15:35:26 -050024 r"""
25 Get dump information and return as an ordered dictionary where the keys
26 are the dump IDs and the values are the full path names of the dumps.
27
28 Example robot program call:
29
30 ${dump_dict}= Get Dump Dict
Michael Walsh39c00512019-07-17 10:54:06 -050031 Rprint Vars dump_dict
Michael Walshe11a1362017-10-19 15:35:26 -050032
33 Example output:
34
35 dump_dict:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050036 [1]:
37 /var/lib/phosphor-debug-collector/dumps/1/obmcdump_1_1508255216.tar.xz
38 [2]:
39 /var/lib/phosphor-debug-collector/dumps/2/obmcdump_2_1508255245.tar.xz
40 [3]:
41 /var/lib/phosphor-debug-collector/dumps/3/obmcdump_3_1508255267.tar.xz
42 [4]:
43 /var/lib/phosphor-debug-collector/dumps/4/obmcdump_4_1508255283.tar.xz
Michael Walshe11a1362017-10-19 15:35:26 -050044
45 Description of argument(s):
46 quiet If quiet is set to 1, this function will
47 NOT write status messages to stdout.
48 """
49
50 quiet = int(gp.get_var_value(quiet, 1))
George Keishingbd8ec922022-03-31 13:39:35 -050051 cmd_buf = "find /var/lib/phosphor-debug-collector/ -maxdepth 4 -type f"
Michael Walshe11a1362017-10-19 15:35:26 -050052 output, stderr, rc = bsu.bmc_execute_command(cmd_buf, quiet=quiet)
53
George Keishingbd8ec922022-03-31 13:39:35 -050054 BuiltIn().log_to_console(output)
55 return output.split("\n")
Michael Walshe11a1362017-10-19 15:35:26 -050056
57
58def valid_dump(dump_id,
59 dump_dict=None,
60 quiet=None):
Michael Walshe11a1362017-10-19 15:35:26 -050061 r"""
62 Verify that dump_id is a valid. If it is not valid, issue robot failure
63 message.
64
65 A dump is valid if the indicated dump_id refers to an existing dump with a
66 valid associated dump file.
67
68 Description of argument(s):
69 dump_id A dump ID (e.g. "1", "2", etc.)
70 dump_dict A dump dictionary such as the one returned
71 by get_dump_dict. If this value is None,
72 this function will call get_dump_dict on
73 the caller's behalf.
74 quiet If quiet is set to 1, this function will
75 NOT write status messages to stdout.
76 """
77
78 if dump_dict is None:
79 dump_dict = get_dump_dict(quiet=quiet)
80
81 if dump_id not in dump_dict:
82 message = "The specified dump ID was not found among the existing" \
83 + " dumps:\n"
84 message += gp.sprint_var(dump_id)
85 message += gp.sprint_var(dump_dict)
86 BuiltIn().fail(gp.sprint_error(message))
87
88 if not dump_dict[dump_id].endswith("tar.xz"):
89 message = "There is no \"tar.xz\" file associated with the given" \
90 + " dump_id:\n"
91 message += gp.sprint_var(dump_id)
92 dump_file_path = dump_dict[dump_id]
93 message += gp.sprint_var(dump_file_path)
94 BuiltIn().fail(gp.sprint_error(message))
Michael Walsh4f3ce172017-10-19 17:28:49 -050095
96
97def scp_dumps(targ_dir_path,
98 targ_file_prefix="",
99 dump_dict=None,
100 quiet=None):
Michael Walsh4f3ce172017-10-19 17:28:49 -0500101 r"""
102 SCP all dumps from the BMC to the indicated directory on the local system
103 and return a list of the new files.
104
105 Description of argument(s):
106 targ_dir_path The path of the directory to receive the
107 dump files.
108 targ_file_prefix Prefix which will be pre-pended to each
109 target file's name.
110 dump_dict A dump dictionary such as the one returned
111 by get_dump_dict. If this value is None,
112 this function will call get_dump_dict on
113 the caller's behalf.
114 quiet If quiet is set to 1, this function will
115 NOT write status messages to stdout.
116 """
117
118 targ_dir_path = gm.add_trailing_slash(targ_dir_path)
119
120 if dump_dict is None:
George Keishingbd8ec922022-03-31 13:39:35 -0500121 dump_list = get_dump_dict(quiet=quiet)
Michael Walsh4f3ce172017-10-19 17:28:49 -0500122
123 status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)
124
125 dump_file_list = []
George Keishingbd8ec922022-03-31 13:39:35 -0500126 for file_path in dump_list:
Michael Walsh4f3ce172017-10-19 17:28:49 -0500127 targ_file_path = targ_dir_path + targ_file_prefix \
George Keishingbd8ec922022-03-31 13:39:35 -0500128 + os.path.basename(file_path)
129 status, ret_values = grk.run_key("scp.Get File " + file_path
George Keishing1165a022021-05-19 06:23:13 -0500130 + " " + targ_file_path, quiet=quiet)
131 dump_file_list.append(targ_file_path)
132
133 return dump_file_list