|
| 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 | +import re |
| 27 | +from typing import List, Optional, Union, cast |
| 28 | + |
| 29 | +from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus |
| 30 | +from nodescraper.interfaces import DataAnalyzer |
| 31 | +from nodescraper.models import TaskResult |
| 32 | + |
| 33 | +from .analyzer_args import SysSettingsAnalyzerArgs |
| 34 | +from .sys_settings_data import SysSettingsDataModel |
| 35 | + |
| 36 | + |
| 37 | +def _get_actual_for_path(data: SysSettingsDataModel, path: str) -> Optional[str]: |
| 38 | + """Return the actual value from the data model for the given sysfs path. |
| 39 | +
|
| 40 | + Args: |
| 41 | + data: Collected sysfs readings (path -> value). |
| 42 | + path: Sysfs path (with or without trailing slash). |
| 43 | +
|
| 44 | + Returns: |
| 45 | + Normalized value for that path, or None if not present. |
| 46 | + """ |
| 47 | + value = data.readings.get(path) or data.readings.get(path.rstrip("/")) |
| 48 | + return (value or "").strip().lower() if value is not None else None |
| 49 | + |
| 50 | + |
| 51 | +class SysSettingsAnalyzer(DataAnalyzer[SysSettingsDataModel, SysSettingsAnalyzerArgs]): |
| 52 | + """Check sysfs settings against expected values from the checks list.""" |
| 53 | + |
| 54 | + DATA_MODEL = SysSettingsDataModel |
| 55 | + |
| 56 | + def analyze_data( |
| 57 | + self, data: SysSettingsDataModel, args: Optional[SysSettingsAnalyzerArgs] = None |
| 58 | + ) -> TaskResult: |
| 59 | + """Compare sysfs data to expected settings from args.checks. |
| 60 | +
|
| 61 | + Args: |
| 62 | + data: Collected sysfs readings to check. |
| 63 | + args: Analyzer args with checks (path, expected, name). If None or no checks, returns OK. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + TaskResult with status OK if all checks pass, ERROR if any mismatch or missing path. |
| 67 | + """ |
| 68 | + mismatches: dict[str, dict[str, Union[Optional[str], List[str]]]] = {} |
| 69 | + |
| 70 | + if not args or not args.checks: |
| 71 | + self.result.status = ExecutionStatus.OK |
| 72 | + self.result.message = "No checks configured." |
| 73 | + return self.result |
| 74 | + |
| 75 | + for check in args.checks: |
| 76 | + raw_reading = data.readings.get(check.path) or data.readings.get(check.path.rstrip("/")) |
| 77 | + |
| 78 | + if check.pattern: |
| 79 | + # Directory-listing check: at least one line must match the regex (e.g. ^hsn[0-9]+) |
| 80 | + if raw_reading is None: |
| 81 | + mismatches[check.name] = { |
| 82 | + "path": check.path, |
| 83 | + "pattern": check.pattern, |
| 84 | + "actual": None, |
| 85 | + "reason": "path not collected by this plugin", |
| 86 | + } |
| 87 | + continue |
| 88 | + try: |
| 89 | + pat = re.compile(check.pattern) |
| 90 | + except re.error: |
| 91 | + mismatches[check.name] = { |
| 92 | + "path": check.path, |
| 93 | + "pattern": check.pattern, |
| 94 | + "reason": "invalid regex", |
| 95 | + } |
| 96 | + continue |
| 97 | + lines = [ln.strip() for ln in raw_reading.splitlines() if ln.strip()] |
| 98 | + if not any(pat.search(ln) for ln in lines): |
| 99 | + mismatches[check.name] = { |
| 100 | + "path": check.path, |
| 101 | + "pattern": check.pattern, |
| 102 | + "actual": lines, |
| 103 | + } |
| 104 | + continue |
| 105 | + |
| 106 | + actual = _get_actual_for_path(data, check.path) |
| 107 | + if actual is None: |
| 108 | + mismatches[check.name] = { |
| 109 | + "path": check.path, |
| 110 | + "expected": check.expected, |
| 111 | + "actual": None, |
| 112 | + "reason": "path not collected by this plugin", |
| 113 | + } |
| 114 | + continue |
| 115 | + |
| 116 | + if not check.expected: |
| 117 | + continue |
| 118 | + expected_normalized = [e.strip().lower() for e in check.expected] |
| 119 | + if actual not in expected_normalized: |
| 120 | + raw = data.readings.get(check.path) or data.readings.get(check.path.rstrip("/")) |
| 121 | + mismatches[check.name] = { |
| 122 | + "path": check.path, |
| 123 | + "expected": check.expected, |
| 124 | + "actual": raw, |
| 125 | + } |
| 126 | + |
| 127 | + if mismatches: |
| 128 | + self.result.status = ExecutionStatus.ERROR |
| 129 | + parts = [] |
| 130 | + for name, info in mismatches.items(): |
| 131 | + path = info.get("path", "") |
| 132 | + reason = info.get("reason") |
| 133 | + pattern = info.get("pattern") |
| 134 | + if reason: |
| 135 | + part = f"{name} ({path}): {reason}" |
| 136 | + elif pattern is not None: |
| 137 | + part = f"{name} ({path}): no entry matching pattern {pattern!r}" |
| 138 | + else: |
| 139 | + expected = info.get("expected") |
| 140 | + actual = cast(Optional[str], info.get("actual")) |
| 141 | + part = f"{name} ({path}): expected one of {expected}, actual {repr(actual)}" |
| 142 | + parts.append(part) |
| 143 | + self.result.message = "Sysfs mismatch: " + "; ".join(parts) |
| 144 | + self._log_event( |
| 145 | + category=EventCategory.OS, |
| 146 | + description="Sysfs mismatch detected", |
| 147 | + data=mismatches, |
| 148 | + priority=EventPriority.ERROR, |
| 149 | + console_log=True, |
| 150 | + ) |
| 151 | + else: |
| 152 | + self._log_event( |
| 153 | + category=EventCategory.OS, |
| 154 | + description="Sysfs settings match expected", |
| 155 | + priority=EventPriority.INFO, |
| 156 | + console_log=True, |
| 157 | + ) |
| 158 | + self.result.status = ExecutionStatus.OK |
| 159 | + self.result.message = "Sysfs settings as expected." |
| 160 | + |
| 161 | + return self.result |
0 commit comments