|
| 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 .sysctldata import SysctlDataModel |
| 32 | + |
| 33 | + |
| 34 | +class SysctlCollector(InBandDataCollector[SysctlDataModel, None]): |
| 35 | + """Collect sysctl kernel VM settings.""" |
| 36 | + |
| 37 | + DATA_MODEL = SysctlDataModel |
| 38 | + |
| 39 | + def collect_data( |
| 40 | + self, |
| 41 | + args=None, |
| 42 | + ) -> tuple[TaskResult, SysctlDataModel | None]: |
| 43 | + """Collect sysctl VM tuning values from the system.""" |
| 44 | + values = {} |
| 45 | + |
| 46 | + if self.system_info.os_family == OSFamily.WINDOWS: |
| 47 | + self._log_event( |
| 48 | + category=EventCategory.OS, |
| 49 | + description="Windows is not supported for sysctl collection.", |
| 50 | + priority=EventPriority.WARNING, |
| 51 | + console_log=True, |
| 52 | + ) |
| 53 | + return self.result, None |
| 54 | + |
| 55 | + for field_name in SysctlDataModel.model_fields: |
| 56 | + sysctl_key = field_name.replace("_", ".", 1) |
| 57 | + res = self._run_sut_cmd(f"sysctl -n {sysctl_key}") |
| 58 | + |
| 59 | + if res.exit_code == 0: |
| 60 | + try: |
| 61 | + values[field_name] = int(res.stdout.strip()) |
| 62 | + except ValueError: |
| 63 | + self._log_event( |
| 64 | + category=EventCategory.OS, |
| 65 | + description=f"Invalid integer value for {sysctl_key}", |
| 66 | + data={"stdout": res.stdout}, |
| 67 | + priority=EventPriority.ERROR, |
| 68 | + console_log=True, |
| 69 | + ) |
| 70 | + else: |
| 71 | + self._log_event( |
| 72 | + category=EventCategory.OS, |
| 73 | + description=f"Error checking Linux system setting : {sysctl_key}", |
| 74 | + data={"system_setting": sysctl_key, "exit_code": res.exit_code}, |
| 75 | + priority=EventPriority.WARNING, |
| 76 | + console_log=True, |
| 77 | + ) |
| 78 | + |
| 79 | + if values: |
| 80 | + sysctl_data = SysctlDataModel(**values) |
| 81 | + self._log_event( |
| 82 | + category="OS", |
| 83 | + description="Sysctl settings read", |
| 84 | + data=sysctl_data.model_dump(), |
| 85 | + priority=EventPriority.INFO, |
| 86 | + ) |
| 87 | + self.result.message = "SYSCTL data collected" |
| 88 | + self.result.status = ExecutionStatus.OK |
| 89 | + else: |
| 90 | + sysctl_data = None |
| 91 | + self._log_event( |
| 92 | + category=EventCategory.OS, |
| 93 | + description="Sysctl settings not read", |
| 94 | + priority=EventPriority.CRITICAL, |
| 95 | + ) |
| 96 | + self.result.message = "Sysctl settings not read" |
| 97 | + self.result.status = ExecutionStatus.ERROR |
| 98 | + |
| 99 | + return self.result, sysctl_data |
0 commit comments