blob: 7c65df9eadd46e5f82abb6f7815b78b99accec31 [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 -050013import sys
14import os
15import imp
Sridevi Ramesh47375aa2022-12-08 05:05:31 -060016import variables as var
17
18from robot.libraries.BuiltIn import BuiltIn
19
Michael Walshe11a1362017-10-19 15:35:26 -050020base_path = os.path.dirname(os.path.dirname(
21 imp.find_module("gen_robot_print")[1])) + os.sep
22sys.path.append(base_path + "data/")
Michael Walshe11a1362017-10-19 15:35:26 -050023
24
25def get_dump_dict(quiet=None):
Michael Walshe11a1362017-10-19 15:35:26 -050026 r"""
27 Get dump information and return as an ordered dictionary where the keys
28 are the dump IDs and the values are the full path names of the dumps.
29
30 Example robot program call:
31
32 ${dump_dict}= Get Dump Dict
Michael Walsh39c00512019-07-17 10:54:06 -050033 Rprint Vars dump_dict
Michael Walshe11a1362017-10-19 15:35:26 -050034
35 Example output:
36
37 dump_dict:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050038 [1]:
39 /var/lib/phosphor-debug-collector/dumps/1/obmcdump_1_1508255216.tar.xz
40 [2]:
41 /var/lib/phosphor-debug-collector/dumps/2/obmcdump_2_1508255245.tar.xz
42 [3]:
43 /var/lib/phosphor-debug-collector/dumps/3/obmcdump_3_1508255267.tar.xz
44 [4]:
45 /var/lib/phosphor-debug-collector/dumps/4/obmcdump_4_1508255283.tar.xz
Michael Walshe11a1362017-10-19 15:35:26 -050046
47 Description of argument(s):
48 quiet If quiet is set to 1, this function will
49 NOT write status messages to stdout.
50 """
51
52 quiet = int(gp.get_var_value(quiet, 1))
George Keishingbd8ec922022-03-31 13:39:35 -050053 cmd_buf = "find /var/lib/phosphor-debug-collector/ -maxdepth 4 -type f"
Michael Walshe11a1362017-10-19 15:35:26 -050054 output, stderr, rc = bsu.bmc_execute_command(cmd_buf, quiet=quiet)
55
George Keishingbd8ec922022-03-31 13:39:35 -050056 BuiltIn().log_to_console(output)
57 return output.split("\n")
Michael Walshe11a1362017-10-19 15:35:26 -050058
59
60def valid_dump(dump_id,
61 dump_dict=None,
62 quiet=None):
Michael Walshe11a1362017-10-19 15:35:26 -050063 r"""
64 Verify that dump_id is a valid. If it is not valid, issue robot failure
65 message.
66
67 A dump is valid if the indicated dump_id refers to an existing dump with a
68 valid associated dump file.
69
70 Description of argument(s):
71 dump_id A dump ID (e.g. "1", "2", etc.)
72 dump_dict A dump dictionary such as the one returned
73 by get_dump_dict. If this value is None,
74 this function will call get_dump_dict on
75 the caller's behalf.
76 quiet If quiet is set to 1, this function will
77 NOT write status messages to stdout.
78 """
79
80 if dump_dict is None:
81 dump_dict = get_dump_dict(quiet=quiet)
82
83 if dump_id not in dump_dict:
84 message = "The specified dump ID was not found among the existing" \
85 + " dumps:\n"
86 message += gp.sprint_var(dump_id)
87 message += gp.sprint_var(dump_dict)
88 BuiltIn().fail(gp.sprint_error(message))
89
90 if not dump_dict[dump_id].endswith("tar.xz"):
91 message = "There is no \"tar.xz\" file associated with the given" \
92 + " dump_id:\n"
93 message += gp.sprint_var(dump_id)
94 dump_file_path = dump_dict[dump_id]
95 message += gp.sprint_var(dump_file_path)
96 BuiltIn().fail(gp.sprint_error(message))
Michael Walsh4f3ce172017-10-19 17:28:49 -050097
98
99def scp_dumps(targ_dir_path,
100 targ_file_prefix="",
101 dump_dict=None,
102 quiet=None):
Michael Walsh4f3ce172017-10-19 17:28:49 -0500103 r"""
104 SCP all dumps from the BMC to the indicated directory on the local system
105 and return a list of the new files.
106
107 Description of argument(s):
108 targ_dir_path The path of the directory to receive the
109 dump files.
110 targ_file_prefix Prefix which will be pre-pended to each
111 target file's name.
112 dump_dict A dump dictionary such as the one returned
113 by get_dump_dict. If this value is None,
114 this function will call get_dump_dict on
115 the caller's behalf.
116 quiet If quiet is set to 1, this function will
117 NOT write status messages to stdout.
118 """
119
120 targ_dir_path = gm.add_trailing_slash(targ_dir_path)
121
122 if dump_dict is None:
George Keishingbd8ec922022-03-31 13:39:35 -0500123 dump_list = get_dump_dict(quiet=quiet)
Michael Walsh4f3ce172017-10-19 17:28:49 -0500124
125 status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)
126
127 dump_file_list = []
George Keishingbd8ec922022-03-31 13:39:35 -0500128 for file_path in dump_list:
Michael Walsh4f3ce172017-10-19 17:28:49 -0500129 targ_file_path = targ_dir_path + targ_file_prefix \
George Keishingbd8ec922022-03-31 13:39:35 -0500130 + os.path.basename(file_path)
131 status, ret_values = grk.run_key("scp.Get File " + file_path
George Keishing1165a022021-05-19 06:23:13 -0500132 + " " + targ_file_path, quiet=quiet)
133 dump_file_list.append(targ_file_path)
134
135 return dump_file_list