blob: 2316df2d202c12fd66b4cd94caa6ba13114283df [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
Peter D Phan0c669772021-06-24 13:52:42 -050013import subprocess
Peter D Phan72ce6b82021-06-03 06:18:26 -050014from ssh_utility import SSHRemoteclient
15
16
17class FFDCCollector:
18
19 r"""
20 Sends commands from configuration file to the targeted system to collect log files.
21 Fetch and store generated files at the specified location.
22
23 """
24
Peter D Phan04aca3b2021-06-21 10:37:18 -050025 # List of supported OSes.
26 supported_oses = ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
27
Peter D Phan0c669772021-06-24 13:52:42 -050028 def __init__(self,
29 hostname,
30 username,
31 password,
32 ffdc_config,
33 location,
34 remote_type,
35 remote_protocol):
Peter D Phan72ce6b82021-06-03 06:18:26 -050036 r"""
37 Description of argument(s):
38
39 hostname name/ip of the targeted (remote) system
40 username user on the targeted system with access to FFDC files
41 password password for user on targeted system
42 ffdc_config configuration file listing commands and files for FFDC
Peter D Phan04aca3b2021-06-21 10:37:18 -050043 location where to store collected FFDC
44 remote_type os type of the remote host
Peter D Phan72ce6b82021-06-03 06:18:26 -050045
46 """
47 if self.verify_script_env():
48 self.hostname = hostname
49 self.username = username
50 self.password = password
51 self.ffdc_config = ffdc_config
52 self.location = location
53 self.remote_client = None
54 self.ffdc_dir_path = ""
55 self.ffdc_prefix = ""
Peter D Phan04aca3b2021-06-21 10:37:18 -050056 self.target_type = remote_type.upper()
Peter D Phan0c669772021-06-24 13:52:42 -050057 self.remote_protocol = remote_protocol.upper()
Peter D Phan72ce6b82021-06-03 06:18:26 -050058 else:
Peter D Phan8462faf2021-06-16 12:24:15 -050059 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -050060
George Keishing86764b52021-07-01 04:32:03 -050061 # Load default or user define YAML configuration file.
62 with open(self.ffdc_config, 'r') as file:
63 self.ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
64
65 # List of supported OSes.
66 self.supported_oses = self.ffdc_actions['DISTRO_OS'][0]
67
Peter D Phan72ce6b82021-06-03 06:18:26 -050068 def verify_script_env(self):
69
70 # Import to log version
71 import click
72 import paramiko
73
74 run_env_ok = True
Peter D Phan0c669772021-06-24 13:52:42 -050075
George Keishingeafba182021-06-29 13:44:58 -050076 redfishtool_version = self.run_redfishtool('-V').split(' ')[2].strip('\n')
77 ipmitool_version = self.run_ipmitool('-V').split(' ')[2]
Peter D Phan0c669772021-06-24 13:52:42 -050078
Peter D Phan72ce6b82021-06-03 06:18:26 -050079 print("\n\t---- Script host environment ----")
80 print("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
81 print("\t{:<10} {:<10}".format('Script host os', platform.platform()))
82 print("\t{:<10} {:>10}".format('Python', platform.python_version()))
83 print("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
84 print("\t{:<10} {:>10}".format('click', click.__version__))
85 print("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
George Keishingeafba182021-06-29 13:44:58 -050086 print("\t{:<10} {:>9}".format('redfishtool', redfishtool_version))
87 print("\t{:<10} {:>12}".format('ipmitool', ipmitool_version))
Peter D Phan72ce6b82021-06-03 06:18:26 -050088
Peter D Phan8462faf2021-06-16 12:24:15 -050089 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
90 print("\n\tERROR: Python or python packages do not meet minimum version requirement.")
91 print("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050092 run_env_ok = False
93
94 print("\t---- End script host environment ----")
95 return run_env_ok
96
97 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050098 r"""
99 Check if target system is ping-able.
100
101 """
102 response = os.system("ping -c 1 -w 2 %s 2>&1 >/dev/null" % self.hostname)
103 if response == 0:
George Keishing615fe322021-06-24 01:24:36 -0500104 print("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500105 return True
106 else:
107 print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
108 sys.exit(-1)
109
Peter D Phan04aca3b2021-06-21 10:37:18 -0500110 def inspect_target_machine_type(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500111 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500112 Inspect remote host os-release or uname.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500113
114 """
George Keishing86764b52021-07-01 04:32:03 -0500115 print("\n\tSupported distro: %s" % self.supported_oses)
116
Peter D Phan04aca3b2021-06-21 10:37:18 -0500117 command = "cat /etc/os-release"
118 response = self.remoteclient.execute_command(command)
119 if response:
120 print("\n\t[INFO] %s /etc/os-release\n" % self.hostname)
Peter D Phan0c42a942021-06-29 09:17:27 -0500121 for each_info in response:
122 print("\t\t %s" % each_info)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500123 identity = self.find_os_type(response, 'ID').split('=')[1].upper()
124 else:
125 response = self.remoteclient.execute_command('uname -a')
126 print("\n\t[INFO] %s uname -a\n" % self.hostname)
127 print("\t\t %s" % ' '.join(response))
128 identity = self.find_os_type(response, 'AIX').split(' ')[0].upper()
Peter D Phan2b8052d2021-06-22 10:55:41 -0500129
130 # If OS does not have /etc/os-release and is not AIX,
131 # script does not yet know what to do.
Peter D Phan04aca3b2021-06-21 10:37:18 -0500132 if not identity:
133 print(">>>>>\tERROR: Script does not yet know about %s" % ' '.join(response))
134 sys.exit(-1)
135
Peter D Phan0c669772021-06-24 13:52:42 -0500136 if (self.target_type not in identity):
137
Peter D Phan04aca3b2021-06-21 10:37:18 -0500138 user_target_type = self.target_type
Peter D Phan2b8052d2021-06-22 10:55:41 -0500139 self.target_type = ""
George Keishing86764b52021-07-01 04:32:03 -0500140 for each_os in self.supported_oses:
Peter D Phan04aca3b2021-06-21 10:37:18 -0500141 if each_os in identity:
142 self.target_type = each_os
143 break
Peter D Phan2b8052d2021-06-22 10:55:41 -0500144
145 # If OS in not one of ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
146 # script does not yet know what to do.
147 if not self.target_type:
148 print(">>>>>\tERROR: Script does not yet know about %s" % identity)
149 sys.exit(-1)
150
Peter D Phan04aca3b2021-06-21 10:37:18 -0500151 print("\n\t[WARN] user request %s does not match remote host type %s.\n"
152 % (user_target_type, self.target_type))
153 print("\t[WARN] FFDC collection continues for %s.\n" % self.target_type)
154
155 def find_os_type(self,
156 listing_from_os,
157 key):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500158 r"""
159 Return OS information with the requested key
160
161 Description of argument(s):
162
163 listing_from_os list of information returns from OS command
164 key key of the desired data
165
166 """
167
168 for each_item in listing_from_os:
169 if key in each_item:
170 return each_item
171 return ''
Peter D Phan72ce6b82021-06-03 06:18:26 -0500172
173 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500174 r"""
175 Initiate FFDC Collection depending on requested protocol.
176
177 """
178
George Keishing772c9772021-06-16 23:23:42 -0500179 print("\n\t---- Start communicating with %s ----" % self.hostname)
180 working_protocol_list = []
Peter D Phan72ce6b82021-06-03 06:18:26 -0500181 if self.target_is_pingable():
George Keishing772c9772021-06-16 23:23:42 -0500182 # Check supported protocol ping,ssh, redfish are working.
183 if self.ssh_to_target_system():
184 working_protocol_list.append("SSH")
George Keishing615fe322021-06-24 01:24:36 -0500185 working_protocol_list.append("SCP")
Peter D Phan0c669772021-06-24 13:52:42 -0500186
187 # Redfish
188 if self.verify_redfish():
189 working_protocol_list.append("REDFISH")
190 print("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
191 else:
192 print("\n\t[Check] %s Redfish Service.\t\t [FAILED]" % self.hostname)
193
George Keishingeafba182021-06-29 13:44:58 -0500194 if self.verify_ipmi():
195 working_protocol_list.append("IPMI")
196 print("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
197 else:
198 print("\n\t[Check] %s IPMI LAN Service.\t\t [FAILED]" % self.hostname)
199
Peter D Phan72ce6b82021-06-03 06:18:26 -0500200 # Verify top level directory exists for storage
201 self.validate_local_store(self.location)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500202 self.inspect_target_machine_type()
George Keishing772c9772021-06-16 23:23:42 -0500203 print("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500204
205 if ((self.remote_protocol not in working_protocol_list) and (self.remote_protocol != 'ALL')):
206 print("\n\tWorking protocol list: %s" % working_protocol_list)
207 print(
208 '>>>>>\tERROR: Requested protocol %s is not in working protocol list.\n'
209 % self.remote_protocol)
210 sys.exit(-1)
211 else:
212 self.generate_ffdc(working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500213
214 def ssh_to_target_system(self):
215 r"""
216 Open a ssh connection to targeted system.
217
218 """
219
220 self.remoteclient = SSHRemoteclient(self.hostname,
221 self.username,
222 self.password)
223
224 self.remoteclient.ssh_remoteclient_login()
George Keishing772c9772021-06-16 23:23:42 -0500225 print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500226
227 # Check scp connection.
228 # If scp connection fails,
229 # continue with FFDC generation but skip scp files to local host.
230 self.remoteclient.scp_connection()
George Keishing772c9772021-06-16 23:23:42 -0500231 return True
Peter D Phan72ce6b82021-06-03 06:18:26 -0500232
George Keishing772c9772021-06-16 23:23:42 -0500233 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500234 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500235 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500236
Peter D Phan04aca3b2021-06-21 10:37:18 -0500237 Description of argument(s):
238 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500239 """
240
George Keishing772c9772021-06-16 23:23:42 -0500241 print("\n\t---- Executing commands on " + self.hostname + " ----")
242 print("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500243
Peter D Phan2b8052d2021-06-22 10:55:41 -0500244 # Set prefix values for scp files and directory.
245 # Since the time stamp is at second granularity, these values are set here
246 # to be sure that all files for this run will have same timestamps
247 # and they will be saved in the same directory.
248 # self.location == local system for now
249 self.set_ffdc_defaults()
George Keishing86764b52021-07-01 04:32:03 -0500250 ffdc_actions = self.ffdc_actions
Peter D Phan2b8052d2021-06-22 10:55:41 -0500251
Peter D Phan72ce6b82021-06-03 06:18:26 -0500252 for machine_type in ffdc_actions.keys():
George Keishing6ea92b02021-07-01 11:20:50 -0500253 if self.target_type != machine_type:
254 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500255
George Keishing6ea92b02021-07-01 11:20:50 -0500256 print("\tSystem Type: %s" % machine_type)
257 for k, v in ffdc_actions[machine_type].items():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500258
George Keishing6ea92b02021-07-01 11:20:50 -0500259 if self.remote_protocol != ffdc_actions[machine_type][k]['PROTOCOL'][0] \
260 and self.remote_protocol != 'ALL':
261 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500262
George Keishing6ea92b02021-07-01 11:20:50 -0500263 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SSH':
264 self.protocol_ssh(ffdc_actions, machine_type, k)
265
266 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'REDFISH':
267 self.protocol_redfish(ffdc_actions, machine_type, k)
268
269 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'IPMI':
270 self.protocol_ipmi(ffdc_actions, machine_type, k)
George Keishingeafba182021-06-29 13:44:58 -0500271
Peter D Phan04aca3b2021-06-21 10:37:18 -0500272 # Close network connection after collecting all files
273 self.remoteclient.ssh_remoteclient_disconnect()
274
Peter D Phan0c669772021-06-24 13:52:42 -0500275 def protocol_ssh(self,
276 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500277 machine_type,
278 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500279 r"""
280 Perform actions using SSH and SCP protocols.
281
282 Description of argument(s):
283 ffdc_actions List of actions from ffdc_config.yaml.
284 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500285 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500286 """
287
George Keishing6ea92b02021-07-01 11:20:50 -0500288 if sub_type == 'DUMP_LOGS':
289 self.group_copy(ffdc_actions[machine_type][sub_type])
290 else:
291 self.collect_and_copy_ffdc(ffdc_actions[machine_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500292
293 def protocol_redfish(self,
294 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500295 machine_type,
296 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500297 r"""
298 Perform actions using Redfish protocol.
299
300 Description of argument(s):
301 ffdc_actions List of actions from ffdc_config.yaml.
302 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500303 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500304 """
305
306 print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH'))
307 redfish_files_saved = []
308 progress_counter = 0
George Keishing6ea92b02021-07-01 11:20:50 -0500309 list_of_URL = ffdc_actions[machine_type][sub_type]['URL']
Peter D Phan0c669772021-06-24 13:52:42 -0500310 for index, each_url in enumerate(list_of_URL, start=0):
311 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
312 + self.hostname + ' -S Always raw GET ' + each_url
313
314 result = self.run_redfishtool(redfish_parm)
315 if result:
316 try:
George Keishing6ea92b02021-07-01 11:20:50 -0500317 targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index]
Peter D Phan0c669772021-06-24 13:52:42 -0500318 except IndexError:
319 targ_file = each_url.split('/')[-1]
320 print("\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url)
321 print("\t[WARN] Data will be stored in %s." % targ_file)
322
323 targ_file_with_path = (self.ffdc_dir_path
324 + self.ffdc_prefix
325 + targ_file)
326
327 # Creates a new file
328 with open(targ_file_with_path, 'w') as fp:
329 fp.write(result)
330 fp.close
331 redfish_files_saved.append(targ_file)
332
333 progress_counter += 1
334 self.print_progress(progress_counter)
335
336 print("\n\t[Run] Commands execution completed.\t\t [OK]")
337
338 for file in redfish_files_saved:
339 print("\n\t\tSuccessfully save file " + file + ".")
340
George Keishingeafba182021-06-29 13:44:58 -0500341 def protocol_ipmi(self,
342 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500343 machine_type,
344 sub_type):
George Keishingeafba182021-06-29 13:44:58 -0500345 r"""
346 Perform actions using ipmitool over LAN protocol.
347
348 Description of argument(s):
349 ffdc_actions List of actions from ffdc_config.yaml.
350 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500351 sub_type Group type of commands.
George Keishingeafba182021-06-29 13:44:58 -0500352 """
353
354 print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI'))
355 ipmi_files_saved = []
356 progress_counter = 0
George Keishing6ea92b02021-07-01 11:20:50 -0500357 list_of_cmd = ffdc_actions[machine_type][sub_type]['COMMANDS']
George Keishingeafba182021-06-29 13:44:58 -0500358 for index, each_cmd in enumerate(list_of_cmd, start=0):
359 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
360 + self.hostname + ' ' + each_cmd
361
362 result = self.run_ipmitool(ipmi_parm)
363 if result:
364 try:
George Keishing6ea92b02021-07-01 11:20:50 -0500365 targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index]
George Keishingeafba182021-06-29 13:44:58 -0500366 except IndexError:
George Keishing6ea92b02021-07-01 11:20:50 -0500367 targ_file = each_cmd.split('/')[-1]
George Keishingeafba182021-06-29 13:44:58 -0500368 print("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd)
369 print("\t[WARN] Data will be stored in %s." % targ_file)
370
371 targ_file_with_path = (self.ffdc_dir_path
372 + self.ffdc_prefix
373 + targ_file)
374
375 # Creates a new file
376 with open(targ_file_with_path, 'w') as fp:
377 fp.write(result)
378 fp.close
379 ipmi_files_saved.append(targ_file)
380
381 progress_counter += 1
382 self.print_progress(progress_counter)
383
384 print("\n\t[Run] Commands execution completed.\t\t [OK]")
385
386 for file in ipmi_files_saved:
387 print("\n\t\tSuccessfully save file " + file + ".")
388
Peter D Phan04aca3b2021-06-21 10:37:18 -0500389 def collect_and_copy_ffdc(self,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500390 ffdc_actions_for_machine_type,
391 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500392 r"""
393 Send commands in ffdc_config file to targeted system.
394
395 Description of argument(s):
396 ffdc_actions_for_machine_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500397 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500398 """
399
400 print("\n\t[Run] Executing commands on %s using %s"
401 % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
402 list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
403 progress_counter = 0
404 for command in list_of_commands:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500405 if form_filename:
406 command = str(command % self.target_type)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500407 self.remoteclient.execute_command(command)
408 progress_counter += 1
409 self.print_progress(progress_counter)
410
411 print("\n\t[Run] Commands execution completed.\t\t [OK]")
412
413 if self.remoteclient.scpclient:
414 print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500415
Peter D Phan04aca3b2021-06-21 10:37:18 -0500416 # Retrieving files from target system
417 list_of_files = ffdc_actions_for_machine_type['FILES']
Peter D Phan2b8052d2021-06-22 10:55:41 -0500418 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500419 else:
420 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
421
Peter D Phan56429a62021-06-23 08:38:29 -0500422 def group_copy(self,
423 ffdc_actions_for_machine_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500424 r"""
425 scp group of files (wild card) from remote host.
426
427 Description of argument(s):
428 ffdc_actions_for_machine_type commands and files for the selected remote host type.
429 """
430 if self.remoteclient.scpclient:
George Keishing615fe322021-06-24 01:24:36 -0500431 print("\n\tCopying DUMP files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500432
433 # Retrieving files from target system, if any
434 list_of_files = ffdc_actions_for_machine_type['FILES']
435
436 for filename in list_of_files:
437 command = 'ls -AX ' + filename
438 response = self.remoteclient.execute_command(command)
439 # self.remoteclient.scp_file_from_remote() completed without exception,
440 # if any
441 if response:
442 scp_result = self.remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
443 if scp_result:
444 print("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
445 else:
446 print("\t\tThere is no " + filename)
447
448 else:
449 print("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
450
Peter D Phan72ce6b82021-06-03 06:18:26 -0500451 def scp_ffdc(self,
452 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500453 targ_file_prefix,
454 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500455 file_list=None,
456 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500457 r"""
458 SCP all files in file_dict to the indicated directory on the local system.
459
460 Description of argument(s):
461 targ_dir_path The path of the directory to receive the files.
462 targ_file_prefix Prefix which will be pre-pended to each
463 target file's name.
464 file_dict A dictionary of files to scp from targeted system to this system
465
466 """
467
Peter D Phan72ce6b82021-06-03 06:18:26 -0500468 progress_counter = 0
469 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500470 if form_filename:
471 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500472 source_file_path = filename
473 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
474
475 # self.remoteclient.scp_file_from_remote() completed without exception,
476 # add file to the receiving file list.
477 scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500478
479 if not quiet:
480 if scp_result:
Peter D Phan8462faf2021-06-16 12:24:15 -0500481 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500482 else:
483 print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
484 else:
485 progress_counter += 1
486 self.print_progress(progress_counter)
487
Peter D Phan72ce6b82021-06-03 06:18:26 -0500488 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500489 r"""
490 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
491 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
492 Individual ffdc file will have timestr_filename.
493
494 Description of class variables:
495 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
496
497 self.ffdc_prefix The prefix to be given to each ffdc file name.
498
499 """
500
501 timestr = time.strftime("%Y%m%d-%H%M%S")
502 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
503 self.ffdc_prefix = timestr + "_"
504 self.validate_local_store(self.ffdc_dir_path)
505
506 def validate_local_store(self, dir_path):
507 r"""
508 Ensure path exists to store FFDC files locally.
509
510 Description of variable:
511 dir_path The dir path where collected ffdc data files will be stored.
512
513 """
514
515 if not os.path.exists(dir_path):
516 try:
517 os.mkdir(dir_path, 0o755)
518 except (IOError, OSError) as e:
519 # PermissionError
520 if e.errno == EPERM or e.errno == EACCES:
521 print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path)
522 else:
523 print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror))
524 sys.exit(-1)
525
526 def print_progress(self, progress):
527 r"""
528 Print activity progress +
529
530 Description of variable:
531 progress Progress counter.
532
533 """
534
535 sys.stdout.write("\r\t" + "+" * progress)
536 sys.stdout.flush()
537 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500538
539 def verify_redfish(self):
540 r"""
541 Verify remote host has redfish service active
542
543 """
544 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
545 + self.hostname + ' -S Always raw GET /redfish/v1/'
546 return(self.run_redfishtool(redfish_parm, True))
547
George Keishingeafba182021-06-29 13:44:58 -0500548 def verify_ipmi(self):
549 r"""
550 Verify remote host has IPMI LAN service active
551
552 """
553 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
554 + self.hostname + ' power status'
555 return(self.run_ipmitool(ipmi_parm, True))
556
Peter D Phan0c669772021-06-24 13:52:42 -0500557 def run_redfishtool(self,
558 parms_string,
559 quiet=False):
560 r"""
561 Run CLI redfishtool
562
563 Description of variable:
564 parms_string redfishtool subcommand and options.
565 quiet do not print redfishtool error message if True
566 """
567
568 result = subprocess.run(['redfishtool ' + parms_string],
569 stdout=subprocess.PIPE,
570 stderr=subprocess.PIPE,
571 shell=True,
572 universal_newlines=True)
573
574 if result.stderr and not quiet:
575 print('\n\t\tERROR with redfishtool ' + parms_string)
576 print('\t\t' + result.stderr)
577
578 return result.stdout
George Keishingeafba182021-06-29 13:44:58 -0500579
580 def run_ipmitool(self,
581 parms_string,
582 quiet=False):
583 r"""
584 Run CLI IPMI tool.
585
586 Description of variable:
587 parms_string ipmitool subcommand and options.
588 quiet do not print redfishtool error message if True
589 """
590
591 result = subprocess.run(['ipmitool -I lanplus -C 17 ' + parms_string],
592 stdout=subprocess.PIPE,
593 stderr=subprocess.PIPE,
594 shell=True,
595 universal_newlines=True)
596
597 if result.stderr and not quiet:
598 print('\n\t\tERROR with ipmitool -I lanplus -C 17 ' + parms_string)
599 print('\t\t' + result.stderr)
600
601 return result.stdout