blob: a50b494c108491b9ecb00666fc69c45fbabd670b [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
Peter D Phane86d9a52021-07-15 10:42:25 -050011import logging
Peter D Phan72ce6b82021-06-03 06:18:26 -050012import platform
13from errno import EACCES, EPERM
Peter D Phan0c669772021-06-24 13:52:42 -050014import subprocess
Peter D Phan72ce6b82021-06-03 06:18:26 -050015from ssh_utility import SSHRemoteclient
Peter D Phan5963d632021-07-12 09:58:55 -050016from telnet_utility import TelnetRemoteclient
Peter D Phan72ce6b82021-06-03 06:18:26 -050017
18
19class FFDCCollector:
20
21 r"""
22 Sends commands from configuration file to the targeted system to collect log files.
23 Fetch and store generated files at the specified location.
24
25 """
26
Peter D Phan0c669772021-06-24 13:52:42 -050027 def __init__(self,
28 hostname,
29 username,
30 password,
31 ffdc_config,
32 location,
33 remote_type,
Peter D Phane86d9a52021-07-15 10:42:25 -050034 remote_protocol,
35 log_level):
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 """
Peter D Phane86d9a52021-07-15 10:42:25 -050047
48 self.hostname = hostname
49 self.username = username
50 self.password = password
51 self.ffdc_config = ffdc_config
52 self.location = location + "/" + remote_type.upper()
53 self.ssh_remoteclient = None
54 self.telnet_remoteclient = None
55 self.ffdc_dir_path = ""
56 self.ffdc_prefix = ""
57 self.target_type = remote_type.upper()
58 self.remote_protocol = remote_protocol.upper()
59 self.start_time = 0
60 self.elapsed_time = ''
61 self.logger = None
62
63 # Set prefix values for scp files and directory.
64 # Since the time stamp is at second granularity, these values are set here
65 # to be sure that all files for this run will have same timestamps
66 # and they will be saved in the same directory.
67 # self.location == local system for now
68 self.set_ffdc_defaults()
69
70 # Logger for this run. Need to be after set_ffdc_defaults()
71 self.script_logging(getattr(logging, log_level.upper()))
72
73 # Verify top level directory exists for storage
74 self.validate_local_store(self.location)
75
Peter D Phan72ce6b82021-06-03 06:18:26 -050076 if self.verify_script_env():
Peter D Phane86d9a52021-07-15 10:42:25 -050077 # Load default or user define YAML configuration file.
78 with open(self.ffdc_config, 'r') as file:
79 self.ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
80
81 if self.target_type not in self.ffdc_actions.keys():
82 self.logger.error(
83 "\n\tERROR: %s is not listed in %s.\n\n" % (self.target_type, self.ffdc_config))
84 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -050085 else:
Peter D Phan8462faf2021-06-16 12:24:15 -050086 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -050087
88 def verify_script_env(self):
89
90 # Import to log version
91 import click
92 import paramiko
93
94 run_env_ok = True
Peter D Phan0c669772021-06-24 13:52:42 -050095
George Keishingeafba182021-06-29 13:44:58 -050096 redfishtool_version = self.run_redfishtool('-V').split(' ')[2].strip('\n')
97 ipmitool_version = self.run_ipmitool('-V').split(' ')[2]
Peter D Phan0c669772021-06-24 13:52:42 -050098
Peter D Phane86d9a52021-07-15 10:42:25 -050099 self.logger.info("\n\t---- Script host environment ----")
100 self.logger.info("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
101 self.logger.info("\t{:<10} {:<10}".format('Script host os', platform.platform()))
102 self.logger.info("\t{:<10} {:>10}".format('Python', platform.python_version()))
103 self.logger.info("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
104 self.logger.info("\t{:<10} {:>10}".format('click', click.__version__))
105 self.logger.info("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
106 self.logger.info("\t{:<10} {:>9}".format('redfishtool', redfishtool_version))
107 self.logger.info("\t{:<10} {:>12}".format('ipmitool', ipmitool_version))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500108
Peter D Phan8462faf2021-06-16 12:24:15 -0500109 if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
Peter D Phane86d9a52021-07-15 10:42:25 -0500110 self.logger.error("\n\tERROR: Python or python packages do not meet minimum version requirement.")
111 self.logger.error("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500112 run_env_ok = False
113
Peter D Phane86d9a52021-07-15 10:42:25 -0500114 self.logger.info("\t---- End script host environment ----")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500115 return run_env_ok
116
Peter D Phane86d9a52021-07-15 10:42:25 -0500117 def script_logging(self,
118 log_level_attr):
119 r"""
120 Create logger
121
122 """
123 self.logger = logging.getLogger()
124 self.logger.setLevel(log_level_attr)
125 log_file_handler = logging.FileHandler(self.ffdc_dir_path + "collector.log")
126
127 stdout_handler = logging.StreamHandler(sys.stdout)
128 self.logger.addHandler(log_file_handler)
129 self.logger.addHandler(stdout_handler)
130
131 # Turn off paramiko INFO logging
132 logging.getLogger("paramiko").setLevel(logging.WARNING)
133
Peter D Phan72ce6b82021-06-03 06:18:26 -0500134 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500135 r"""
136 Check if target system is ping-able.
137
138 """
George Keishing0662e942021-07-13 05:12:20 -0500139 response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500140 if response == 0:
Peter D Phane86d9a52021-07-15 10:42:25 -0500141 self.logger.info("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500142 return True
143 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500144 self.logger.error(
145 "\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500146 sys.exit(-1)
147
Peter D Phan72ce6b82021-06-03 06:18:26 -0500148 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500149 r"""
150 Initiate FFDC Collection depending on requested protocol.
151
152 """
153
Peter D Phane86d9a52021-07-15 10:42:25 -0500154 self.logger.info("\n\t---- Start communicating with %s ----" % self.hostname)
Peter D Phan7610bc42021-07-06 06:31:05 -0500155 self.start_time = time.time()
George Keishing772c9772021-06-16 23:23:42 -0500156 working_protocol_list = []
Peter D Phan72ce6b82021-06-03 06:18:26 -0500157 if self.target_is_pingable():
George Keishing772c9772021-06-16 23:23:42 -0500158 # Check supported protocol ping,ssh, redfish are working.
159 if self.ssh_to_target_system():
160 working_protocol_list.append("SSH")
George Keishing615fe322021-06-24 01:24:36 -0500161 working_protocol_list.append("SCP")
Peter D Phan0c669772021-06-24 13:52:42 -0500162
163 # Redfish
164 if self.verify_redfish():
165 working_protocol_list.append("REDFISH")
Peter D Phane86d9a52021-07-15 10:42:25 -0500166 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
Peter D Phan0c669772021-06-24 13:52:42 -0500167 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500168 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan0c669772021-06-24 13:52:42 -0500169
Peter D Phan5963d632021-07-12 09:58:55 -0500170 # IPMI
George Keishingeafba182021-06-29 13:44:58 -0500171 if self.verify_ipmi():
172 working_protocol_list.append("IPMI")
Peter D Phane86d9a52021-07-15 10:42:25 -0500173 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
George Keishingeafba182021-06-29 13:44:58 -0500174 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500175 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname)
George Keishingeafba182021-06-29 13:44:58 -0500176
Peter D Phan5963d632021-07-12 09:58:55 -0500177 # Telnet
178 if self.telnet_to_target_system():
179 working_protocol_list.append("TELNET")
180
Peter D Phan72ce6b82021-06-03 06:18:26 -0500181 # Verify top level directory exists for storage
182 self.validate_local_store(self.location)
Peter D Phane86d9a52021-07-15 10:42:25 -0500183 self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500184
185 if ((self.remote_protocol not in working_protocol_list) and (self.remote_protocol != 'ALL')):
Peter D Phane86d9a52021-07-15 10:42:25 -0500186 self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list)
187 self.logger.error(
Peter D Phan0c669772021-06-24 13:52:42 -0500188 '>>>>>\tERROR: Requested protocol %s is not in working protocol list.\n'
189 % self.remote_protocol)
190 sys.exit(-1)
191 else:
192 self.generate_ffdc(working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500193
194 def ssh_to_target_system(self):
195 r"""
196 Open a ssh connection to targeted system.
197
198 """
199
Peter D Phan5963d632021-07-12 09:58:55 -0500200 self.ssh_remoteclient = SSHRemoteclient(self.hostname,
201 self.username,
202 self.password)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500203
Peter D Phan5963d632021-07-12 09:58:55 -0500204 if self.ssh_remoteclient.ssh_remoteclient_login():
Peter D Phane86d9a52021-07-15 10:42:25 -0500205 self.logger.info("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500206
Peter D Phan5963d632021-07-12 09:58:55 -0500207 # Check scp connection.
208 # If scp connection fails,
209 # continue with FFDC generation but skip scp files to local host.
210 self.ssh_remoteclient.scp_connection()
211 return True
212 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500213 self.logger.info("\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500214 return False
215
216 def telnet_to_target_system(self):
217 r"""
218 Open a telnet connection to targeted system.
219 """
220 self.telnet_remoteclient = TelnetRemoteclient(self.hostname,
221 self.username,
222 self.password)
223 if self.telnet_remoteclient.tn_remoteclient_login():
Peter D Phane86d9a52021-07-15 10:42:25 -0500224 self.logger.info("\n\t[Check] %s Telnet connection established.\t [OK]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500225 return True
226 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500227 self.logger.info("\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500228 return False
Peter D Phan72ce6b82021-06-03 06:18:26 -0500229
George Keishing772c9772021-06-16 23:23:42 -0500230 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500231 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500232 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500233
Peter D Phan04aca3b2021-06-21 10:37:18 -0500234 Description of argument(s):
235 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500236 """
237
Peter D Phane86d9a52021-07-15 10:42:25 -0500238 self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----")
239 self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500240
George Keishing86764b52021-07-01 04:32:03 -0500241 ffdc_actions = self.ffdc_actions
Peter D Phan2b8052d2021-06-22 10:55:41 -0500242
Peter D Phan72ce6b82021-06-03 06:18:26 -0500243 for machine_type in ffdc_actions.keys():
George Keishing6ea92b02021-07-01 11:20:50 -0500244 if self.target_type != machine_type:
245 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500246
Peter D Phane86d9a52021-07-15 10:42:25 -0500247 self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path)
248 self.logger.info("\tSystem Type: %s" % machine_type)
George Keishing6ea92b02021-07-01 11:20:50 -0500249 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
Peter D Phanbabf2962021-07-07 11:24:40 -0500255 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SSH' \
256 or ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'SCP':
257 if 'SSH' in working_protocol_list \
258 or 'SCP' in working_protocol_list:
Peter D Phan3beb02e2021-07-06 13:25:17 -0500259 self.protocol_ssh(ffdc_actions, machine_type, k)
260 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500261 self.logger.error("\n\tERROR: SSH or SCP is not available for %s." % self.hostname)
George Keishing6ea92b02021-07-01 11:20:50 -0500262
Peter D Phan5963d632021-07-12 09:58:55 -0500263 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'TELNET':
264 if 'TELNET' in working_protocol_list:
265 self.protocol_telnet(ffdc_actions, machine_type, k)
266 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500267 self.logger.error("\n\tERROR: TELNET is not available for %s." % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500268
George Keishing6ea92b02021-07-01 11:20:50 -0500269 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'REDFISH':
Peter D Phan3beb02e2021-07-06 13:25:17 -0500270 if 'REDFISH' in working_protocol_list:
271 self.protocol_redfish(ffdc_actions, machine_type, k)
272 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500273 self.logger.error("\n\tERROR: REDFISH is not available for %s." % self.hostname)
George Keishing6ea92b02021-07-01 11:20:50 -0500274
275 if ffdc_actions[machine_type][k]['PROTOCOL'][0] == 'IPMI':
Peter D Phan3beb02e2021-07-06 13:25:17 -0500276 if 'IPMI' in working_protocol_list:
277 self.protocol_ipmi(ffdc_actions, machine_type, k)
278 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500279 self.logger.error("\n\tERROR: IMPI is not available for %s." % self.hostname)
George Keishingeafba182021-06-29 13:44:58 -0500280
Peter D Phan04aca3b2021-06-21 10:37:18 -0500281 # Close network connection after collecting all files
Peter D Phan7610bc42021-07-06 06:31:05 -0500282 self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time))
Peter D Phan5963d632021-07-12 09:58:55 -0500283 self.ssh_remoteclient.ssh_remoteclient_disconnect()
284 self.telnet_remoteclient.tn_remoteclient_disconnect()
Peter D Phan04aca3b2021-06-21 10:37:18 -0500285
Peter D Phan0c669772021-06-24 13:52:42 -0500286 def protocol_ssh(self,
287 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500288 machine_type,
289 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500290 r"""
291 Perform actions using SSH and SCP protocols.
292
293 Description of argument(s):
294 ffdc_actions List of actions from ffdc_config.yaml.
295 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500296 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500297 """
298
George Keishing6ea92b02021-07-01 11:20:50 -0500299 if sub_type == 'DUMP_LOGS':
300 self.group_copy(ffdc_actions[machine_type][sub_type])
301 else:
302 self.collect_and_copy_ffdc(ffdc_actions[machine_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500303
Peter D Phan5963d632021-07-12 09:58:55 -0500304 def protocol_telnet(self,
305 ffdc_actions,
306 machine_type,
307 sub_type):
308 r"""
309 Perform actions using telnet protocol.
310 Description of argument(s):
311 ffdc_actions List of actions from ffdc_config.yaml.
312 machine_type OS Type of remote host.
313 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500314 self.logger.info("\n\t[Run] Executing commands on %s using %s" % (self.hostname, 'TELNET'))
Peter D Phan5963d632021-07-12 09:58:55 -0500315 telnet_files_saved = []
316 progress_counter = 0
317 list_of_commands = ffdc_actions[machine_type][sub_type]['COMMANDS']
318 for index, each_cmd in enumerate(list_of_commands, start=0):
319 command_txt, command_timeout = self.unpack_command(each_cmd)
320 result = self.telnet_remoteclient.execute_command(command_txt, command_timeout)
321 if result:
322 try:
323 targ_file = ffdc_actions[machine_type][sub_type]['FILES'][index]
324 except IndexError:
Peter D Phane86d9a52021-07-15 10:42:25 -0500325 targ_file = command_txt
326 self.logger.warning(
327 "\n\t[WARN] Missing filename to store data from telnet %s." % each_cmd)
328 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
Peter D Phan5963d632021-07-12 09:58:55 -0500329 targ_file_with_path = (self.ffdc_dir_path
330 + self.ffdc_prefix
331 + targ_file)
332 # Creates a new file
333 with open(targ_file_with_path, 'wb') as fp:
334 fp.write(result)
335 fp.close
336 telnet_files_saved.append(targ_file)
337 progress_counter += 1
338 self.print_progress(progress_counter)
Peter D Phane86d9a52021-07-15 10:42:25 -0500339 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan5963d632021-07-12 09:58:55 -0500340 for file in telnet_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500341 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
Peter D Phan5963d632021-07-12 09:58:55 -0500342
Peter D Phan0c669772021-06-24 13:52:42 -0500343 def protocol_redfish(self,
344 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500345 machine_type,
346 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500347 r"""
348 Perform actions using Redfish protocol.
349
350 Description of argument(s):
351 ffdc_actions List of actions from ffdc_config.yaml.
352 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500353 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500354 """
355
Peter D Phane86d9a52021-07-15 10:42:25 -0500356 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH'))
Peter D Phan0c669772021-06-24 13:52:42 -0500357 redfish_files_saved = []
358 progress_counter = 0
George Keishing6ea92b02021-07-01 11:20:50 -0500359 list_of_URL = ffdc_actions[machine_type][sub_type]['URL']
Peter D Phan0c669772021-06-24 13:52:42 -0500360 for index, each_url in enumerate(list_of_URL, start=0):
361 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
362 + self.hostname + ' -S Always raw GET ' + each_url
363
364 result = self.run_redfishtool(redfish_parm)
365 if result:
366 try:
Peter D Phanbabf2962021-07-07 11:24:40 -0500367 targ_file = self.get_file_list(ffdc_actions[machine_type][sub_type])[index]
Peter D Phan0c669772021-06-24 13:52:42 -0500368 except IndexError:
369 targ_file = each_url.split('/')[-1]
Peter D Phane86d9a52021-07-15 10:42:25 -0500370 self.logger.warning(
371 "\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url)
372 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
Peter D Phan0c669772021-06-24 13:52:42 -0500373
374 targ_file_with_path = (self.ffdc_dir_path
375 + self.ffdc_prefix
376 + targ_file)
377
378 # Creates a new file
379 with open(targ_file_with_path, 'w') as fp:
380 fp.write(result)
381 fp.close
382 redfish_files_saved.append(targ_file)
383
384 progress_counter += 1
385 self.print_progress(progress_counter)
386
Peter D Phane86d9a52021-07-15 10:42:25 -0500387 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan0c669772021-06-24 13:52:42 -0500388
389 for file in redfish_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500390 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
Peter D Phan0c669772021-06-24 13:52:42 -0500391
George Keishingeafba182021-06-29 13:44:58 -0500392 def protocol_ipmi(self,
393 ffdc_actions,
George Keishing6ea92b02021-07-01 11:20:50 -0500394 machine_type,
395 sub_type):
George Keishingeafba182021-06-29 13:44:58 -0500396 r"""
397 Perform actions using ipmitool over LAN protocol.
398
399 Description of argument(s):
400 ffdc_actions List of actions from ffdc_config.yaml.
401 machine_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500402 sub_type Group type of commands.
George Keishingeafba182021-06-29 13:44:58 -0500403 """
404
Peter D Phane86d9a52021-07-15 10:42:25 -0500405 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI'))
George Keishingeafba182021-06-29 13:44:58 -0500406 ipmi_files_saved = []
407 progress_counter = 0
Peter D Phanbabf2962021-07-07 11:24:40 -0500408 list_of_cmd = self.get_command_list(ffdc_actions[machine_type][sub_type])
George Keishingeafba182021-06-29 13:44:58 -0500409 for index, each_cmd in enumerate(list_of_cmd, start=0):
410 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
411 + self.hostname + ' ' + each_cmd
412
413 result = self.run_ipmitool(ipmi_parm)
414 if result:
415 try:
Peter D Phanbabf2962021-07-07 11:24:40 -0500416 targ_file = self.get_file_list(ffdc_actions[machine_type][sub_type])[index]
George Keishingeafba182021-06-29 13:44:58 -0500417 except IndexError:
George Keishing6ea92b02021-07-01 11:20:50 -0500418 targ_file = each_cmd.split('/')[-1]
Peter D Phane86d9a52021-07-15 10:42:25 -0500419 self.logger.warning("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd)
420 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500421
422 targ_file_with_path = (self.ffdc_dir_path
423 + self.ffdc_prefix
424 + targ_file)
425
426 # Creates a new file
427 with open(targ_file_with_path, 'w') as fp:
428 fp.write(result)
429 fp.close
430 ipmi_files_saved.append(targ_file)
431
432 progress_counter += 1
433 self.print_progress(progress_counter)
434
Peter D Phane86d9a52021-07-15 10:42:25 -0500435 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
George Keishingeafba182021-06-29 13:44:58 -0500436
437 for file in ipmi_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500438 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
George Keishingeafba182021-06-29 13:44:58 -0500439
Peter D Phan04aca3b2021-06-21 10:37:18 -0500440 def collect_and_copy_ffdc(self,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500441 ffdc_actions_for_machine_type,
442 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500443 r"""
444 Send commands in ffdc_config file to targeted system.
445
446 Description of argument(s):
447 ffdc_actions_for_machine_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500448 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500449 """
450
Peter D Phan3beb02e2021-07-06 13:25:17 -0500451 # Executing commands, , if any
452 self.ssh_execute_ffdc_commands(ffdc_actions_for_machine_type,
453 form_filename)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500454
Peter D Phan3beb02e2021-07-06 13:25:17 -0500455 # Copying files
Peter D Phan5963d632021-07-12 09:58:55 -0500456 if self.ssh_remoteclient.scpclient:
Peter D Phane86d9a52021-07-15 10:42:25 -0500457 self.logger.info("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500458
Peter D Phan04aca3b2021-06-21 10:37:18 -0500459 # Retrieving files from target system
Peter D Phanbabf2962021-07-07 11:24:40 -0500460 list_of_files = self.get_file_list(ffdc_actions_for_machine_type)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500461 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500462 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500463 self.logger.info("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500464
Peter D Phanbabf2962021-07-07 11:24:40 -0500465 def get_command_list(self,
466 ffdc_actions_for_machine_type):
467 r"""
468 Fetch list of commands from configuration file
469
470 Description of argument(s):
471 ffdc_actions_for_machine_type commands and files for the selected remote host type.
472 """
473 try:
474 list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
475 except KeyError:
476 list_of_commands = []
477 return list_of_commands
478
479 def get_file_list(self,
480 ffdc_actions_for_machine_type):
481 r"""
482 Fetch list of commands from configuration file
483
484 Description of argument(s):
485 ffdc_actions_for_machine_type commands and files for the selected remote host type.
486 """
487 try:
488 list_of_files = ffdc_actions_for_machine_type['FILES']
489 except KeyError:
490 list_of_files = []
491 return list_of_files
492
Peter D Phan5963d632021-07-12 09:58:55 -0500493 def unpack_command(self,
494 command):
495 r"""
496 Unpack command from config file
497
498 Description of argument(s):
499 command Command from config file.
500 """
501 if isinstance(command, dict):
502 command_txt = next(iter(command))
503 command_timeout = next(iter(command.values()))
504 elif isinstance(command, str):
505 command_txt = command
506 # Default command timeout 60 seconds
507 command_timeout = 60
508
509 return command_txt, command_timeout
510
Peter D Phan3beb02e2021-07-06 13:25:17 -0500511 def ssh_execute_ffdc_commands(self,
512 ffdc_actions_for_machine_type,
513 form_filename=False):
514 r"""
515 Send commands in ffdc_config file to targeted system.
516
517 Description of argument(s):
518 ffdc_actions_for_machine_type commands and files for the selected remote host type.
519 form_filename if true, pre-pend self.target_type to filename
520 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500521 self.logger.info("\n\t[Run] Executing commands on %s using %s"
522 % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
Peter D Phan3beb02e2021-07-06 13:25:17 -0500523
Peter D Phanbabf2962021-07-07 11:24:40 -0500524 list_of_commands = self.get_command_list(ffdc_actions_for_machine_type)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500525 # If command list is empty, returns
526 if not list_of_commands:
527 return
528
529 progress_counter = 0
530 for command in list_of_commands:
Peter D Phan5963d632021-07-12 09:58:55 -0500531 command_txt, command_timeout = self.unpack_command(command)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500532
533 if form_filename:
534 command_txt = str(command_txt % self.target_type)
535
Peter D Phan5963d632021-07-12 09:58:55 -0500536 err, response = self.ssh_remoteclient.execute_command(command_txt, command_timeout)
Peter D Phanbabf2962021-07-07 11:24:40 -0500537
Peter D Phan3beb02e2021-07-06 13:25:17 -0500538 progress_counter += 1
539 self.print_progress(progress_counter)
540
Peter D Phane86d9a52021-07-15 10:42:25 -0500541 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan3beb02e2021-07-06 13:25:17 -0500542
Peter D Phan56429a62021-06-23 08:38:29 -0500543 def group_copy(self,
544 ffdc_actions_for_machine_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500545 r"""
546 scp group of files (wild card) from remote host.
547
548 Description of argument(s):
549 ffdc_actions_for_machine_type commands and files for the selected remote host type.
550 """
Peter D Phan3beb02e2021-07-06 13:25:17 -0500551
Peter D Phan5963d632021-07-12 09:58:55 -0500552 if self.ssh_remoteclient.scpclient:
Peter D Phane86d9a52021-07-15 10:42:25 -0500553 self.logger.info("\n\tCopying DUMP files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500554
Peter D Phanbabf2962021-07-07 11:24:40 -0500555 list_of_commands = self.get_command_list(ffdc_actions_for_machine_type)
556 # If command list is empty, returns
557 if not list_of_commands:
558 return
Peter D Phan56429a62021-06-23 08:38:29 -0500559
Peter D Phanbabf2962021-07-07 11:24:40 -0500560 for command in list_of_commands:
561 try:
562 filename = command.split(' ')[2]
563 except IndexError:
Peter D Phane86d9a52021-07-15 10:42:25 -0500564 self.logger.info("\t\tInvalid command %s for DUMP_LOGS block." % command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500565 continue
566
Peter D Phan5963d632021-07-12 09:58:55 -0500567 err, response = self.ssh_remoteclient.execute_command(command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500568
Peter D Phan56429a62021-06-23 08:38:29 -0500569 if response:
Peter D Phan5963d632021-07-12 09:58:55 -0500570 scp_result = self.ssh_remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
Peter D Phan56429a62021-06-23 08:38:29 -0500571 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500572 self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
Peter D Phan56429a62021-06-23 08:38:29 -0500573 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500574 self.logger.info("\t\tThere is no " + filename)
Peter D Phan56429a62021-06-23 08:38:29 -0500575
576 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500577 self.logger.info("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500578
Peter D Phan72ce6b82021-06-03 06:18:26 -0500579 def scp_ffdc(self,
580 targ_dir_path,
Peter D Phan2b8052d2021-06-22 10:55:41 -0500581 targ_file_prefix,
582 form_filename,
Peter D Phan72ce6b82021-06-03 06:18:26 -0500583 file_list=None,
584 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500585 r"""
586 SCP all files in file_dict to the indicated directory on the local system.
587
588 Description of argument(s):
589 targ_dir_path The path of the directory to receive the files.
590 targ_file_prefix Prefix which will be pre-pended to each
591 target file's name.
592 file_dict A dictionary of files to scp from targeted system to this system
593
594 """
595
Peter D Phan72ce6b82021-06-03 06:18:26 -0500596 progress_counter = 0
597 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500598 if form_filename:
599 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500600 source_file_path = filename
601 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
602
Peter D Phanbabf2962021-07-07 11:24:40 -0500603 # If source file name contains wild card, copy filename as is.
604 if '*' in source_file_path:
Peter D Phan5963d632021-07-12 09:58:55 -0500605 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, self.ffdc_dir_path)
Peter D Phanbabf2962021-07-07 11:24:40 -0500606 else:
Peter D Phan5963d632021-07-12 09:58:55 -0500607 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500608
609 if not quiet:
610 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500611 self.logger.info(
612 "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500613 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500614 self.logger.info(
615 "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500616 else:
617 progress_counter += 1
618 self.print_progress(progress_counter)
619
Peter D Phan72ce6b82021-06-03 06:18:26 -0500620 def set_ffdc_defaults(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500621 r"""
622 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
623 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
624 Individual ffdc file will have timestr_filename.
625
626 Description of class variables:
627 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
628
629 self.ffdc_prefix The prefix to be given to each ffdc file name.
630
631 """
632
633 timestr = time.strftime("%Y%m%d-%H%M%S")
634 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
635 self.ffdc_prefix = timestr + "_"
636 self.validate_local_store(self.ffdc_dir_path)
637
638 def validate_local_store(self, dir_path):
639 r"""
640 Ensure path exists to store FFDC files locally.
641
642 Description of variable:
643 dir_path The dir path where collected ffdc data files will be stored.
644
645 """
646
647 if not os.path.exists(dir_path):
648 try:
George Keishing7b3a5132021-07-13 09:24:02 -0500649 os.makedirs(dir_path, 0o755)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500650 except (IOError, OSError) as e:
651 # PermissionError
652 if e.errno == EPERM or e.errno == EACCES:
Peter D Phane86d9a52021-07-15 10:42:25 -0500653 self.logger.error(
654 '>>>>>\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500655 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500656 self.logger.error(
657 '>>>>>\tERROR: os.makedirs %s failed with %s.\n' % (dir_path, e.strerror))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500658 sys.exit(-1)
659
660 def print_progress(self, progress):
661 r"""
662 Print activity progress +
663
664 Description of variable:
665 progress Progress counter.
666
667 """
668
669 sys.stdout.write("\r\t" + "+" * progress)
670 sys.stdout.flush()
671 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500672
673 def verify_redfish(self):
674 r"""
675 Verify remote host has redfish service active
676
677 """
678 redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
679 + self.hostname + ' -S Always raw GET /redfish/v1/'
680 return(self.run_redfishtool(redfish_parm, True))
681
George Keishingeafba182021-06-29 13:44:58 -0500682 def verify_ipmi(self):
683 r"""
684 Verify remote host has IPMI LAN service active
685
686 """
687 ipmi_parm = '-U ' + self.username + ' -P ' + self.password + ' -H ' \
688 + self.hostname + ' power status'
689 return(self.run_ipmitool(ipmi_parm, True))
690
Peter D Phan0c669772021-06-24 13:52:42 -0500691 def run_redfishtool(self,
692 parms_string,
693 quiet=False):
694 r"""
695 Run CLI redfishtool
696
697 Description of variable:
698 parms_string redfishtool subcommand and options.
699 quiet do not print redfishtool error message if True
700 """
701
702 result = subprocess.run(['redfishtool ' + parms_string],
703 stdout=subprocess.PIPE,
704 stderr=subprocess.PIPE,
705 shell=True,
706 universal_newlines=True)
707
708 if result.stderr and not quiet:
Peter D Phane86d9a52021-07-15 10:42:25 -0500709 self.logger.error('\n\t\tERROR with redfishtool ' + parms_string)
710 self.logger.error('\t\t' + result.stderr)
Peter D Phan0c669772021-06-24 13:52:42 -0500711
712 return result.stdout
George Keishingeafba182021-06-29 13:44:58 -0500713
714 def run_ipmitool(self,
715 parms_string,
716 quiet=False):
717 r"""
718 Run CLI IPMI tool.
719
720 Description of variable:
721 parms_string ipmitool subcommand and options.
722 quiet do not print redfishtool error message if True
723 """
724
725 result = subprocess.run(['ipmitool -I lanplus -C 17 ' + parms_string],
726 stdout=subprocess.PIPE,
727 stderr=subprocess.PIPE,
728 shell=True,
729 universal_newlines=True)
730
731 if result.stderr and not quiet:
Peter D Phane86d9a52021-07-15 10:42:25 -0500732 self.logger.error('\n\t\tERROR with ipmitool -I lanplus -C 17 ' + parms_string)
733 self.logger.error('\t\t' + result.stderr)
George Keishingeafba182021-06-29 13:44:58 -0500734
735 return result.stdout