|
| 1 | +############################################################################### |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Advanced Micro Devices, Inc. |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +############################################################################### |
| 26 | +import os |
| 27 | +import re |
| 28 | + |
| 29 | +from pydantic import ValidationError |
| 30 | + |
| 31 | +from nodescraper.base import InBandDataCollector |
| 32 | +from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily |
| 33 | +from nodescraper.models import TaskResult |
| 34 | + |
| 35 | +from .nvmedata import NvmeDataModel |
| 36 | + |
| 37 | + |
| 38 | +class NvmeCollector(InBandDataCollector[NvmeDataModel, None]): |
| 39 | + """Collect NVMe details from the system.""" |
| 40 | + |
| 41 | + DATA_MODEL = NvmeDataModel |
| 42 | + |
| 43 | + def collect_data( |
| 44 | + self, |
| 45 | + args=None, |
| 46 | + ) -> tuple[TaskResult, NvmeDataModel | None]: |
| 47 | + """Collect detailed NVMe information from all NVMe devices. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + tuple[TaskResult, NvmeDataModel | None]: Task result and data model with NVMe command outputs. |
| 51 | + """ |
| 52 | + if self.system_info.os_family == OSFamily.WINDOWS: |
| 53 | + self._log_event( |
| 54 | + category=EventCategory.SW_DRIVER, |
| 55 | + description="NVMe collection not supported on Windows", |
| 56 | + priority=EventPriority.WARNING, |
| 57 | + ) |
| 58 | + self.result.message = "NVMe data collection skipped on Windows" |
| 59 | + self.result.status = ExecutionStatus.NOT_RAN |
| 60 | + return self.result, None |
| 61 | + |
| 62 | + nvme_devices = self._get_nvme_devices() |
| 63 | + if not nvme_devices: |
| 64 | + self._log_event( |
| 65 | + category=EventCategory.SW_DRIVER, |
| 66 | + description="No NVMe devices found", |
| 67 | + priority=EventPriority.ERROR, |
| 68 | + ) |
| 69 | + self.result.message = "No NVMe devices found" |
| 70 | + self.result.status = ExecutionStatus.ERROR |
| 71 | + return self.result, None |
| 72 | + |
| 73 | + all_device_data = {} |
| 74 | + |
| 75 | + for dev in nvme_devices: |
| 76 | + device_data = {} |
| 77 | + commands = { |
| 78 | + "smart_log": f"nvme smart-log {dev}", |
| 79 | + "error_log": f"nvme error-log {dev} --log-entries=256", |
| 80 | + "id_ctrl": f"nvme id-ctrl {dev}", |
| 81 | + "id_ns": f"nvme id-ns {dev}n1", |
| 82 | + "fw_log": f"nvme fw-log {dev}", |
| 83 | + "self_test_log": f"nvme self-test-log {dev}", |
| 84 | + "get_log": f"nvme get-log {dev} --log-id=6 --log-len=512", |
| 85 | + } |
| 86 | + |
| 87 | + for key, cmd in commands.items(): |
| 88 | + res = self._run_sut_cmd(cmd, sudo=True) |
| 89 | + if res.exit_code == 0: |
| 90 | + device_data[key] = res.stdout |
| 91 | + else: |
| 92 | + self._log_event( |
| 93 | + category=EventCategory.SW_DRIVER, |
| 94 | + description=f"Failed to execute NVMe command: '{cmd}'", |
| 95 | + data={"command": cmd, "exit_code": res.exit_code}, |
| 96 | + priority=EventPriority.WARNING, |
| 97 | + console_log=True, |
| 98 | + ) |
| 99 | + |
| 100 | + if device_data: |
| 101 | + all_device_data[os.path.basename(dev)] = device_data |
| 102 | + |
| 103 | + if all_device_data: |
| 104 | + try: |
| 105 | + nvme_data = NvmeDataModel(devices=all_device_data) |
| 106 | + except ValidationError as exp: |
| 107 | + self._log_event( |
| 108 | + category=EventCategory.SW_DRIVER, |
| 109 | + description="Validation error while building NvmeDataModel", |
| 110 | + data={"errors": exp.errors(include_url=False)}, |
| 111 | + priority=EventPriority.ERROR, |
| 112 | + ) |
| 113 | + self.result.message = "NVMe data invalid format" |
| 114 | + self.result.status = ExecutionStatus.ERROR |
| 115 | + return self.result, None |
| 116 | + |
| 117 | + self._log_event( |
| 118 | + category=EventCategory.SW_DRIVER, |
| 119 | + description="Collected NVMe data", |
| 120 | + data=nvme_data.model_dump(), |
| 121 | + priority=EventPriority.INFO, |
| 122 | + ) |
| 123 | + self.result.message = "NVMe data successfully collected" |
| 124 | + self.result.status = ExecutionStatus.OK |
| 125 | + return self.result, nvme_data |
| 126 | + else: |
| 127 | + self._log_event( |
| 128 | + category=EventCategory.SW_DRIVER, |
| 129 | + description="Failed to collect any NVMe data", |
| 130 | + priority=EventPriority.ERROR, |
| 131 | + ) |
| 132 | + self.result.message = "No NVMe data collected" |
| 133 | + self.result.status = ExecutionStatus.ERROR |
| 134 | + return self.result, None |
| 135 | + |
| 136 | + def _get_nvme_devices(self) -> list[str]: |
| 137 | + nvme_devs = [] |
| 138 | + |
| 139 | + res = self._run_sut_cmd("ls /dev", sudo=False) |
| 140 | + if res.exit_code != 0: |
| 141 | + self._log_event( |
| 142 | + category=EventCategory.SW_DRIVER, |
| 143 | + description="Failed to list /dev directory", |
| 144 | + data={"exit_code": res.exit_code, "stderr": res.stderr}, |
| 145 | + priority=EventPriority.ERROR, |
| 146 | + ) |
| 147 | + return [] |
| 148 | + |
| 149 | + for entry in res.stdout.strip().splitlines(): |
| 150 | + if re.fullmatch(r"nvme\d+$", entry): |
| 151 | + nvme_devs.append(f"/dev/{entry}") |
| 152 | + |
| 153 | + return nvme_devs |
0 commit comments