xref: /openbmc/openbmc-test-automation/ffdc/ffdc_collector.py (revision 454b791a3560c4e4bcdc78474c3a7071272fa9aa)
1#!/usr/bin/env python
2
3r"""
4See class prolog below for details.
5"""
6
7import os
8import sys
9import yaml
10import time
11import platform
12from errno import EACCES, EPERM
13from ssh_utility import SSHRemoteclient
14
15
16class FFDCCollector:
17
18    r"""
19    Sends commands from configuration file to the targeted system to collect log files.
20    Fetch and store generated files at the specified location.
21
22    """
23
24    # List of supported OSes.
25    supported_oses = ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
26
27    def __init__(self, hostname, username, password, ffdc_config, location, remote_type):
28        r"""
29        Description of argument(s):
30
31        hostname                name/ip of the targeted (remote) system
32        username                user on the targeted system with access to FFDC files
33        password                password for user on targeted system
34        ffdc_config             configuration file listing commands and files for FFDC
35        location                where to store collected FFDC
36        remote_type             os type of the remote host
37
38        """
39        if self.verify_script_env():
40            self.hostname = hostname
41            self.username = username
42            self.password = password
43            self.ffdc_config = ffdc_config
44            self.location = location
45            self.remote_client = None
46            self.ffdc_dir_path = ""
47            self.ffdc_prefix = ""
48            self.target_type = remote_type.upper()
49        else:
50            sys.exit(-1)
51
52    def verify_script_env(self):
53
54        # Import to log version
55        import click
56        import paramiko
57
58        run_env_ok = True
59        print("\n\t---- Script host environment ----")
60        print("\t{:<10}  {:<10}".format('Script hostname', os.uname()[1]))
61        print("\t{:<10}  {:<10}".format('Script host os', platform.platform()))
62        print("\t{:<10}  {:>10}".format('Python', platform.python_version()))
63        print("\t{:<10}  {:>10}".format('PyYAML', yaml.__version__))
64        print("\t{:<10}  {:>10}".format('click', click.__version__))
65        print("\t{:<10}  {:>10}".format('paramiko', paramiko.__version__))
66
67        if eval(yaml.__version__.replace('.', ',')) < (5, 4, 1):
68            print("\n\tERROR: Python or python packages do not meet minimum version requirement.")
69            print("\tERROR: PyYAML version 5.4.1 or higher is needed.\n")
70            run_env_ok = False
71
72        print("\t---- End script host environment ----")
73        return run_env_ok
74
75    def target_is_pingable(self):
76        r"""
77        Check if target system is ping-able.
78
79        """
80        response = os.system("ping -c 1 -w 2 %s  2>&1 >/dev/null" % self.hostname)
81        if response == 0:
82            print("\n\t[Check] %s is ping-able.\t\t\t [OK]" % self.hostname)
83            return True
84        else:
85            print("\n>>>>>\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
86            sys.exit(-1)
87
88    def inspect_target_machine_type(self):
89        r"""
90        Inspect remote host os-release or uname.
91
92        """
93        command = "cat /etc/os-release"
94        response = self.remoteclient.execute_command(command)
95        if response:
96            print("\n\t[INFO] %s /etc/os-release\n" % self.hostname)
97            print("\t\t %s" % self.find_os_type(response, 'PRETTY_NAME'))
98            identity = self.find_os_type(response, 'ID').split('=')[1].upper()
99        else:
100            response = self.remoteclient.execute_command('uname -a')
101            print("\n\t[INFO] %s uname -a\n" % self.hostname)
102            print("\t\t %s" % ' '.join(response))
103            identity = self.find_os_type(response, 'AIX').split(' ')[0].upper()
104
105            # If OS does not have /etc/os-release and is not AIX,
106            # script does not yet know what to do.
107            if not identity:
108                print(">>>>>\tERROR: Script does not yet know about %s" % ' '.join(response))
109                sys.exit(-1)
110
111        if self.target_type not in identity:
112            user_target_type = self.target_type
113            self.target_type = ""
114            for each_os in FFDCCollector.supported_oses:
115                if each_os in identity:
116                    self.target_type = each_os
117                    break
118
119            # If OS in not one of ['OPENBMC', 'RHEL', 'AIX', 'UBUNTU']
120            # script does not yet know what to do.
121            if not self.target_type:
122                print(">>>>>\tERROR: Script does not yet know about %s" % identity)
123                sys.exit(-1)
124
125            print("\n\t[WARN] user request %s does not match remote host type %s.\n"
126                  % (user_target_type, self.target_type))
127            print("\t[WARN] FFDC collection continues for %s.\n" % self.target_type)
128
129    def find_os_type(self,
130                     listing_from_os,
131                     key):
132
133        r"""
134        Return OS information with the requested key
135
136        Description of argument(s):
137
138        listing_from_os    list of information returns from OS command
139        key                key of the desired data
140
141        """
142
143        for each_item in listing_from_os:
144            if key in each_item:
145                return each_item
146        return ''
147
148    def collect_ffdc(self):
149        r"""
150        Initiate FFDC Collection depending on requested protocol.
151
152        """
153
154        print("\n\t---- Start communicating with %s ----" % self.hostname)
155        working_protocol_list = []
156        if self.target_is_pingable():
157            # Check supported protocol ping,ssh, redfish are working.
158            if self.ssh_to_target_system():
159                working_protocol_list.append("SSH")
160            # Verify top level directory exists for storage
161            self.validate_local_store(self.location)
162            self.inspect_target_machine_type()
163            print("\n\t---- Completed protocol pre-requisite check ----\n")
164            self.generate_ffdc(working_protocol_list)
165
166    def ssh_to_target_system(self):
167        r"""
168        Open a ssh connection to targeted system.
169
170        """
171
172        self.remoteclient = SSHRemoteclient(self.hostname,
173                                            self.username,
174                                            self.password)
175
176        self.remoteclient.ssh_remoteclient_login()
177        print("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
178
179        # Check scp connection.
180        # If scp connection fails,
181        # continue with FFDC generation but skip scp files to local host.
182        self.remoteclient.scp_connection()
183        return True
184
185    def generate_ffdc(self, working_protocol_list):
186        r"""
187        Determine actions based on remote host type
188
189        Description of argument(s):
190        working_protocol_list    list of confirmed working protocols to connect to remote host.
191        """
192
193        print("\n\t---- Executing commands on " + self.hostname + " ----")
194        print("\n\tWorking protocol list: %s" % working_protocol_list)
195        with open(self.ffdc_config, 'r') as file:
196            ffdc_actions = yaml.load(file, Loader=yaml.FullLoader)
197
198        # Set prefix values for scp files and directory.
199        # Since the time stamp is at second granularity, these values are set here
200        # to be sure that all files for this run will have same timestamps
201        # and they will be saved in the same directory.
202        # self.location == local system for now
203        self.set_ffdc_defaults()
204
205        for machine_type in ffdc_actions.keys():
206
207            if machine_type == self.target_type:
208                if (ffdc_actions[machine_type]['PROTOCOL'][0] in working_protocol_list):
209
210                    # For OPENBMC collect general system info.
211                    if self.target_type == 'OPENBMC':
212
213                        self.collect_and_copy_ffdc(ffdc_actions['GENERAL'],
214                                                   form_filename=True)
215                        self.group_copy(ffdc_actions['OPENBMC_DUMPS'])
216
217                    # For RHEL and UBUNTU, collect common Linux OS FFDC.
218                    if self.target_type == 'RHEL' \
219                            or self.target_type == 'UBUNTU':
220
221                        self.collect_and_copy_ffdc(ffdc_actions['LINUX'])
222
223                    # Collect remote host specific FFDC.
224                    self.collect_and_copy_ffdc(ffdc_actions[machine_type])
225                else:
226                    print("\n\tProtocol %s is not yet supported by this script.\n"
227                          % ffdc_actions[machine_type]['PROTOCOL'][0])
228
229        # Close network connection after collecting all files
230        self.remoteclient.ssh_remoteclient_disconnect()
231
232    def collect_and_copy_ffdc(self,
233                              ffdc_actions_for_machine_type,
234                              form_filename=False):
235        r"""
236        Send commands in ffdc_config file to targeted system.
237
238        Description of argument(s):
239        ffdc_actions_for_machine_type    commands and files for the selected remote host type.
240        form_filename                    if true, pre-pend self.target_type to filename
241        """
242
243        print("\n\t[Run] Executing commands on %s using %s"
244              % (self.hostname, ffdc_actions_for_machine_type['PROTOCOL'][0]))
245        list_of_commands = ffdc_actions_for_machine_type['COMMANDS']
246        progress_counter = 0
247        for command in list_of_commands:
248            if form_filename:
249                command = str(command % self.target_type)
250            self.remoteclient.execute_command(command)
251            progress_counter += 1
252            self.print_progress(progress_counter)
253
254        print("\n\t[Run] Commands execution completed.\t\t [OK]")
255
256        if self.remoteclient.scpclient:
257            print("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
258
259            # Retrieving files from target system
260            list_of_files = ffdc_actions_for_machine_type['FILES']
261            self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
262        else:
263            print("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
264
265    def group_copy(self,
266                   ffdc_actions_for_machine_type):
267
268        r"""
269        scp group of files (wild card) from remote host.
270
271        Description of argument(s):
272        ffdc_actions_for_machine_type    commands and files for the selected remote host type.
273        """
274        if self.remoteclient.scpclient:
275            print("\n\tCopying files from remote system %s.\n" % self.hostname)
276
277            # Retrieving files from target system, if any
278            list_of_files = ffdc_actions_for_machine_type['FILES']
279
280            for filename in list_of_files:
281                command = 'ls -AX ' + filename
282                response = self.remoteclient.execute_command(command)
283                # self.remoteclient.scp_file_from_remote() completed without exception,
284                # if any
285                if response:
286                    scp_result = self.remoteclient.scp_file_from_remote(filename, self.ffdc_dir_path)
287                    if scp_result:
288                        print("\t\tSuccessfully copied from " + self.hostname + ':' + filename)
289                else:
290                    print("\t\tThere is no  " + filename)
291
292        else:
293            print("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
294
295    def scp_ffdc(self,
296                 targ_dir_path,
297                 targ_file_prefix,
298                 form_filename,
299                 file_list=None,
300                 quiet=None):
301        r"""
302        SCP all files in file_dict to the indicated directory on the local system.
303
304        Description of argument(s):
305        targ_dir_path                   The path of the directory to receive the files.
306        targ_file_prefix                Prefix which will be pre-pended to each
307                                        target file's name.
308        file_dict                       A dictionary of files to scp from targeted system to this system
309
310        """
311
312        progress_counter = 0
313        for filename in file_list:
314            if form_filename:
315                filename = str(filename % self.target_type)
316            source_file_path = filename
317            targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
318
319            # self.remoteclient.scp_file_from_remote() completed without exception,
320            # add file to the receiving file list.
321            scp_result = self.remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
322
323            if not quiet:
324                if scp_result:
325                    print("\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
326                else:
327                    print("\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
328            else:
329                progress_counter += 1
330                self.print_progress(progress_counter)
331
332    def set_ffdc_defaults(self):
333        r"""
334        Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
335        Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
336        Individual ffdc file will have timestr_filename.
337
338        Description of class variables:
339        self.ffdc_dir_path  The dir path where collected ffdc data files should be put.
340
341        self.ffdc_prefix    The prefix to be given to each ffdc file name.
342
343        """
344
345        timestr = time.strftime("%Y%m%d-%H%M%S")
346        self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
347        self.ffdc_prefix = timestr + "_"
348        self.validate_local_store(self.ffdc_dir_path)
349
350    def validate_local_store(self, dir_path):
351        r"""
352        Ensure path exists to store FFDC files locally.
353
354        Description of variable:
355        dir_path  The dir path where collected ffdc data files will be stored.
356
357        """
358
359        if not os.path.exists(dir_path):
360            try:
361                os.mkdir(dir_path, 0o755)
362            except (IOError, OSError) as e:
363                # PermissionError
364                if e.errno == EPERM or e.errno == EACCES:
365                    print('>>>>>\tERROR: os.mkdir %s failed with PermissionError.\n' % dir_path)
366                else:
367                    print('>>>>>\tERROR: os.mkdir %s failed with %s.\n' % (dir_path, e.strerror))
368                sys.exit(-1)
369
370    def print_progress(self, progress):
371        r"""
372        Print activity progress +
373
374        Description of variable:
375        progress  Progress counter.
376
377        """
378
379        sys.stdout.write("\r\t" + "+" * progress)
380        sys.stdout.flush()
381        time.sleep(.1)
382