Skip to content

Commit bf66d88

Browse files
Merge pull request #133 from amd/bargajda_cursor_test
SysSettingsPlugin
2 parents 12ffc3b + 7366fcf commit bf66d88

14 files changed

Lines changed: 1036 additions & 2 deletions

nodescraper/interfaces/dataplugin.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ def collect(
142142
Union[SystemInteractionLevel, str]
143143
] = SystemInteractionLevel.INTERACTIVE,
144144
preserve_connection: bool = False,
145-
collection_args: Optional[Union[TCollectArg, dict]] = None,
145+
collection_args: Optional[TCollectArg] = None,
146146
) -> TaskResult:
147147
"""Run data collector task
148148
149149
Args:
150150
max_event_priority_level (Union[EventPriority, str], optional): priority limit for events. Defaults to EventPriority.CRITICAL.
151151
system_interaction_level (Union[SystemInteractionLevel, str], optional): system interaction level. Defaults to SystemInteractionLevel.INTERACTIVE.
152152
preserve_connection (bool, optional): whether we should close the connection after data collection. Defaults to False.
153-
collection_args (Optional[Union[TCollectArg , dict]], optional): args for data collection. Defaults to None.
153+
collection_args (Optional[TCollectArg], optional): args for data collection (validated model). Defaults to None.
154154
155155
Returns:
156156
TaskResult: task result for data collection
@@ -195,6 +195,13 @@ def collect(
195195
message="Connection not available, data collection skipped",
196196
)
197197
else:
198+
if (
199+
collection_args is not None
200+
and isinstance(collection_args, dict)
201+
and hasattr(self, "COLLECTOR_ARGS")
202+
and self.COLLECTOR_ARGS is not None
203+
):
204+
collection_args = self.COLLECTOR_ARGS.model_validate(collection_args)
198205

199206
collection_task = self.COLLECTOR(
200207
system_info=self.system_info,
@@ -264,6 +271,14 @@ def analyze(
264271
)
265272
return self.analysis_result
266273

274+
if (
275+
analysis_args is not None
276+
and isinstance(analysis_args, dict)
277+
and hasattr(self, "ANALYZER_ARGS")
278+
and self.ANALYZER_ARGS is not None
279+
):
280+
analysis_args = self.ANALYZER_ARGS.model_validate(analysis_args)
281+
267282
analyzer_task = self.ANALYZER(
268283
self.system_info,
269284
logger=self.logger,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 .analyzer_args import SysfsCheck, SysSettingsAnalyzerArgs
27+
from .sys_settings_plugin import SysSettingsPlugin
28+
29+
__all__ = ["SysSettingsPlugin", "SysSettingsAnalyzerArgs", "SysfsCheck"]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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
27+
28+
from pydantic import BaseModel, Field
29+
30+
from nodescraper.models import AnalyzerArgs
31+
32+
33+
class SysfsCheck(BaseModel):
34+
"""One sysfs check: path to read, acceptable values or pattern, and display name.
35+
36+
For file paths: use expected (list of acceptable values); if empty, check passes.
37+
For directory paths: use pattern (regex); at least one directory entry must match (e.g. ^hsn[0-9]+).
38+
"""
39+
40+
path: str
41+
expected: list[str] = Field(default_factory=list)
42+
name: str
43+
pattern: Optional[str] = None
44+
45+
46+
class SysSettingsAnalyzerArgs(AnalyzerArgs):
47+
"""Sysfs settings for analysis via a list of checks (path, expected values, name).
48+
49+
The path in each check is the sysfs path to read; the collector uses these paths
50+
when collection_args is derived from analysis_args (e.g. by the plugin).
51+
"""
52+
53+
checks: Optional[list[SysfsCheck]] = None
54+
55+
def paths_to_collect(self) -> list[str]:
56+
"""Return unique sysfs file paths from checks (those without pattern), for use by the collector."""
57+
if not self.checks:
58+
return []
59+
seen = set()
60+
out = []
61+
for c in self.checks:
62+
if c.pattern:
63+
continue
64+
p = c.path.rstrip("/")
65+
if p not in seen:
66+
seen.add(p)
67+
out.append(c.path)
68+
return out
69+
70+
def paths_to_list(self) -> list[str]:
71+
"""Return unique sysfs directory paths from checks (those with pattern), for listing (ls)."""
72+
if not self.checks:
73+
return []
74+
seen = set()
75+
out = []
76+
for c in self.checks:
77+
if not c.pattern:
78+
continue
79+
p = c.path.rstrip("/")
80+
if p not in seen:
81+
seen.add(p)
82+
out.append(c.path)
83+
return out
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 pydantic import BaseModel
27+
28+
29+
class SysSettingsCollectorArgs(BaseModel):
30+
"""Collection args for SysSettingsCollector.
31+
32+
paths: sysfs paths to read (cat).
33+
directory_paths: sysfs paths to list (ls -1); use for checks that match entry names by regex.
34+
"""
35+
36+
paths: list[str] = []
37+
directory_paths: list[str] = []
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)