blob: 873f563a0e19ca87c4df5e8b9d6f03082d8aa748 [file] [log] [blame]
Peter D Phan72ce6b82021-06-03 06:18:26 -05001#!/usr/bin/env python
2
3r"""
4See class prolog below for details.
5"""
6
7import os
8import sys
9import yaml
10import time
11import platform
12from errno import EACCES, EPERM
13from ssh_utility import SSHRemoteclient
14
15
16class FFDCCollector:
17
18 r"""
19 Sends commands from configuration file to the targeted system to collect log files.
20 Fetch and store generated files at the specified location.
21
22 """
23
Peter D Phan04aca3b2021-06-21 10:37:18 -050024 # List of supported OSes.
25 supported_oses = ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
26
27 def __init__(self, hostname, username, password, ffdc_config, location, remote_type):
Peter D Phan72ce6b82021-06-03 06:18:26 -050028 r"""
29 Description of argument(s):
30
31 hostname name/ip of the targeted (remote) system
32 username user on the targeted system with access to FFDC files
33 password password for user on targeted system
34 ffdc_config configuration file listing commands and files for FFDC
Peter D Phan04aca3b2021-06-21 10:37:18 -050035 location where to store collected FFDC
36 remote_type os type of the remote host
Peter D Phan72ce6b82021-06-03 06:18:26 -050037
38 """
39 if self.verify_script_env():
40 self.hostname = hostname
41 self.username = username
42 self.password = password
43 self.ffdc_config = ffdc_config
44 self.location = location
45 self.remote_client = None
46 self.ffdc_dir_path = ""
47 self.ffdc_prefix = ""
48 self.receive_file_list = []
Peter D Phan04aca3b2021-06-21 10:37:18 -050049 self.target_type = remote_type.upper()
Peter D Phan72ce6b82021-06-03 06:18:26 -050050 else:
Peter D Phan8462faf2021-06-16 12:24:15 -050051 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -050052
53 def verify_script_env(self):
54
55 # Import to log version
56 import click
57 import paramiko
58
59 run_env_ok = True
60 print("\n\t---- Script host environment ----")
61 print("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
62 print("\t{:<10} {:<10}".format('Script host os', platform.platform()))
63 print("\t{:<10} {:>10}".format('Python', platform.python_version()))
64 print("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
65 print("\t{:<10} {:>10}".format('click', click.__version__))
66 print("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
67
Peter D Phan8462faf2021-06-16 12:24:15 -050068 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
69 print("\n\tERROR: Python or python packages do not meet minimum version requirement.")
70 print("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050071 run_env_ok = False
72
73 print("\t---- End script host environment ----")
74 return run_env_ok
75
76 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050077 r"""
78 Check if target system is ping-able.
79
80 """
81 response = os.system("ping -c 1 -w 2 %s 2>&1 >/dev/null" % self.hostname)
82 if response == 0:
George Keishing772c9772021-06-16 23:23:42 -050083 print("\n\t[Check] %s is ping-able.\t\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -050084 return True
85 else:
86 print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
87 sys.exit(-1)
88
Peter D Phan04aca3b2021-06-21 10:37:18 -050089 def inspect_target_machine_type(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050090 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -050091 Inspect remote host os-release or uname.
Peter D Phan72ce6b82021-06-03 06:18:26 -050092
93 """
Peter D Phan04aca3b2021-06-21 10:37:18 -050094 command = "cat /etc/os-release"
95 response = self.remoteclient.execute_command(command)
96 if response:
97 print("\n\t[INFO] %s /etc/os-release\n" % self.hostname)
98 print("\t\t %s" % self.find_os_type(response, 'PRETTY_NAME'))
99 identity = self.find_os_type(response, 'ID').split('=')[1].upper()
100 else:
101 response = self.remoteclient.execute_command('uname -a')
102 print("\n\t[INFO] %s uname -a\n" % self.hostname)
103 print("\t\t %s" % ' '.join(response))
104 identity = self.find_os_type(response, 'AIX').split(' ')[0].upper()
105 if not identity:
106 print(">>>>>\tERROR: Script does not yet know about %s" % ' '.join(response))
107 sys.exit(-1)
108
109 if self.target_type not in identity:
110 user_target_type = self.target_type
111 for each_os in FFDCCollector.supported_oses:
112 if each_os in identity:
113 self.target_type = each_os
114 break
115 print("\n\t[WARN] user request %s does not match remote host type %s.\n"
116 % (user_target_type, self.target_type))
117 print("\t[WARN] FFDC collection continues for %s.\n" % self.target_type)
118
119 def find_os_type(self,
120 listing_from_os,
121 key):
122
123 r"""
124 Return OS information with the requested key
125
126 Description of argument(s):
127
128 listing_from_os list of information returns from OS command
129 key key of the desired data
130
131 """
132
133 for each_item in listing_from_os:
134 if key in each_item:
135 return each_item
136 return ''
Peter D Phan72ce6b82021-06-03 06:18:26 -0500137
138 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500139 r"""
140 Initiate FFDC Collection depending on requested protocol.
141
142 """
143
George Keishing772c9772021-06-16 23:23:42 -0500144 print("\n\t---- Start communicating with %s ----" % self.hostname)
145 working_protocol_list = []
Peter D Phan72ce6b82021-06-03 06:18:26 -0500146 if self.target_is_pingable():
George Keishing772c9772021-06-16 23:23:42 -0500147 # Check supported protocol ping,ssh, redfish are working.
148 if self.ssh_to_target_system():
149 working_protocol_list.append("SSH")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500150 # Verify top level directory exists for storage
151 self.validate_local_store(self.location)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500152 self.inspect_target_machine_type()
George Keishing772c9772021-06-16 23:23:42 -0500153 print("\n\t---- Completed protocol pre-requisite check ----\n")
154 self.generate_ffdc(working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500155
156 def ssh_to_target_system(self):
157 r"""
158 Open a ssh connection to targeted system.
159
160 """
161
162 self.remoteclient = SSHRemoteclient(self.hostname,
163 self.username,
164 self.password)
165
166 self.remoteclient.ssh_remoteclient_login()
George Keishing772c9772021-06-16 23:23:42 -0500167 print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500168
169 # Check scp connection.
170 # If scp connection fails,
171 # continue with FFDC generation but skip scp files to local host.
172 self.remoteclient.scp_connection()
George Keishing772c9772021-06-16 23:23:42 -0500173 return True
Peter D Phan72ce6b82021-06-03 06:18:26 -0500174
George Keishing772c9772021-06-16 23:23:42 -0500175 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500176 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500177 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500178
Peter D Phan04aca3b2021-06-21 10:37:18 -0500179 Description of argument(s):
180 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500181 """
182
George Keishing772c9772021-06-16 23:23:42 -0500183 print("\n\t---- Executing commands on " + self.hostname + " ----")
184 print("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500185 with open(self.ffdc_config, 'r') as file:
186 ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
187
188 for machine_type in ffdc_actions.keys():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500189
Peter D Phan04aca3b2021-06-21 10:37:18 -0500190 if machine_type == self.target_type:
George Keishing772c9772021-06-16 23:23:42 -0500191 if (ffdc_actions[machine_type]['PROTOCOL'][0] in working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500192
Peter D Phan04aca3b2021-06-21 10:37:18 -0500193 # For RHEL and UBUNTU, collect common Linux FFDC.
194 if self.target_type == 'RHEL' \
195 or self.target_type == 'UBUNTU':
Peter D Phan72ce6b82021-06-03 06:18:26 -0500196
Peter D Phan04aca3b2021-06-21 10:37:18 -0500197 self.collect_and_copy_ffdc(ffdc_actions['LINUX'])
198
199 # Collect remote host specific FFDC.
200 self.collect_and_copy_ffdc(ffdc_actions[machine_type])
Peter D Phan72ce6b82021-06-03 06:18:26 -0500201 else:
202 print("\n\tProtocol %s is not yet supported by this script.\n"
203 % ffdc_actions[machine_type]['PROTOCOL'][0])
204
Peter D Phan04aca3b2021-06-21 10:37:18 -0500205 # Close network connection after collecting all files
206 self.remoteclient.ssh_remoteclient_disconnect()
207
208 def collect_and_copy_ffdc(self,
209 ffdc_actions_for_machine_type):
210 r"""
211 Send commands in ffdc_config file to targeted system.
212
213 Description of argument(s):
214 ffdc_actions_for_machine_type commands and files for the selected remote host type.
215 """
216
217 print("\n\t[Run] Executing commands on %s using %s"
218 % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
219 list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
220 progress_counter = 0
221 for command in list_of_commands:
222 self.remoteclient.execute_command(command)
223 progress_counter += 1
224 self.print_progress(progress_counter)
225
226 print("\n\t[Run] Commands execution completed.\t\t [OK]")
227
228 if self.remoteclient.scpclient:
229 print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
230 # Get default values for scp action.
231 # self.location == local system for now
232 self.set_ffdc_defaults()
233 # Retrieving files from target system
234 list_of_files = ffdc_actions_for_machine_type['FILES']
235 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, list_of_files)
236 else:
237 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
238
Peter D Phan72ce6b82021-06-03 06:18:26 -0500239 def scp_ffdc(self,
240 targ_dir_path,
241 targ_file_prefix="",
242 file_list=None,
243 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500244 r"""
245 SCP all files in file_dict to the indicated directory on the local system.
246
247 Description of argument(s):
248 targ_dir_path The path of the directory to receive the files.
249 targ_file_prefix Prefix which will be pre-pended to each
250 target file's name.
251 file_dict A dictionary of files to scp from targeted system to this system
252
253 """
254
Peter D Phan72ce6b82021-06-03 06:18:26 -0500255 progress_counter = 0
256 for filename in file_list:
257 source_file_path = filename
258 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
259
260 # self.remoteclient.scp_file_from_remote() completed without exception,
261 # add file to the receiving file list.
262 scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
263 if scp_result:
264 self.receive_file_list.append(targ_file_path)
265
266 if not quiet:
267 if scp_result:
Peter D Phan8462faf2021-06-16 12:24:15 -0500268 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500269 else:
270 print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
271 else:
272 progress_counter += 1
273 self.print_progress(progress_counter)
274
Peter D Phan72ce6b82021-06-03 06:18:26 -0500275 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500276 r"""
277 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
278 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
279 Individual ffdc file will have timestr_filename.
280
281 Description of class variables:
282 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
283
284 self.ffdc_prefix The prefix to be given to each ffdc file name.
285
286 """
287
288 timestr = time.strftime("%Y%m%d-%H%M%S")
289 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
290 self.ffdc_prefix = timestr + "_"
291 self.validate_local_store(self.ffdc_dir_path)
292
293 def validate_local_store(self, dir_path):
294 r"""
295 Ensure path exists to store FFDC files locally.
296
297 Description of variable:
298 dir_path The dir path where collected ffdc data files will be stored.
299
300 """
301
302 if not os.path.exists(dir_path):
303 try:
304 os.mkdir(dir_path, 0o755)
305 except (IOError, OSError) as e:
306 # PermissionError
307 if e.errno == EPERM or e.errno == EACCES:
308 print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path)
309 else:
310 print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror))
311 sys.exit(-1)
312
313 def print_progress(self, progress):
314 r"""
315 Print activity progress +
316
317 Description of variable:
318 progress Progress counter.
319
320 """
321
322 sys.stdout.write("\r\t" + "+" * progress)
323 sys.stdout.flush()
324 time.sleep(.1)