Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/collect/asys_collect.py: 88%

99 statements  

« prev     ^ index     » next       coverage.py v7.15.3, created at 2026-08-11 18:39 +0800

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3# ---------------------------------------------------------------------------- 

4# Copyright (c) 2025 Huawei Technologies Co., Ltd. 

5# 

6# Licensed under the Apache License, Version 2.0 (the "License"); 

7# you may not use this file except in compliance with the License. 

8# You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, software 

13# distributed under the License is distributed on an "AS IS" BASIS, 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

15# See the License for the specific language governing permissions and 

16# limitations under the License. 

17# ---------------------------------------------------------------------------- 

18 

19import os.path 

20import threading 

21 

22from common.const import GET_DEVICES_INFO_TIMEOUT, STACKTRACE 

23from common import timeout_decorator 

24from params import ParamDict 

25from common import log_error, log_info 

26from common import run_cmd_output 

27from common import FileOperate as f 

28from drv.env_type import LoadSoType 

29from info import AsysInfo 

30from health import AsysHealth 

31 

32from view.progress_display import waiting 

33 

34from collect.stacktrace.stacktrace_collect import AsysStackTrace 

35from collect.log import collect_device_logs 

36from collect.log import collect_host_logs 

37from collect.log import collect_rc_logs 

38from collect.graph import collect_graph 

39from collect.ops import collect_ops 

40from collect.trace import collect_trace 

41from collect.data_dump import collect_data_dump 

42 

43__all__ = ["AsysCollect"] 

44 

45 

46class AsysCollect: 

47 def __init__(self): 

48 self.output_root_path = ParamDict().asys_output_timestamp_dir 

49 self.finish_flag = False 

50 

51 @timeout_decorator(GET_DEVICES_INFO_TIMEOUT) 

52 def collect_status_info(self): 

53 """ 

54 no redundant plog temporary directory is generated when the status and health information is collected. 

55 """ 

56 if ParamDict().get_env_type() == "EP": 

57 # collect software, hardware and status information 

58 AsysInfo().write_info() 

59 else: 

60 AsysInfo().get_software_info(write_file=True) 

61 

62 @timeout_decorator(GET_DEVICES_INFO_TIMEOUT) 

63 def collect_health_info(self): 

64 if ParamDict().get_env_type() == "EP": 

65 # collect health check result 

66 AsysHealth().run() 

67 

68 def _device_file_export(self): 

69 def run_msnpureport(export_dir_path): 

70 f.create_dir(export_dir_path) 

71 export_dir_cmd = "cd " + export_dir_path 

72 export_tool = "msnpureport -f" 

73 export_cmd = "{0};{1}".format(export_dir_cmd, export_tool) 

74 cmd_res = run_cmd_output(export_cmd) 

75 if not cmd_res[0]: 

76 log_error("Call msnpureport tool failed, sys.stderr: \"{}\"".format(cmd_res[1].strip())) 

77 f.remove_dir(export_dir_path) 

78 return False 

79 return True 

80 

81 export_dir_path = os.path.join(self.output_root_path, "export_tmp") 

82 if not run_msnpureport(export_dir_path): 

83 return False 

84 # export success 

85 in_export_dir = f.list_dir(export_dir_path) 

86 if not in_export_dir: 

87 log_error("No files or directories in {0}".format(export_dir_path)) 

88 return False 

89 msnpureport_output_dir = os.path.join(export_dir_path, in_export_dir[0]) 

90 return msnpureport_output_dir 

91 

92 def collect(self): 

93 # check params 

94 if ParamDict().get_arg("remote") is not False or ParamDict().get_arg("all") or ParamDict().get_arg("quiet"): 

95 log_error("'--remote', '--all' and '--quiet' can be used only when '-r=stacktrace'.") 

96 return False 

97 

98 log_info('Collect task start, running:') 

99 # When the main program exits, the system checks whether there is a sub-thread whose daemon value is False. 

100 # If it exists, the main program exits after the sub-thread exits. Default daemon value is False. 

101 t = threading.Thread(target=self.wait_view, daemon=True) 

102 t.start() 

103 

104 if ParamDict().get_env_type() == "EP": 

105 # collect log files 

106 collect_host_logs(self.output_root_path) 

107 # export device files 

108 msnpureport_output_dir = self._device_file_export() 

109 if msnpureport_output_dir: 

110 collect_device_logs(msnpureport_output_dir, self.output_root_path) 

111 else: 

112 log_error("msnpureport tool export device files failed.") 

113 else: 

114 collect_rc_logs(self.output_root_path) 

115 

116 # collect graph 

117 collect_graph(self.output_root_path) 

118 

119 # collect_data_dump 

120 collect_data_dump(self.output_root_path) 

121 

122 # collect ops 

123 collect_ops(self.output_root_path) 

124 

125 # collect trace 

126 collect_trace(self.output_root_path) 

127 

128 # plog are generated when health and status are collected. 

129 LoadSoType().dll_close() 

130 try: 

131 self.collect_status_info() 

132 except TimeoutError: 

133 log_error("Timeout in retrieving device status information.") 

134 

135 try: 

136 self.collect_health_info() 

137 except TimeoutError: 

138 log_error("Timeout in retrieving device health information.") 

139 

140 self.finish_flag = True 

141 t.join() # wait print process_display end 

142 return True 

143 

144 def wait_view(self): 

145 while not self.finish_flag: 

146 waiting() 

147 continue 

148 

149 def clean_work(self): 

150 msnpureport_export_path = os.path.join(self.output_root_path, "export_tmp") 

151 dir_list = [msnpureport_export_path] 

152 f.delete_dirs(dir_list) 

153 

154 def run(self): 

155 if ParamDict().get_arg("run_mode") == STACKTRACE: 

156 return AsysStackTrace().run() 

157 else: 

158 task_res = self.collect() 

159 self.clean_work() 

160 return task_res