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

100 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-19 17:46 +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 shlex 

21import threading 

22 

23from common.const import GET_DEVICES_INFO_TIMEOUT, STACKTRACE 

24from common import timeout_decorator 

25from params import ParamDict 

26from common import log_error, log_info 

27from common import run_cmd_output 

28from common import FileOperate as f 

29from drv.env_type import LoadSoType 

30from info import AsysInfo 

31from health import AsysHealth 

32 

33from view.progress_display import waiting 

34 

35from collect.stacktrace.stacktrace_collect import AsysStackTrace 

36from collect.log import collect_device_logs 

37from collect.log import collect_host_logs 

38from collect.log import collect_rc_logs 

39from collect.graph import collect_graph 

40from collect.ops import collect_ops 

41from collect.trace import collect_trace 

42from collect.data_dump import collect_data_dump 

43 

44__all__ = ["AsysCollect"] 

45 

46 

47class AsysCollect: 

48 def __init__(self): 

49 self.output_root_path = ParamDict().asys_output_timestamp_dir 

50 self.finish_flag = False 

51 

52 @timeout_decorator(GET_DEVICES_INFO_TIMEOUT) 

53 def collect_status_info(self): 

54 """ 

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

56 """ 

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

58 # collect software, hardware and status information 

59 AsysInfo().write_info() 

60 else: 

61 AsysInfo().get_software_info(write_file=True) 

62 

63 @timeout_decorator(GET_DEVICES_INFO_TIMEOUT) 

64 def collect_health_info(self): 

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

66 # collect health check result 

67 AsysHealth().run() 

68 

69 def _device_file_export(self): 

70 def run_msnpureport(export_dir_path): 

71 f.create_dir(export_dir_path) 

72 export_dir_cmd = "cd " + shlex.quote(export_dir_path) 

73 export_tool = "msnpureport -f" 

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

75 cmd_res = run_cmd_output(export_cmd) 

76 if not cmd_res[0]: 

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

78 f.remove_dir(export_dir_path) 

79 return False 

80 return True 

81 

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

83 if not run_msnpureport(export_dir_path): 

84 return False 

85 # export success 

86 in_export_dir = f.list_dir(export_dir_path) 

87 if not in_export_dir: 

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

89 return False 

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

91 return msnpureport_output_dir 

92 

93 def collect(self): 

94 # check params 

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

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

97 return False 

98 

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

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

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

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

103 t.start() 

104 

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

106 # collect log files 

107 collect_host_logs(self.output_root_path) 

108 # export device files 

109 msnpureport_output_dir = self._device_file_export() 

110 if msnpureport_output_dir: 

111 collect_device_logs(msnpureport_output_dir, self.output_root_path) 

112 else: 

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

114 else: 

115 collect_rc_logs(self.output_root_path) 

116 

117 # collect graph 

118 collect_graph(self.output_root_path) 

119 

120 # collect_data_dump 

121 collect_data_dump(self.output_root_path) 

122 

123 # collect ops 

124 collect_ops(self.output_root_path) 

125 

126 # collect trace 

127 collect_trace(self.output_root_path) 

128 

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

130 LoadSoType().dll_close() 

131 try: 

132 self.collect_status_info() 

133 except TimeoutError: 

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

135 

136 try: 

137 self.collect_health_info() 

138 except TimeoutError: 

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

140 

141 self.finish_flag = True 

142 t.join() # wait print process_display end 

143 return True 

144 

145 def wait_view(self): 

146 while not self.finish_flag: 

147 waiting() 

148 continue 

149 

150 def clean_work(self): 

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

152 dir_list = [msnpureport_export_path] 

153 f.delete_dirs(dir_list) 

154 

155 def run(self): 

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

157 return AsysStackTrace().run() 

158 else: 

159 task_res = self.collect() 

160 self.clean_work() 

161 return task_res