|
| 1 | +############################################################################### |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2026 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 | +from typing import Optional, cast |
| 27 | + |
| 28 | +from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus |
| 29 | +from nodescraper.interfaces import DataAnalyzer |
| 30 | +from nodescraper.models import TaskResult |
| 31 | + |
| 32 | +from .analyzer_args import SysSettingsAnalyzerArgs |
| 33 | +from .sys_settings_data import SysSettingsDataModel |
| 34 | + |
| 35 | + |
| 36 | +def _get_actual_for_path(data: SysSettingsDataModel, path: str) -> Optional[str]: |
| 37 | + """Return the actual value from the data model for the given sysfs path. |
| 38 | +
|
| 39 | + Args: |
| 40 | + data: Collected sysfs readings (path -> value). |
| 41 | + path: Sysfs path (with or without trailing slash). |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Normalized value for that path, or None if not present. |
| 45 | + """ |
| 46 | + value = data.readings.get(path) or data.readings.get(path.rstrip("/")) |
| 47 | + return (value or "").strip().lower() if value is not None else None |
| 48 | + |
| 49 | + |
| 50 | +class SysSettingsAnalyzer(DataAnalyzer[SysSettingsDataModel, SysSettingsAnalyzerArgs]): |
| 51 | + """Check sysfs settings against expected values from the checks list.""" |
| 52 | + |
| 53 | + DATA_MODEL = SysSettingsDataModel |
| 54 | + |
| 55 | + def analyze_data( |
| 56 | + self, data: SysSettingsDataModel, args: Optional[SysSettingsAnalyzerArgs] = None |
| 57 | + ) -> TaskResult: |
| 58 | + """Compare sysfs data to expected settings from args.checks. |
| 59 | +
|
| 60 | + Args: |
| 61 | + data: Collected sysfs readings to check. |
| 62 | + args: Analyzer args with checks (path, expected, name). If None or no checks, returns OK. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + TaskResult with status OK if all checks pass, ERROR if any mismatch or missing path. |
| 66 | + """ |
| 67 | + mismatches = {} |
| 68 | + |
| 69 | + if not args or not args.checks: |
| 70 | + self.result.status = ExecutionStatus.OK |
| 71 | + self.result.message = "No checks configured." |
| 72 | + return self.result |
| 73 | + |
| 74 | + for check in args.checks: |
| 75 | + actual = _get_actual_for_path(data, check.path) |
| 76 | + if actual is None: |
| 77 | + mismatches[check.name] = { |
| 78 | + "path": check.path, |
| 79 | + "expected": check.expected, |
| 80 | + "actual": None, |
| 81 | + "reason": "path not collected by this plugin", |
| 82 | + } |
| 83 | + continue |
| 84 | + |
| 85 | + if not check.expected: |
| 86 | + continue |
| 87 | + expected_normalized = [e.strip().lower() for e in check.expected] |
| 88 | + if actual not in expected_normalized: |
| 89 | + raw = data.readings.get(check.path) or data.readings.get(check.path.rstrip("/")) |
| 90 | + mismatches[check.name] = { |
| 91 | + "path": check.path, |
| 92 | + "expected": check.expected, |
| 93 | + "actual": raw, |
| 94 | + } |
| 95 | + |
| 96 | + if mismatches: |
| 97 | + self.result.status = ExecutionStatus.ERROR |
| 98 | + parts = [] |
| 99 | + for name, info in mismatches.items(): |
| 100 | + path = info.get("path", "") |
| 101 | + expected = info.get("expected") |
| 102 | + actual = cast(Optional[str], info.get("actual")) |
| 103 | + reason = info.get("reason") |
| 104 | + if reason: |
| 105 | + part = f"{name} ({path})" |
| 106 | + else: |
| 107 | + part = f"{name} ({path}): expected one of {expected}, actual {repr(actual)}" |
| 108 | + parts.append(part) |
| 109 | + self.result.message = "Sysfs mismatch: " + "; ".join(parts) |
| 110 | + self._log_event( |
| 111 | + category=EventCategory.OS, |
| 112 | + description="Sysfs mismatch detected", |
| 113 | + data=mismatches, |
| 114 | + priority=EventPriority.ERROR, |
| 115 | + console_log=True, |
| 116 | + ) |
| 117 | + else: |
| 118 | + self._log_event( |
| 119 | + category=EventCategory.OS, |
| 120 | + description="Sysfs settings match expected", |
| 121 | + priority=EventPriority.INFO, |
| 122 | + console_log=True, |
| 123 | + ) |
| 124 | + self.result.status = ExecutionStatus.OK |
| 125 | + self.result.message = "Sysfs settings as expected." |
| 126 | + |
| 127 | + return self.result |
0 commit comments