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