|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +bmc_techsupport script. |
| 5 | + This script is invoked by the generate_dump script for BMC techsupport fetching, |
| 6 | + but also can be invoked manually to trigger and collect BMC debug log dump. |
| 7 | +
|
| 8 | + The usage of this script is divided into two parts: |
| 9 | + 1. Triggering BMC debug log dump Redfish task |
| 10 | + * In this case the script triggers a POST request to BMC to start collecting debug log dump. |
| 11 | + * In this script we will print the new task-id to the console output |
| 12 | + to collect the debug log dump once the task-id has finished. |
| 13 | + * This step is non-blocking, task-id is returned immediately. |
| 14 | + * It is invoked with the parameter '--mode trigger' |
| 15 | + E.g.: /usr/local/bin/bmc_techsupport.py --mode trigger |
| 16 | +
|
| 17 | + 2. Collecting BMC debug log dump |
| 18 | + * In this step we will wait for the task-id to finish if it has not finished. |
| 19 | + * Blocking action until we get the file or encounter an ERROR or Timeout. |
| 20 | + * It is invoked with the parameter '--mode collect --task <task-id> --path <path>' |
| 21 | + E.g.: /usr/local/bin/bmc_techsupport.py --mode collect --path <path> --task <task-id> |
| 22 | +
|
| 23 | + Basically, in the generate_dump script we will call the first method |
| 24 | + at the beginning of its process and the second method towards the end of the process. |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +import argparse |
| 29 | +import os |
| 30 | +import sonic_platform |
| 31 | +import time |
| 32 | +from sonic_py_common.syslogger import SysLogger |
| 33 | + |
| 34 | + |
| 35 | +TIMEOUT_FOR_GET_BMC_DEBUG_LOG_DUMP_IN_SECONDS = 60 |
| 36 | +SYSLOG_IDENTIFIER = "bmc_techsupport" |
| 37 | +log = SysLogger(SYSLOG_IDENTIFIER) |
| 38 | + |
| 39 | + |
| 40 | +class BMCDebugDumpExtractor: |
| 41 | + ''' |
| 42 | + Class to trigger and extract BMC debug log dump |
| 43 | + ''' |
| 44 | + |
| 45 | + INVALID_TASK_ID = '-1' |
| 46 | + TRIGGER_MODE = 'trigger' |
| 47 | + COLLECT_MODE = 'collect' |
| 48 | + |
| 49 | + def __init__(self): |
| 50 | + platform = sonic_platform.platform.Platform() |
| 51 | + chassis = platform.get_chassis() |
| 52 | + self.bmc = chassis.get_bmc() |
| 53 | + |
| 54 | + def trigger_debug_dump(self): |
| 55 | + ''' |
| 56 | + Trigger BMC debug log dump and prints the running task id to the console output |
| 57 | + ''' |
| 58 | + try: |
| 59 | + task_id = BMCDebugDumpExtractor.INVALID_TASK_ID |
| 60 | + log.log_info("Triggering BMC debug log dump Redfish task") |
| 61 | + (ret, (task_id, err_msg)) = self.bmc.trigger_bmc_debug_log_dump() |
| 62 | + if ret != 0: |
| 63 | + raise Exception(err_msg) |
| 64 | + log.log_info(f'Successfully triggered BMC debug log dump - Task-id: {task_id}') |
| 65 | + except Exception as e: |
| 66 | + log.log_error(f'Failed to trigger BMC debug log dump - {str(e)}') |
| 67 | + finally: |
| 68 | + # generate_dump script captures the task id from the console output via $(...) syntax |
| 69 | + print(f'{task_id}') |
| 70 | + |
| 71 | + def extract_debug_dump_file(self, task_id, filepath): |
| 72 | + ''' |
| 73 | + Extract BMC debug log dump file for the given task id and save it to the given filepath |
| 74 | + ''' |
| 75 | + try: |
| 76 | + if task_id is None or task_id == BMCDebugDumpExtractor.INVALID_TASK_ID: |
| 77 | + raise Exception('Invalid Task-ID') |
| 78 | + log_dump_dir = os.path.dirname(filepath) |
| 79 | + log_dump_filename = os.path.basename(filepath) |
| 80 | + if not log_dump_dir or not log_dump_filename: |
| 81 | + raise Exception(f'Invalid given filepath: {filepath}') |
| 82 | + if not log_dump_filename.endswith('.tar.xz'): |
| 83 | + raise Exception(f'Invalid given filepath extension, should be .tar.xz: {log_dump_filename}') |
| 84 | + |
| 85 | + start_time = time.time() |
| 86 | + log.log_info("Collecting BMC debug log dump") |
| 87 | + ret, err_msg = self.bmc.get_bmc_debug_log_dump( |
| 88 | + task_id=task_id, |
| 89 | + filename=log_dump_filename, |
| 90 | + path=log_dump_dir, |
| 91 | + timeout=TIMEOUT_FOR_GET_BMC_DEBUG_LOG_DUMP_IN_SECONDS |
| 92 | + ) |
| 93 | + end_time = time.time() |
| 94 | + duration = end_time - start_time |
| 95 | + if ret != 0: |
| 96 | + timeout_msg = ( |
| 97 | + f'BMC debug log dump does not finish within ' |
| 98 | + f'{TIMEOUT_FOR_GET_BMC_DEBUG_LOG_DUMP_IN_SECONDS} seconds: {err_msg}' |
| 99 | + ) |
| 100 | + log.log_error(timeout_msg) |
| 101 | + raise Exception(err_msg) |
| 102 | + log.log_info(f'Finished successfully collecting BMC debug log dump. Duration: {duration} seconds') |
| 103 | + except Exception as e: |
| 104 | + log.log_error(f'Failed to collect BMC debug log dump - {str(e)}') |
| 105 | + |
| 106 | + |
| 107 | +def main(mode, task_id, filepath): |
| 108 | + try: |
| 109 | + extractor = BMCDebugDumpExtractor() |
| 110 | + if extractor.bmc is None: |
| 111 | + raise Exception('BMC instance is not available') |
| 112 | + except Exception as e: |
| 113 | + log.log_error(f'Failed to initialize BMCDebugDumpExtractor: {str(e)}') |
| 114 | + if mode == BMCDebugDumpExtractor.TRIGGER_MODE: |
| 115 | + print(f'{BMCDebugDumpExtractor.INVALID_TASK_ID}') |
| 116 | + return |
| 117 | + if mode == BMCDebugDumpExtractor.TRIGGER_MODE: |
| 118 | + extractor.trigger_debug_dump() |
| 119 | + elif mode == BMCDebugDumpExtractor.COLLECT_MODE: |
| 120 | + if not task_id or not filepath: |
| 121 | + log.log_error("Both --task and --path arguments are required for 'collect' mode") |
| 122 | + return |
| 123 | + extractor.extract_debug_dump_file(task_id, filepath) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + parser = argparse.ArgumentParser(description="BMC tech-support generator script.") |
| 128 | + parser.add_argument( |
| 129 | + '-m', '--mode', |
| 130 | + choices=['collect', 'trigger'], |
| 131 | + required=True, |
| 132 | + help="Mode of operation: 'collect' for collecting debug dump or 'trigger' for triggering debug dump task." |
| 133 | + ) |
| 134 | + parser.add_argument('-p', '--path', help="Path to save the BMC debug log dump file.") |
| 135 | + parser.add_argument('-t', '--task', help="Task-ID to monitor and collect the debug dump from.") |
| 136 | + args = parser.parse_args() |
| 137 | + mode = args.mode |
| 138 | + task_id = args.task |
| 139 | + filepath = args.path |
| 140 | + main(mode, task_id, filepath) |
0 commit comments