|
| 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 | + |
| 27 | +from nodescraper.base import InBandDataCollector |
| 28 | +from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus, OSFamily |
| 29 | +from nodescraper.models import TaskResult |
| 30 | + |
| 31 | +from .nvmedata import NvmeDataModel |
| 32 | + |
| 33 | + |
| 34 | +class NvmeCollector(InBandDataCollector[NvmeDataModel, None]): |
| 35 | + """Collect NVMe details from the system.""" |
| 36 | + |
| 37 | + DATA_MODEL = NvmeDataModel |
| 38 | + |
| 39 | + def collect_data( |
| 40 | + self, |
| 41 | + args=None, |
| 42 | + ) -> tuple[TaskResult, NvmeDataModel | None]: |
| 43 | + """Collect detailed NVMe information from the system. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + tuple[TaskResult, NvmeDataModel | None]: Task result and data model with NVMe command outputs. |
| 47 | + """ |
| 48 | + data = {} |
| 49 | + |
| 50 | + if self.system_info.os_family == OSFamily.WINDOWS: |
| 51 | + self._log_event( |
| 52 | + category=EventCategory.SW_DRIVER, |
| 53 | + description="NVMe collection not supported on Windows", |
| 54 | + priority=EventPriority.WARNING, |
| 55 | + ) |
| 56 | + self.result.message = "NVMe data collection skipped on Windows" |
| 57 | + self.result.status = ExecutionStatus.SKIPPED |
| 58 | + return self.result, None |
| 59 | + |
| 60 | + commands = [ |
| 61 | + "nvme smart-log /dev/nvme0", |
| 62 | + "nvme error-log /dev/nvme0 --log-entries=256", |
| 63 | + "nvme id-ctrl /dev/nvme0", |
| 64 | + "nvme id-ns /dev/nvme0", |
| 65 | + "nvme fw-log /dev/nvme0", |
| 66 | + "nvme self-test-log /dev/nvme0", |
| 67 | + "nvme telemetry-log /dev/nvme0", |
| 68 | + ] |
| 69 | + |
| 70 | + for cmd in commands: |
| 71 | + res = self._run_sut_cmd(cmd) |
| 72 | + if res.exit_code == 0: |
| 73 | + data[cmd] = res.stdout |
| 74 | + else: |
| 75 | + self._log_event( |
| 76 | + category=EventCategory.SW_DRIVER, |
| 77 | + description="Failed to execute NVMe command", |
| 78 | + data={"command": cmd, "exit_code": res.exit_code}, |
| 79 | + priority=EventPriority.ERROR, |
| 80 | + console_log=True, |
| 81 | + ) |
| 82 | + |
| 83 | + if data: |
| 84 | + nvme_data = NvmeDataModel(nvme_data=data) |
| 85 | + self._log_event( |
| 86 | + category=EventCategory.SW_DRIVER, |
| 87 | + description="Collected NVMe data", |
| 88 | + data=nvme_data.model_dump(), |
| 89 | + priority=EventPriority.INFO, |
| 90 | + ) |
| 91 | + self.result.message = "NVMe data successfully collected" |
| 92 | + else: |
| 93 | + nvme_data = None |
| 94 | + self._log_event( |
| 95 | + category=EventCategory.SW_DRIVER, |
| 96 | + description="Failed to collect any NVMe data", |
| 97 | + priority=EventPriority.CRITICAL, |
| 98 | + ) |
| 99 | + self.result.message = "No NVMe data collected" |
| 100 | + self.result.status = ExecutionStatus.ERROR |
| 101 | + |
| 102 | + return self.result, nvme_data |
0 commit comments