1#!/usr/bin/env python3
2r"""
3#    @file     openbmc_ffdc_list.py
4#    @brief    List for FFDC ( First failure data capture )
5#              commands and files to be collected as a part
6#              of the test case failure.
7"""
8import os
9
10from robot.libraries.BuiltIn import BuiltIn
11
12# -------------------
13# FFDC default list
14# -------------------
15# -----------------------------------------------------------------
16# Dict Name {  Index string : { Key String :  Command string} }
17# -----------------------------------------------------------------
18# Add cmd's needed to be part of the ffdc report manifest file
19FFDC_BMC_CMD = {
20    'DRIVER INFO':
21    {
22        # String Name         Command
23        'FW Level': 'cat /etc/os-release',
24        'FW Timestamp': 'cat /etc/timestamp',
25    },
26    'BMC DATA':
27    {
28        'BMC OS': 'uname -a',
29        'BMC Uptime': 'uptime;cat /proc/uptime',
30        'BMC File System Disk Space Usage': 'df -hT',
31        'BMC Date Time': 'date;/sbin/hwclock --show;/usr/bin/timedatectl'
32    },
33    'APPLICATION DATA':
34    {
35        'BMC state': '/usr/bin/obmcutil state',
36        'bmcweb data': 'for f in `find /var/lib/bmcweb/ -name "*.json"`; do (echo $f; cat $f) done',
37    },
38}
39# Add file name and corresponding command needed for BMC
40FFDC_BMC_FILE = {
41    'BMC FILES':
42    {
43        # File Name         Command
44        'BMC_flash_side.txt': 'cat /sys/class/watchdog/watchdog1/bootstatus >/tmp/BMC_flash_side.txt 2>&1',
45        'BMC_hwmon.txt': 'grep -r . /sys/class/hwmon/* >/tmp/BMC_hwmon.txt 2>&1',
46        'BMC_proc_list.txt': 'top -n 1 -b >/tmp/BMC_proc_list.txt 2>&1',
47        'BMC_proc_fd_active_list.txt': 'ls -Al /proc/*/fd/ >/tmp/BMC_proc_fd_active_list.txt 2>&1',
48        'BMC_journalctl_nopager.txt': 'journalctl --no-pager >/tmp/BMC_journalctl_nopager.txt 2>&1',
49        'BMC_journalctl_pretty.json': 'journalctl -o json-pretty >/tmp/BMC_journalctl_pretty.json 2>&1',
50        'BMC_dmesg.txt': 'dmesg >/tmp/BMC_dmesg.txt 2>&1',
51        'BMC_procinfo.txt': 'cat /proc/cpuinfo >/tmp/BMC_procinfo.txt 2>&1',
52        'BMC_meminfo.txt': 'cat /proc/meminfo >/tmp/BMC_meminfo.txt 2>&1',
53        'BMC_systemd.txt': 'systemctl status --all >/tmp/BMC_systemd.txt 2>&1',
54        'BMC_failed_service.txt': 'systemctl list-units --failed >/tmp/BMC_failed_service.txt 2>&1',
55        'BMC_list_service.txt': 'systemctl list-jobs >/tmp/BMC_list_service.txt 2>&1',
56        'BMC_obmc_console.txt': 'cat /var/log/obmc-console.log >/tmp/BMC_obmc_console.txt 2>&1',
57        'BMC_obmc_console1.txt': 'cat /var/log/obmc-console1.log >/tmp/BMC_obmc_console1.txt 2>&1',
58        'PEL_logs_list.json': 'peltool -l >/tmp/PEL_logs_list.json 2>&1',
59        'PEL_logs_display.json': 'peltool -a >/tmp/PEL_logs_display.json 2>&1',
60        'BMC_pldm_flight_recorder.txt': 'rm -rf /tmp/pldm_flight_recorder; killall -s SIGUSR1 pldmd;'
61        + ' sleep 5; cat /tmp/pldm_flight_recorder > /tmp/BMC_pldm_flight_recorder.txt 2>&1;',
62        'OCC_state.txt': 'for i in {0..3};'
63        + ' do (echo /org/open_power/control/occ$i;'
64        + ' busctl get-property org.open_power.OCC.Control /org/open_power/control/occ$i'
65        + ' org.open_power.OCC.Status OccActive) done > /tmp/OCC_state.txt 2>&1',
66    },
67}
68# Add file name and corresponding command needed for all Linux distributions
69FFDC_OS_ALL_DISTROS_FILE = {
70    'OS FILES':
71    {
72        # File Name         Command
73        'OS_msglog.txt': 'cat /sys/firmware/opal/msglog >/tmp/OS_msglog.txt 2>&1',
74        'OS_cpufrequency.txt': 'ppc64_cpu --frequency '
75        + '>/tmp/OS_cpufrequency.txt 2>&1',
76        'OS_dmesg.txt': 'dmesg >/tmp/OS_dmesg.txt 2>&1',
77        'OS_opal_prd.txt': 'cat /var/log/opal-prd* >/tmp/OS_opal_prd.txt 2>&1',
78        'OS_boot.txt': 'cat /var/log/boot.log >/tmp/OS_boot.txt 2>&1',
79        'OS_procinfo.txt': 'cat /proc/cpuinfo >/tmp/OS_procinfo.txt 2>&1',
80        'OS_meminfo.txt': 'cat /proc/meminfo >/tmp/OS_meminfo.txt 2>&1',
81        'OS_netstat.txt': 'netstat -a >/tmp/OS_netstat.txt 2>&1',
82        'OS_lspci.txt': 'lspci >/tmp/OS_lspci.txt 2>&1',
83        'OS_lscpu.txt': 'lscpu >/tmp/OS_lscpu.txt 2>&1',
84        'OS_lscfg.txt': 'lscfg >/tmp/OS_lscfg.txt 2>&1',
85        'OS_journalctl_nopager.txt': 'journalctl --no-pager -b '
86        + '> /tmp/OS_journalctl_nopager.txt  2>&1',
87    },
88}
89# Add file name and corresponding command needed for Ubuntu Linux
90FFDC_OS_UBUNTU_FILE = {
91    'OS FILES':
92    {
93        # File Name         Command
94        'OS_isusb.txt': '{ lsusb -t ; lsusb -v ; } >/tmp/OS_isusb.txt 2>&1',
95        'OS_kern.txt': 'tail -n 50000 /var/log/kern.log >/tmp/OS_kern.txt 2>&1',
96        'OS_authlog.txt': '{ cat /var/log/auth.log; cat /var/log/auth.log.1 ; } '
97        + '>/tmp/OS_authlog.txt 2>&1',
98        'OS_syslog.txt': 'tail -n 200000 /var/log/syslog >/tmp/OS_syslog.txt 2>&1',
99        'OS_info.txt': '{ uname -a; dpkg -s opal-prd; dpkg -s ipmitool ; } '
100        + '>/tmp/OS_info.txt 2>&1',
101        'OS_sosreport.txt': '{ rm -rf /tmp/sosreport*FFDC* ; sosreport --batch --tmp-dir '
102        + '/tmp --ticket-number FFDC ; } >/tmp/OS_sosreport.txt 2>&1',
103    },
104}
105# Add file name and corresponding command needed for RHEL Linux
106FFDC_OS_RHEL_FILE = {
107    'OS FILES':
108    {
109        # File Name         Command
110        'OS_rsct.txt': '/usr/bin/ctversion -bv >/tmp/OS_rsct.txt 2>&1',
111        'OS_secure.txt': 'cat /var/log/secure >/tmp/OS_secure.txt 2>&1',
112        'OS_syslog.txt': 'tail -n 200000 /var/log/messages '
113        + '>/tmp/OS_syslog.txt 2>&1',
114        'OS_info.txt': '{ lsb_release -a; cat /etc/redhat-release; '
115        + 'uname -a; rpm -qa ; } >/tmp/OS_info.txt 2>&1',
116        'OS_sosreport.txt': '{ rm -rf /tmp/sosreport*FFDC* ; sosreport --batch --tmp-dir '
117        + '/tmp --label FFDC ; } >/tmp/OS_sosreport.txt 2>&1',
118    },
119}
120# Add file name and corresponding command needed for AIX.
121FFDC_OS_AIX_FILE = {
122    'OS FILES':
123    {
124        # File Name         Command
125        'OS_errpt.txt': 'errpt >/tmp/OS_errpt.txt 2>&1 ; errclear 0;',
126        'OS_processors.txt': 'bindprocessor -q >/tmp/OS_processors.txt 2>&1',
127    },
128}
129
130try:
131    redfish_support_trans_state = os.environ.get('REDFISH_SUPPORT_TRANS_STATE', 0) or \
132        int(BuiltIn().get_variable_value("${REDFISH_SUPPORT_TRANS_STATE}", default=0))
133except RobotNotRunningError:
134    pass
135
136OPENBMC_BASE = '/xyz/openbmc_project/'
137OPENPOWER_BASE = '/org/open_power/'
138ENUMERATE_SENSORS = OPENBMC_BASE + 'sensors/enumerate'
139ENUMERATE_INVENTORY = OPENBMC_BASE + 'inventory/enumerate'
140ENUMERATE_ELOG = OPENBMC_BASE + 'logging/entry/enumerate'
141ENUMERATE_LED = OPENBMC_BASE + 'led/enumerate'
142ENUMERATE_SW = OPENBMC_BASE + 'software/enumerate'
143ENUMERATE_CONTROL = OPENBMC_BASE + 'control/enumerate'
144ENUMERATE_STATE = OPENBMC_BASE + 'state/enumerate'
145ENUMERATE_OCC = OPENPOWER_BASE + '/enumerate'
146ENUMERATE_DUMPS = OPENBMC_BASE + 'dumps/enumerate'
147ENUMERATE_USER = OPENBMC_BASE + 'user/enumerate'
148
149# Add file name and corresponding Get Request
150FFDC_GET_REQUEST = {
151    'GET REQUESTS':
152    {
153        # File Name         Command
154        'FIRMWARE_list.txt': ENUMERATE_SW,
155        'BMC_sensor_list.txt': ENUMERATE_SENSORS,
156        'BMC_control_list.txt': ENUMERATE_CONTROL,
157        'BMC_inventory.txt': ENUMERATE_INVENTORY,
158        'BMC_elog.txt': ENUMERATE_ELOG,
159        'BMC_led.txt': ENUMERATE_LED,
160        'BMC_state.txt': ENUMERATE_STATE,
161        'OCC_state.txt': ENUMERATE_OCC,
162        'BMC_dumps.txt': ENUMERATE_DUMPS,
163        'BMC_USER.txt': ENUMERATE_USER,
164    },
165}
166
167# Remove the REST dictionary elements.
168if redfish_support_trans_state == 1:
169    for key in list(FFDC_GET_REQUEST):
170        del FFDC_GET_REQUEST[key]
171
172REDFISH_BASE = '/redfish/v1/'
173REDFISH_ELOG = REDFISH_BASE + 'Systems/system/LogServices/EventLog/Entries'
174REDFISH_FIRMWARE = REDFISH_BASE + 'UpdateService/FirmwareInventory'
175
176# Add file name and corresponding Get Request
177FFDC_GET_REDFISH_REQUEST = {
178    'GET REQUESTS':
179    {
180        # File Name         Command
181        'BMC_redfish_elog.txt': REDFISH_ELOG,
182    },
183}
184
185# Define your keywords in method/utils and call here
186FFDC_METHOD_CALL = {
187    'BMC LOGS':
188    {
189        # Description               Keyword name
190        'FFDC Generic Report': 'BMC FFDC Manifest',
191        'BMC Specific Files': 'BMC FFDC Files',
192        'Get Request FFDC': 'BMC FFDC Get Requests',
193        'Get Redfish Request FFDC': 'BMC FFDC Get Redfish Requests',
194        'OS FFDC': 'OS FFDC Files',
195        'Core Files': 'SCP Coredump Files',
196        'SEL Log': 'Collect eSEL Log',
197        'Sys Inventory Files': 'System Inventory Files',
198        'Dump Log': 'Collect Dump Log',
199        'Dump Files': 'SCP Dump Files',
200        'Dump HB Files': 'SCP Dump HB Files',
201        'PEL Files': 'Collect PEL Log',
202        'Redfish Log': 'Enumerate Redfish Resources',
203        'Firmware Log': 'Enumerate Redfish Resources  '
204        + ' enum_uri=/redfish/v1/UpdateService/FirmwareInventory  '
205        + ' file_enum_name=redfish_FIRMWARE_list.txt',
206        'Redfish OEM Log': 'Enumerate Redfish OEM Resources',
207    },
208}
209
210try:
211    platform_arch_type = os.environ.get('PLATFORM_ARCH_TYPE', '') or \
212        BuiltIn().get_variable_value("${PLATFORM_ARCH_TYPE}", default="power")
213except RobotNotRunningError:
214    pass
215
216# Filter the logs based on platform type.
217if platform_arch_type == "x86":
218    del FFDC_BMC_FILE['BMC FILES']['PEL_logs_list.json']
219    del FFDC_BMC_FILE['BMC FILES']['PEL_logs_display.json']
220    del FFDC_METHOD_CALL['BMC LOGS']['PEL Files']
221
222# -----------------------------------------------------------------
223# base class for FFDC default list
224
225
226class openbmc_ffdc_list():
227    def get_ffdc_bmc_cmd(self, i_type):
228        r"""
229        #######################################################################
230        #   @brief    This method returns the list from the dictionary for cmds
231        #   @param    i_type: @type string: string index lookup
232        #   @return   List of key pair from the dictionary
233        #######################################################################
234        """
235        return FFDC_BMC_CMD[i_type].items()
236
237    def get_ffdc_bmc_file(self, i_type):
238        r"""
239        #######################################################################
240        #   @brief    This method returns the list from the dictionary for scp
241        #   @param    i_type: @type string: string index lookup
242        #   @return   List of key pair from the dictionary
243        #######################################################################
244        """
245        return FFDC_BMC_FILE[i_type].items()
246
247    def get_ffdc_get_request(self, i_type):
248        r"""
249        #######################################################################
250        #   @brief    This method returns the list from the dictionary for scp
251        #   @param    i_type: @type string: string index lookup
252        #   @return   List of key pair from the dictionary
253        #######################################################################
254        """
255        return FFDC_GET_REQUEST[i_type].items()
256
257    def get_ffdc_get_redfish_request(self, i_type):
258        r"""
259        #######################################################################
260        #   @brief    This method returns the list from the dictionary for scp
261        #   @param    i_type: @type string: string index lookup
262        #   @return   List of key pair from the dictionary
263        #######################################################################
264        """
265        return FFDC_GET_REDFISH_REQUEST[i_type].items()
266
267    def get_ffdc_cmd_index(self):
268        r"""
269        #######################################################################
270        #   @brief    This method returns the list index from dictionary
271        #   @return   List of index to the dictionary
272        #######################################################################
273        """
274        return FFDC_BMC_CMD.keys()
275
276    def get_ffdc_get_request_index(self):
277        r"""
278        #######################################################################
279        #   @brief    This method returns the list index from dictionary
280        #   @return   List of index to the dictionary
281        #######################################################################
282        """
283        return FFDC_GET_REQUEST.keys()
284
285    def get_ffdc_get_redfish_request_index(self):
286        r"""
287        #######################################################################
288        #   @brief    This method returns the list index from dictionary
289        #   @return   List of index to the dictionary
290        #######################################################################
291        """
292        return FFDC_GET_REDFISH_REQUEST.keys()
293
294    def get_ffdc_file_index(self):
295        r"""
296        #######################################################################
297        #   @brief    This method returns the list index from dictionary
298        #   @return   List of index to the dictionary
299        #######################################################################
300        """
301        return FFDC_BMC_FILE.keys()
302
303    def get_ffdc_method_index(self):
304        r"""
305        #######################################################################
306        #   @brief    This method returns the key pair from the dictionary
307        #   @return   Index of the method dictionary
308        #######################################################################
309        """
310        return FFDC_METHOD_CALL.keys()
311
312    def get_ffdc_method_desc(self,
313                             index):
314        r"""
315        #######################################################################
316        #   @brief   This method returns the just the keys from the dictionary.
317        #   @return  List of ffdc descriptions.
318        #######################################################################
319        """
320        return FFDC_METHOD_CALL[index].keys()
321
322    def get_ffdc_method_call(self, i_type):
323        r"""
324        #######################################################################
325        #   @brief    This method returns the key pair from the dictionary
326        #   @return   List of key pair keywords
327        #######################################################################
328        """
329        return FFDC_METHOD_CALL[i_type].items()
330
331    def get_ffdc_os_all_distros_index(self):
332        r"""
333        #######################################################################
334        #   @brief    This method returns the key pair from the dictionary
335        #   @return   Index of the method dictionary
336        #######################################################################
337        """
338        return FFDC_OS_ALL_DISTROS_FILE.keys()
339
340    def get_ffdc_os_all_distros_call(self, i_type):
341        r"""
342        #######################################################################
343        #   @brief    This method returns the key pair from the dictionary
344        #   @return   List of key pair keywords
345        #######################################################################
346        """
347        return FFDC_OS_ALL_DISTROS_FILE[i_type].items()
348
349    def get_ffdc_os_distro_index(self, distro):
350        r"""
351        #######################################################################
352        #   @brief    This method returns the key pair from the dictionary
353        #   @return   Index of the method dictionary
354        #######################################################################
355        """
356        distro_file = "FFDC_OS_" + str(distro).upper() + "_FILE"
357        return eval(distro_file).keys()
358
359    def get_ffdc_os_distro_call(self, i_type, distro):
360        r"""
361        #######################################################################
362        #   @brief    This method returns the key pair from the dictionary
363        #   @return   List of key pair keywords
364        #######################################################################
365        """
366        distro_file = "FFDC_OS_" + str(distro).upper() + "_FILE"
367        return eval(distro_file)[i_type].items()
368
369    def get_strip_string(self, i_str):
370        r"""
371        #######################################################################
372        #   @brief    Returns the stripped strings
373        #   @param    i_str: @type string: string name
374        #   @return   Remove all special chars and return the string
375        #######################################################################
376        """
377        return ''.join(e for e in i_str if e.isalnum())
378
379    def get_esel_index(self, esel_list):
380        r"""
381        #######################################################################
382        #   @brief    Returns the eSEL binary index.
383        #   @param    esel_ist: @type list: eSEL list.
384        #   @return   Index of "ESEL=" in the list.
385        #######################################################################
386        """
387        index = [i for i, str in enumerate(esel_list) if 'ESEL=' in str]
388        return index[0]
389
390    def get_dump_index(self, dump_list):
391        r"""
392        #######################################################################
393        #   @brief    Returns the eSEL binary index.
394        #   @param    esel_ist: @type list: eSEL list.
395        #   @return   Index of "ESEL=" in the list.
396        #######################################################################
397        """
398        index = [i for i, str in enumerate(dump_list) if 'DUMP=' in str]
399        return index[0]
400