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