blob: 8b242ab550bf4b9082e7fa1646035dcc7045a874 [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
61 def verify_script_env(self):
62
63 # Import to log version
64 import click
65 import paramiko
66
67 run_env_ok = True
Peter D Phan0c669772021-06-24 13:52:42 -050068
George Keishingeafba182021-06-29 13:44:58 -050069 redfishtool_version = self.run_redfishtool('-V').split(' ')[2].strip('\n')
70 ipmitool_version = self.run_ipmitool('-V').split(' ')[2]
Peter D Phan0c669772021-06-24 13:52:42 -050071
Peter D Phan72ce6b82021-06-03 06:18:26 -050072 print("\n\t---- Script host environment ----")
73 print("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
74 print("\t{:<10} {:<10}".format('Script host os', platform.platform()))
75 print("\t{:<10} {:>10}".format('Python', platform.python_version()))
76 print("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
77 print("\t{:<10} {:>10}".format('click', click.__version__))
78 print("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
George Keishingeafba182021-06-29 13:44:58 -050079 print("\t{:<10} {:>9}".format('redfishtool', redfishtool_version))
80 print("\t{:<10} {:>12}".format('ipmitool', ipmitool_version))
Peter D Phan72ce6b82021-06-03 06:18:26 -050081
Peter D Phan8462faf2021-06-16 12:24:15 -050082 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
83 print("\n\tERROR: Python or python packages do not meet minimum version requirement.")
84 print("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -050085 run_env_ok = False
86
87 print("\t---- End script host environment ----")
88 return run_env_ok
89
90 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -050091 r"""
92 Check if target system is ping-able.
93
94 """
95 response = os.system("ping -c 1 -w 2 %s 2>&1 >/dev/null" % self.hostname)
96 if response == 0:
George Keishing615fe322021-06-24 01:24:36 -050097 print("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -050098 return True
99 else:
100 print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
101 sys.exit(-1)
102
Peter D Phan04aca3b2021-06-21 10:37:18 -0500103 def inspect_target_machine_type(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500104 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500105 Inspect remote host os-release or uname.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500106
107 """
Peter D Phan04aca3b2021-06-21 10:37:18 -0500108 command = "cat /etc/os-release"
109 response = self.remoteclient.execute_command(command)
110 if response:
111 print("\n\t[INFO] %s /etc/os-release\n" % self.hostname)
Peter D Phan0c42a942021-06-29 09:17:27 -0500112 for each_info in response:
113 print("\t\t %s" % each_info)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500114 identity = self.find_os_type(response, 'ID').split('=')[1].upper()
115 else:
116 response = self.remoteclient.execute_command('uname -a')
117 print("\n\t[INFO] %s uname -a\n" % self.hostname)
118 print("\t\t %s" % ' '.join(response))
119 identity = self.find_os_type(response, 'AIX').split(' ')[0].upper()
Peter D Phan2b8052d2021-06-22 10:55:41 -0500120
121 # If OS does not have /etc/os-release and is not AIX,
122 # script does not yet know what to do.
Peter D Phan04aca3b2021-06-21 10:37:18 -0500123 if not identity:
124 print(">>>>>\tERROR: Script does not yet know about %s" % ' '.join(response))
125 sys.exit(-1)
126
Peter D Phan0c669772021-06-24 13:52:42 -0500127 if (self.target_type not in identity):
128
Peter D Phan04aca3b2021-06-21 10:37:18 -0500129 user_target_type = self.target_type
Peter D Phan2b8052d2021-06-22 10:55:41 -0500130 self.target_type = ""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500131 for each_os in FFDCCollector.supported_oses:
132 if each_os in identity:
133 self.target_type = each_os
134 break
Peter D Phan2b8052d2021-06-22 10:55:41 -0500135
136 # If OS in not one of ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
137 # script does not yet know what to do.
138 if not self.target_type:
139 print(">>>>>\tERROR: Script does not yet know about %s" % identity)
140 sys.exit(-1)
141
Peter D Phan04aca3b2021-06-21 10:37:18 -0500142 print("\n\t[WARN] user request %s does not match remote host type %s.\n"
143 % (user_target_type, self.target_type))
144 print("\t[WARN] FFDC collection continues for %s.\n" % self.target_type)
145
146 def find_os_type(self,
147 listing_from_os,
148 key):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500149 r"""
150 Return OS information with the requested key
151
152 Description of argument(s):
153
154 listing_from_os list of information returns from OS command
155 key key of the desired data
156
157 """
158
159 for each_item in listing_from_os:
160 if key in each_item:
161 return each_item
162 return ''
Peter D Phan72ce6b82021-06-03 06:18:26 -0500163
164 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500165 r"""
166 Initiate FFDC Collection depending on requested protocol.
167
168 """
169
George Keishing772c9772021-06-16 23:23:42 -0500170 print("\n\t---- Start communicating with %s ----" % self.hostname)
171 working_protocol_list = []
Peter D Phan72ce6b82021-06-03 06:18:26 -0500172 if self.target_is_pingable():
George Keishing772c9772021-06-16 23:23:42 -0500173 # Check supported protocol ping,ssh, redfish are working.
174 if self.ssh_to_target_system():
175 working_protocol_list.append("SSH")
George Keishing615fe322021-06-24 01:24:36 -0500176 working_protocol_list.append("SCP")
Peter D Phan0c669772021-06-24 13:52:42 -0500177
178 # Redfish
179 if self.verify_redfish():
180 working_protocol_list.append("REDFISH")
181 print("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
182 else:
183 print("\n\t[Check] %s Redfish Service.\t\t [FAILED]" % self.hostname)
184
George Keishingeafba182021-06-29 13:44:58 -0500185 if self.verify_ipmi():
186 working_protocol_list.append("IPMI")
187 print("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
188 else:
189 print("\n\t[Check] %s IPMI LAN Service.\t\t [FAILED]" % self.hostname)
190
Peter D Phan72ce6b82021-06-03 06:18:26 -0500191 # Verify top level directory exists for storage
192 self.validate_local_store(self.location)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500193 self.inspect_target_machine_type()
George Keishing772c9772021-06-16 23:23:42 -0500194 print("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500195
196 if ((self.remote_protocol not in working_protocol_list) and (self.remote_protocol != 'ALL')):
197 print("\n\tWorking protocol list: %s" % working_protocol_list)
198 print(
199 '>>>>>\tERROR: Requested protocol %s is not in working protocol list.\n'
200 % self.remote_protocol)
201 sys.exit(-1)
202 else:
203 self.generate_ffdc(working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500204
205 def ssh_to_target_system(self):
206 r"""
207 Open a ssh connection to targeted system.
208
209 """
210
211 self.remoteclient = SSHRemoteclient(self.hostname,
212 self.username,
213 self.password)
214
215 self.remoteclient.ssh_remoteclient_login()
George Keishing772c9772021-06-16 23:23:42 -0500216 print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500217
218 # Check scp connection.
219 # If scp connection fails,
220 # continue with FFDC generation but skip scp files to local host.
221 self.remoteclient.scp_connection()
George Keishing772c9772021-06-16 23:23:42 -0500222 return True
Peter D Phan72ce6b82021-06-03 06:18:26 -0500223
George Keishing772c9772021-06-16 23:23:42 -0500224 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500225 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500226 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500227
Peter D Phan04aca3b2021-06-21 10:37:18 -0500228 Description of argument(s):
229 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500230 """
231
George Keishing772c9772021-06-16 23:23:42 -0500232 print("\n\t---- Executing commands on " + self.hostname + " ----")
233 print("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500234 with open(self.ffdc_config, 'r') as file:
235 ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
236
Peter D Phan2b8052d2021-06-22 10:55:41 -0500237 # Set prefix values for scp files and directory.
238 # Since the time stamp is at second granularity, these values are set here
239 # to be sure that all files for this run will have same timestamps
240 # and they will be saved in the same directory.
241 # self.location == local system for now
242 self.set_ffdc_defaults()
243
Peter D Phan72ce6b82021-06-03 06:18:26 -0500244 for machine_type in ffdc_actions.keys():
George Keishing6ea92b02021-07-01 11:20:50 -0500245 if self.target_type != machine_type:
246 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500247
George Keishing6ea92b02021-07-01 11:20:50 -0500248 print("\tSystem Type: %s" % machine_type)
249 for k, v in ffdc_actions[machine_type].items():
Peter D Phan72ce6b82021-06-03 06:18:26 -0500250
George Keishing6ea92b02021-07-01 11:20:50 -0500251 if self.remote_protocol != ffdc_actions[machine_type][k]['PROTOCOL'][0] \
252 and self.remote_protocol != 'ALL':
253 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500254
George Keishing6ea92b02021-07-01 11:20:50 -0500255 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SSH':
256 self.protocol_ssh(ffdc_actions, machine_type, k)
257
258 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'REDFISH':
259 self.protocol_redfish(ffdc_actions, machine_type, k)
260
261 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'IPMI':
262 self.protocol_ipmi(ffdc_actions, machine_type, k)
George Keishingeafba182021-06-29 13:44:58 -0500263
Peter D Phan04aca3b2021-06-21 10:37:18 -0500264 # Close network connection after collecting all files
265 self.remoteclient.ssh_remoteclient_disconnect()
266
Peter D Phan0c669772021-06-24 13:52:42 -0500267 def protocol_ssh(self,
268 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500269 machine_type,
270 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500271 r"""
272 Perform actions using SSH and SCP protocols.
273
274 Description of argument(s):
275 ffdc_actions List of actions from ffdc_config.yaml.
276 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500277 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500278 """
279
George Keishing6ea92b02021-07-01 11:20:50 -0500280 if sub_type == 'DUMP_LOGS':
281 self.group_copy(ffdc_actions[machine_type][sub_type])
282 else:
283 self.collect_and_copy_ffdc(ffdc_actions[machine_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500284
285 def protocol_redfish(self,
286 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500287 machine_type,
288 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500289 r"""
290 Perform actions using Redfish protocol.
291
292 Description of argument(s):
293 ffdc_actions List of actions from ffdc_config.yaml.
294 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500295 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500296 """
297
298 print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH'))
299 redfish_files_saved = []
300 progress_counter = 0
George Keishing6ea92b02021-07-01 11:20:50 -0500301 list_of_URL = ffdc_actions[machine_type][sub_type]['URL']
Peter D Phan0c669772021-06-24 13:52:42 -0500302 for index, each_url in enumerate(list_of_URL, start=0):
303 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
304 + self.hostname + ' -S Always raw GET ' + each_url
305
306 result = self.run_redfishtool(redfish_parm)
307 if result:
308 try:
George Keishing6ea92b02021-07-01 11:20:50 -0500309 targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index]
Peter D Phan0c669772021-06-24 13:52:42 -0500310 except IndexError:
311 targ_file = each_url.split('/')[-1]
312 print("\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url)
313 print("\t[WARN] Data will be stored in %s." % targ_file)
314
315 targ_file_with_path = (self.ffdc_dir_path
316 + self.ffdc_prefix
317 + targ_file)
318
319 # Creates a new file
320 with open(targ_file_with_path, 'w') as fp:
321 fp.write(result)
322 fp.close
323 redfish_files_saved.append(targ_file)
324
325 progress_counter += 1
326 self.print_progress(progress_counter)
327
328 print("\n\t[Run] Commands execution completed.\t\t [OK]")
329
330 for file in redfish_files_saved:
331 print("\n\t\tSuccessfully save file " + file + ".")
332
George Keishingeafba182021-06-29 13:44:58 -0500333 def protocol_ipmi(self,
334 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500335 machine_type,
336 sub_type):
George Keishingeafba182021-06-29 13:44:58 -0500337 r"""
338 Perform actions using ipmitool over LAN protocol.
339
340 Description of argument(s):
341 ffdc_actions List of actions from ffdc_config.yaml.
342 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500343 sub_type Group type of commands.
George Keishingeafba182021-06-29 13:44:58 -0500344 """
345
346 print("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI'))
347 ipmi_files_saved = []
348 progress_counter = 0
George Keishing6ea92b02021-07-01 11:20:50 -0500349 list_of_cmd = ffdc_actions[machine_type][sub_type]['COMMANDS']
George Keishingeafba182021-06-29 13:44:58 -0500350 for index, each_cmd in enumerate(list_of_cmd, start=0):
351 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
352 + self.hostname + ' ' + each_cmd
353
354 result = self.run_ipmitool(ipmi_parm)
355 if result:
356 try:
George Keishing6ea92b02021-07-01 11:20:50 -0500357 targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index]
George Keishingeafba182021-06-29 13:44:58 -0500358 except IndexError:
George Keishing6ea92b02021-07-01 11:20:50 -0500359 targ_file = each_cmd.split('/')[-1]
George Keishingeafba182021-06-29 13:44:58 -0500360 print("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd)
361 print("\t[WARN] Data will be stored in %s." % targ_file)
362
363 targ_file_with_path = (self.ffdc_dir_path
364 + self.ffdc_prefix
365 + targ_file)
366
367 # Creates a new file
368 with open(targ_file_with_path, 'w') as fp:
369 fp.write(result)
370 fp.close
371 ipmi_files_saved.append(targ_file)
372
373 progress_counter += 1
374 self.print_progress(progress_counter)
375
376 print("\n\t[Run] Commands execution completed.\t\t [OK]")
377
378 for file in ipmi_files_saved:
379 print("\n\t\tSuccessfully save file " + file + ".")
380
Peter D Phan04aca3b2021-06-21 10:37:18 -0500381 def collect_and_copy_ffdc(self,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500382 ffdc_actions_for_machine_type,
383 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500384 r"""
385 Send commands in ffdc_config file to targeted system.
386
387 Description of argument(s):
388 ffdc_actions_for_machine_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500389 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500390 """
391
392 print("\n\t[Run] Executing commands on %s using %s"
393 % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
394 list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
395 progress_counter = 0
396 for command in list_of_commands:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500397 if form_filename:
398 command = str(command % self.target_type)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500399 self.remoteclient.execute_command(command)
400 progress_counter += 1
401 self.print_progress(progress_counter)
402
403 print("\n\t[Run] Commands execution completed.\t\t [OK]")
404
405 if self.remoteclient.scpclient:
406 print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500407
Peter D Phan04aca3b2021-06-21 10:37:18 -0500408 # Retrieving files from target system
409 list_of_files = ffdc_actions_for_machine_type['FILES']
Peter D Phan2b8052d2021-06-22 10:55:41 -0500410 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500411 else:
412 print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
413
Peter D Phan56429a62021-06-23 08:38:29 -0500414 def group_copy(self,
415 ffdc_actions_for_machine_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500416 r"""
417 scp group of files (wild card) from remote host.
418
419 Description of argument(s):
420 ffdc_actions_for_machine_type commands and files for the selected remote host type.
421 """
422 if self.remoteclient.scpclient:
George Keishing615fe322021-06-24 01:24:36 -0500423 print("\n\tCopying DUMP files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500424
425 # Retrieving files from target system, if any
426 list_of_files = ffdc_actions_for_machine_type['FILES']
427
428 for filename in list_of_files:
429 command = 'ls -AX ' + filename
430 response = self.remoteclient.execute_command(command)
431 # self.remoteclient.scp_file_from_remote() completed without exception,
432 # if any
433 if response:
434 scp_result = self.remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
435 if scp_result:
436 print("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
437 else:
438 print("\t\tThere is no " + filename)
439
440 else:
441 print("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
442
Peter D Phan72ce6b82021-06-03 06:18:26 -0500443 def scp_ffdc(self,
444 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500445 targ_file_prefix,
446 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500447 file_list=None,
448 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500449 r"""
450 SCP all files in file_dict to the indicated directory on the local system.
451
452 Description of argument(s):
453 targ_dir_path The path of the directory to receive the files.
454 targ_file_prefix Prefix which will be pre-pended to each
455 target file's name.
456 file_dict A dictionary of files to scp from targeted system to this system
457
458 """
459
Peter D Phan72ce6b82021-06-03 06:18:26 -0500460 progress_counter = 0
461 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500462 if form_filename:
463 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500464 source_file_path = filename
465 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
466
467 # self.remoteclient.scp_file_from_remote() completed without exception,
468 # add file to the receiving file list.
469 scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500470
471 if not quiet:
472 if scp_result:
Peter D Phan8462faf2021-06-16 12:24:15 -0500473 print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500474 else:
475 print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
476 else:
477 progress_counter += 1
478 self.print_progress(progress_counter)
479
Peter D Phan72ce6b82021-06-03 06:18:26 -0500480 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500481 r"""
482 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
483 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
484 Individual ffdc file will have timestr_filename.
485
486 Description of class variables:
487 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
488
489 self.ffdc_prefix The prefix to be given to each ffdc file name.
490
491 """
492
493 timestr = time.strftime("%Y%m%d-%H%M%S")
494 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
495 self.ffdc_prefix = timestr + "_"
496 self.validate_local_store(self.ffdc_dir_path)
497
498 def validate_local_store(self, dir_path):
499 r"""
500 Ensure path exists to store FFDC files locally.
501
502 Description of variable:
503 dir_path The dir path where collected ffdc data files will be stored.
504
505 """
506
507 if not os.path.exists(dir_path):
508 try:
509 os.mkdir(dir_path, 0o755)
510 except (IOError, OSError) as e:
511 # PermissionError
512 if e.errno == EPERM or e.errno == EACCES:
513 print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path)
514 else:
515 print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror))
516 sys.exit(-1)
517
518 def print_progress(self, progress):
519 r"""
520 Print activity progress +
521
522 Description of variable:
523 progress Progress counter.
524
525 """
526
527 sys.stdout.write("\r\t" + "+" * progress)
528 sys.stdout.flush()
529 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500530
531 def verify_redfish(self):
532 r"""
533 Verify remote host has redfish service active
534
535 """
536 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
537 + self.hostname + ' -S Always raw GET /redfish/v1/'
538 return(self.run_redfishtool(redfish_parm, True))
539
George Keishingeafba182021-06-29 13:44:58 -0500540 def verify_ipmi(self):
541 r"""
542 Verify remote host has IPMI LAN service active
543
544 """
545 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
546 + self.hostname + ' power status'
547 return(self.run_ipmitool(ipmi_parm, True))
548
Peter D Phan0c669772021-06-24 13:52:42 -0500549 def run_redfishtool(self,
550 parms_string,
551 quiet=False):
552 r"""
553 Run CLI redfishtool
554
555 Description of variable:
556 parms_string redfishtool subcommand and options.
557 quiet do not print redfishtool error message if True
558 """
559
560 result = subprocess.run(['redfishtool ' + parms_string],
561 stdout=subprocess.PIPE,
562 stderr=subprocess.PIPE,
563 shell=True,
564 universal_newlines=True)
565
566 if result.stderr and not quiet:
567 print('\n\t\tERROR with redfishtool ' + parms_string)
568 print('\t\t' + result.stderr)
569
570 return result.stdout
George Keishingeafba182021-06-29 13:44:58 -0500571
572 def run_ipmitool(self,
573 parms_string,
574 quiet=False):
575 r"""
576 Run CLI IPMI tool.
577
578 Description of variable:
579 parms_string ipmitool subcommand and options.
580 quiet do not print redfishtool error message if True
581 """
582
583 result = subprocess.run(['ipmitool -I lanplus -C 17 ' + parms_string],
584 stdout=subprocess.PIPE,
585 stderr=subprocess.PIPE,
586 shell=True,
587 universal_newlines=True)
588
589 if result.stderr and not quiet:
590 print('\n\t\tERROR with ipmitool -I lanplus -C 17 ' + parms_string)
591 print('\t\t' + result.stderr)
592
593 return result.stdout