blob: 3657136f8560313915bae184124b4468d6e377a1 [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 Phan0c669772021-06-24 13:52:42 -050025 def __init__(self,
26 hostname,
27 username,
28 password,
29 ffdc_config,
30 location,
31 remote_type,
32 remote_protocol):
Peter D Phan72ce6b82021-06-03 06:18:26 -050033 r"""
34 Description of argument(s):
35
36 hostname name/ip of the targeted (remote) system
37 username user on the targeted system with access to FFDC files
38 password password for user on targeted system
39 ffdc_config configuration file listing commands and files for FFDC
Peter D Phan04aca3b2021-06-21 10:37:18 -050040 location where to store collected FFDC
41 remote_type os type of the remote host
Peter D Phan72ce6b82021-06-03 06:18:26 -050042
43 """
44 if self.verify_script_env():
45 self.hostname = hostname
46 self.username = username
47 self.password = password
48 self.ffdc_config = ffdc_config
49 self.location = location
50 self.remote_client = None
51 self.ffdc_dir_path = ""
52 self.ffdc_prefix = ""
Peter D Phan04aca3b2021-06-21 10:37:18 -050053 self.target_type = remote_type.upper()
Peter D Phan0c669772021-06-24 13:52:42 -050054 self.remote_protocol = remote_protocol.upper()
Peter D Phan7610bc42021-07-06 06:31:05 -050055 self.start_time = 0
56 self.elapsed_time = ''
Peter D Phan72ce6b82021-06-03 06:18:26 -050057 else:
Peter D Phan8462faf2021-06-16 12:24:15 -050058 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -050059
George Keishing86764b52021-07-01 04:32:03 -050060 # Load default or user define YAML configuration file.
61 with open(self.ffdc_config, 'r') as file:
62 self.ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
63
Peter D Phan3beb02e2021-07-06 13:25:17 -050064 if self.target_type not in self.ffdc_actions.keys():
65 print("\n\tERROR: %s is not listed in %s.\n\n" % (self.target_type, self.ffdc_config))
66 sys.exit(-1)
George Keishing86764b52021-07-01 04:32:03 -050067
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 Phan72ce6b82021-06-03 06:18:26 -0500110 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500111 r"""
112 Initiate FFDC Collection depending on requested protocol.
113
114 """
115
George Keishing772c9772021-06-16 23:23:42 -0500116 print("\n\t---- Start communicating with %s ----" % self.hostname)
Peter D Phan7610bc42021-07-06 06:31:05 -0500117 self.start_time = time.time()
George Keishing772c9772021-06-16 23:23:42 -0500118 working_protocol_list = []
Peter D Phan72ce6b82021-06-03 06:18:26 -0500119 if self.target_is_pingable():
George Keishing772c9772021-06-16 23:23:42 -0500120 # Check supported protocol ping,ssh, redfish are working.
121 if self.ssh_to_target_system():
122 working_protocol_list.append("SSH")
George Keishing615fe322021-06-24 01:24:36 -0500123 working_protocol_list.append("SCP")
Peter D Phan0c669772021-06-24 13:52:42 -0500124
125 # Redfish
126 if self.verify_redfish():
127 working_protocol_list.append("REDFISH")
128 print("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
129 else:
Peter D Phan3beb02e2021-07-06 13:25:17 -0500130 print("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan0c669772021-06-24 13:52:42 -0500131
George Keishingeafba182021-06-29 13:44:58 -0500132 if self.verify_ipmi():
133 working_protocol_list.append("IPMI")
134 print("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
135 else:
Peter D Phan3beb02e2021-07-06 13:25:17 -0500136 print("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname)
George Keishingeafba182021-06-29 13:44:58 -0500137
Peter D Phan72ce6b82021-06-03 06:18:26 -0500138 # Verify top level directory exists for storage
139 self.validate_local_store(self.location)
George Keishing772c9772021-06-16 23:23:42 -0500140 print("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500141
142 if ((self.remote_protocol not in working_protocol_list) and (self.remote_protocol != 'ALL')):
143 print("\n\tWorking protocol list: %s" % working_protocol_list)
144 print(
145 '>>>>>\tERROR: Requested protocol %s is not in working protocol list.\n'
146 % self.remote_protocol)
147 sys.exit(-1)
148 else:
149 self.generate_ffdc(working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500150
151 def ssh_to_target_system(self):
152 r"""
153 Open a ssh connection to targeted system.
154
155 """
156
157 self.remoteclient = SSHRemoteclient(self.hostname,
158 self.username,
159 self.password)
160
161 self.remoteclient.ssh_remoteclient_login()
George Keishing772c9772021-06-16 23:23:42 -0500162 print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500163
164 # Check scp connection.
165 # If scp connection fails,
166 # continue with FFDC generation but skip scp files to local host.
167 self.remoteclient.scp_connection()
George Keishing772c9772021-06-16 23:23:42 -0500168 return True
Peter D Phan72ce6b82021-06-03 06:18:26 -0500169
George Keishing772c9772021-06-16 23:23:42 -0500170 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500171 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500172 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500173
Peter D Phan04aca3b2021-06-21 10:37:18 -0500174 Description of argument(s):
175 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500176 """
177
George Keishing772c9772021-06-16 23:23:42 -0500178 print("\n\t---- Executing commands on " + self.hostname + " ----")
179 print("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500180
Peter D Phan2b8052d2021-06-22 10:55:41 -0500181 # Set prefix values for scp files and directory.
182 # Since the time stamp is at second granularity, these values are set here
183 # to be sure that all files for this run will have same timestamps
184 # and they will be saved in the same directory.
185 # self.location == local system for now
186 self.set_ffdc_defaults()
George Keishing86764b52021-07-01 04:32:03 -0500187 ffdc_actions = self.ffdc_actions
Peter D Phan2b8052d2021-06-22 10:55:41 -0500188
Peter D Phan72ce6b82021-06-03 06:18:26 -0500189 for machine_type in ffdc_actions.keys():
George Keishing6ea92b02021-07-01 11:20:50 -0500190 if self.target_type != machine_type:
191 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500192
George Keishing6ea92b02021-07-01 11:20:50 -0500193 print("\tSystem Type: %s" % machine_type)
194 for k, v in ffdc_actions[machine_type].items():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500195
George Keishing6ea92b02021-07-01 11:20:50 -0500196 if self.remote_protocol != ffdc_actions[machine_type][k]['PROTOCOL'][0] \
197 and self.remote_protocol != 'ALL':
198 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500199
George Keishing6ea92b02021-07-01 11:20:50 -0500200 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SSH':
Peter D Phan3beb02e2021-07-06 13:25:17 -0500201 if 'SSH' in working_protocol_list:
202 self.protocol_ssh(ffdc_actions, machine_type, k)
203 else:
204 print("\n\tERROR: SSH is not available for %s." % self.hostname)
George Keishing6ea92b02021-07-01 11:20:50 -0500205
206 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'REDFISH':
Peter D Phan3beb02e2021-07-06 13:25:17 -0500207 if 'REDFISH' in working_protocol_list:
208 self.protocol_redfish(ffdc_actions, machine_type, k)
209 else:
210 print("\n\tERROR: REDFISH is not available for %s." % self.hostname)
George Keishing6ea92b02021-07-01 11:20:50 -0500211
212 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'IPMI':
Peter D Phan3beb02e2021-07-06 13:25:17 -0500213 if 'IPMI' in working_protocol_list:
214 self.protocol_ipmi(ffdc_actions, machine_type, k)
215 else:
216 print("\n\tERROR: IMPI is not available for %s." % self.hostname)
George Keishingeafba182021-06-29 13:44:58 -0500217
Peter D Phan04aca3b2021-06-21 10:37:18 -0500218 # Close network connection after collecting all files
Peter D Phan7610bc42021-07-06 06:31:05 -0500219 self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time))
Peter D Phan04aca3b2021-06-21 10:37:18 -0500220 self.remoteclient.ssh_remoteclient_disconnect()
221
Peter D Phan0c669772021-06-24 13:52:42 -0500222 def protocol_ssh(self,
223 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500224 machine_type,
225 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500226 r"""
227 Perform actions using SSH and SCP protocols.
228
229 Description of argument(s):
230 ffdc_actions List of actions from ffdc_config.yaml.
231 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500232 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500233 """
234
George Keishing6ea92b02021-07-01 11:20:50 -0500235 if sub_type == 'DUMP_LOGS':
236 self.group_copy(ffdc_actions[machine_type][sub_type])
237 else:
238 self.collect_and_copy_ffdc(ffdc_actions[machine_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500239
240 def protocol_redfish(self,
241 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500242 machine_type,
243 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500244 r"""
245 Perform actions using Redfish protocol.
246
247 Description of argument(s):
248 ffdc_actions List of actions from ffdc_config.yaml.
249 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500250 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500251 """
252
253 print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH'))
254 redfish_files_saved = []
255 progress_counter = 0
George Keishing6ea92b02021-07-01 11:20:50 -0500256 list_of_URL = ffdc_actions[machine_type][sub_type]['URL']
Peter D Phan0c669772021-06-24 13:52:42 -0500257 for index, each_url in enumerate(list_of_URL, start=0):
258 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
259 + self.hostname + ' -S Always raw GET ' + each_url
260
261 result = self.run_redfishtool(redfish_parm)
262 if result:
263 try:
George Keishing6ea92b02021-07-01 11:20:50 -0500264 targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index]
Peter D Phan0c669772021-06-24 13:52:42 -0500265 except IndexError:
266 targ_file = each_url.split('/')[-1]
267 print("\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url)
268 print("\t[WARN] Data will be stored in %s." % targ_file)
269
270 targ_file_with_path = (self.ffdc_dir_path
271 + self.ffdc_prefix
272 + targ_file)
273
274 # Creates a new file
275 with open(targ_file_with_path, 'w') as fp:
276 fp.write(result)
277 fp.close
278 redfish_files_saved.append(targ_file)
279
280 progress_counter += 1
281 self.print_progress(progress_counter)
282
283 print("\n\t[Run] Commands execution completed.\t\t [OK]")
284
285 for file in redfish_files_saved:
286 print("\n\t\tSuccessfully save file " + file + ".")
287
George Keishingeafba182021-06-29 13:44:58 -0500288 def protocol_ipmi(self,
289 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500290 machine_type,
291 sub_type):
George Keishingeafba182021-06-29 13:44:58 -0500292 r"""
293 Perform actions using ipmitool over LAN protocol.
294
295 Description of argument(s):
296 ffdc_actions List of actions from ffdc_config.yaml.
297 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500298 sub_type Group type of commands.
George Keishingeafba182021-06-29 13:44:58 -0500299 """
300
301 print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI'))
302 ipmi_files_saved = []
303 progress_counter = 0
George Keishing6ea92b02021-07-01 11:20:50 -0500304 list_of_cmd = ffdc_actions[machine_type][sub_type]['COMMANDS']
George Keishingeafba182021-06-29 13:44:58 -0500305 for index, each_cmd in enumerate(list_of_cmd, start=0):
306 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
307 + self.hostname + ' ' + each_cmd
308
309 result = self.run_ipmitool(ipmi_parm)
310 if result:
311 try:
George Keishing6ea92b02021-07-01 11:20:50 -0500312 targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index]
George Keishingeafba182021-06-29 13:44:58 -0500313 except IndexError:
George Keishing6ea92b02021-07-01 11:20:50 -0500314 targ_file = each_cmd.split('/')[-1]
George Keishingeafba182021-06-29 13:44:58 -0500315 print("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd)
316 print("\t[WARN] Data will be stored in %s." % targ_file)
317
318 targ_file_with_path = (self.ffdc_dir_path
319 + self.ffdc_prefix
320 + targ_file)
321
322 # Creates a new file
323 with open(targ_file_with_path, 'w') as fp:
324 fp.write(result)
325 fp.close
326 ipmi_files_saved.append(targ_file)
327
328 progress_counter += 1
329 self.print_progress(progress_counter)
330
331 print("\n\t[Run] Commands execution completed.\t\t [OK]")
332
333 for file in ipmi_files_saved:
334 print("\n\t\tSuccessfully save file " + file + ".")
335
Peter D Phan04aca3b2021-06-21 10:37:18 -0500336 def collect_and_copy_ffdc(self,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500337 ffdc_actions_for_machine_type,
338 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500339 r"""
340 Send commands in ffdc_config file to targeted system.
341
342 Description of argument(s):
343 ffdc_actions_for_machine_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500344 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500345 """
346
Peter D Phan3beb02e2021-07-06 13:25:17 -0500347 # Executing commands, , if any
348 self.ssh_execute_ffdc_commands(ffdc_actions_for_machine_type,
349 form_filename)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500350
Peter D Phan3beb02e2021-07-06 13:25:17 -0500351 # Copying files
Peter D Phan04aca3b2021-06-21 10:37:18 -0500352 if self.remoteclient.scpclient:
353 print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500354
Peter D Phan04aca3b2021-06-21 10:37:18 -0500355 # Retrieving files from target system
356 list_of_files = ffdc_actions_for_machine_type['FILES']
Peter D Phan2b8052d2021-06-22 10:55:41 -0500357 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500358 else:
359 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
360
Peter D Phan3beb02e2021-07-06 13:25:17 -0500361 def ssh_execute_ffdc_commands(self,
362 ffdc_actions_for_machine_type,
363 form_filename=False):
364 r"""
365 Send commands in ffdc_config file to targeted system.
366
367 Description of argument(s):
368 ffdc_actions_for_machine_type commands and files for the selected remote host type.
369 form_filename if true, pre-pend self.target_type to filename
370 """
371 print("\n\t[Run] Executing commands on %s using %s"
372 % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
373 list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
374
375 # If command list is empty, returns
376 if not list_of_commands:
377 return
378
379 progress_counter = 0
380 for command in list_of_commands:
381 if isinstance(command, dict):
382 command_txt = next(iter(command))
383 command_timeout = next(iter(command.values()))
384 elif isinstance(command, str):
385 command_txt = command
386 # Default ssh command timeout 60 seconds
387 command_timeout = 60
388
389 if form_filename:
390 command_txt = str(command_txt % self.target_type)
391
392 self.remoteclient.execute_command(command_txt, command_timeout)
393 progress_counter += 1
394 self.print_progress(progress_counter)
395
396 print("\n\t[Run] Commands execution completed.\t\t [OK]")
397
Peter D Phan56429a62021-06-23 08:38:29 -0500398 def group_copy(self,
399 ffdc_actions_for_machine_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500400 r"""
401 scp group of files (wild card) from remote host.
402
403 Description of argument(s):
404 ffdc_actions_for_machine_type commands and files for the selected remote host type.
405 """
Peter D Phan3beb02e2021-07-06 13:25:17 -0500406
407 # Executing commands, if any
408 self.ssh_execute_ffdc_commands(ffdc_actions_for_machine_type)
409
Peter D Phan56429a62021-06-23 08:38:29 -0500410 if self.remoteclient.scpclient:
George Keishing615fe322021-06-24 01:24:36 -0500411 print("\n\tCopying DUMP files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500412
413 # Retrieving files from target system, if any
414 list_of_files = ffdc_actions_for_machine_type['FILES']
415
416 for filename in list_of_files:
417 command = 'ls -AX ' + filename
418 response = self.remoteclient.execute_command(command)
419 # self.remoteclient.scp_file_from_remote() completed without exception,
420 # if any
421 if response:
422 scp_result = self.remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
423 if scp_result:
424 print("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
425 else:
426 print("\t\tThere is no " + filename)
427
428 else:
429 print("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
430
Peter D Phan72ce6b82021-06-03 06:18:26 -0500431 def scp_ffdc(self,
432 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500433 targ_file_prefix,
434 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500435 file_list=None,
436 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500437 r"""
438 SCP all files in file_dict to the indicated directory on the local system.
439
440 Description of argument(s):
441 targ_dir_path The path of the directory to receive the files.
442 targ_file_prefix Prefix which will be pre-pended to each
443 target file's name.
444 file_dict A dictionary of files to scp from targeted system to this system
445
446 """
447
Peter D Phan72ce6b82021-06-03 06:18:26 -0500448 progress_counter = 0
449 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500450 if form_filename:
451 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500452 source_file_path = filename
453 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
454
455 # self.remoteclient.scp_file_from_remote() completed without exception,
456 # add file to the receiving file list.
457 scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500458
459 if not quiet:
460 if scp_result:
Peter D Phan8462faf2021-06-16 12:24:15 -0500461 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500462 else:
463 print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
464 else:
465 progress_counter += 1
466 self.print_progress(progress_counter)
467
Peter D Phan72ce6b82021-06-03 06:18:26 -0500468 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500469 r"""
470 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
471 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
472 Individual ffdc file will have timestr_filename.
473
474 Description of class variables:
475 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
476
477 self.ffdc_prefix The prefix to be given to each ffdc file name.
478
479 """
480
481 timestr = time.strftime("%Y%m%d-%H%M%S")
482 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
483 self.ffdc_prefix = timestr + "_"
484 self.validate_local_store(self.ffdc_dir_path)
485
486 def validate_local_store(self, dir_path):
487 r"""
488 Ensure path exists to store FFDC files locally.
489
490 Description of variable:
491 dir_path The dir path where collected ffdc data files will be stored.
492
493 """
494
495 if not os.path.exists(dir_path):
496 try:
497 os.mkdir(dir_path, 0o755)
498 except (IOError, OSError) as e:
499 # PermissionError
500 if e.errno == EPERM or e.errno == EACCES:
501 print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path)
502 else:
503 print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror))
504 sys.exit(-1)
505
506 def print_progress(self, progress):
507 r"""
508 Print activity progress +
509
510 Description of variable:
511 progress Progress counter.
512
513 """
514
515 sys.stdout.write("\r\t" + "+" * progress)
516 sys.stdout.flush()
517 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500518
519 def verify_redfish(self):
520 r"""
521 Verify remote host has redfish service active
522
523 """
524 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
525 + self.hostname + ' -S Always raw GET /redfish/v1/'
526 return(self.run_redfishtool(redfish_parm, True))
527
George Keishingeafba182021-06-29 13:44:58 -0500528 def verify_ipmi(self):
529 r"""
530 Verify remote host has IPMI LAN service active
531
532 """
533 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
534 + self.hostname + ' power status'
535 return(self.run_ipmitool(ipmi_parm, True))
536
Peter D Phan0c669772021-06-24 13:52:42 -0500537 def run_redfishtool(self,
538 parms_string,
539 quiet=False):
540 r"""
541 Run CLI redfishtool
542
543 Description of variable:
544 parms_string redfishtool subcommand and options.
545 quiet do not print redfishtool error message if True
546 """
547
548 result = subprocess.run(['redfishtool ' + parms_string],
549 stdout=subprocess.PIPE,
550 stderr=subprocess.PIPE,
551 shell=True,
552 universal_newlines=True)
553
554 if result.stderr and not quiet:
555 print('\n\t\tERROR with redfishtool ' + parms_string)
556 print('\t\t' + result.stderr)
557
558 return result.stdout
George Keishingeafba182021-06-29 13:44:58 -0500559
560 def run_ipmitool(self,
561 parms_string,
562 quiet=False):
563 r"""
564 Run CLI IPMI tool.
565
566 Description of variable:
567 parms_string ipmitool subcommand and options.
568 quiet do not print redfishtool error message if True
569 """
570
571 result = subprocess.run(['ipmitool -I lanplus -C 17 ' + parms_string],
572 stdout=subprocess.PIPE,
573 stderr=subprocess.PIPE,
574 shell=True,
575 universal_newlines=True)
576
577 if result.stderr and not quiet:
578 print('\n\t\tERROR with ipmitool -I lanplus -C 17 ' + parms_string)
579 print('\t\t' + result.stderr)
580
581 return result.stdout