|
| 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.connection.inband.inband import TextFileArtifact |
| 29 | +from nodescraper.enums import EventCategory, EventPriority, OSFamily |
| 30 | +from nodescraper.models import TaskResult |
| 31 | +from nodescraper.utils import nice_rotated_name, shell_quote |
| 32 | + |
| 33 | +from .syslogdata import SyslogData |
| 34 | + |
| 35 | + |
| 36 | +class SyslogCollector(InBandDataCollector[SyslogData, None]): |
| 37 | + """Read syslog log""" |
| 38 | + |
| 39 | + SUPPORTED_OS_FAMILY = {OSFamily.LINUX} |
| 40 | + |
| 41 | + DATA_MODEL = SyslogData |
| 42 | + |
| 43 | + SYSLOG_CMD = r"ls -1 /var/log/syslog* 2>/dev/null | grep -E '^/var/log/syslog(\.[0-9]+(\.gz)?)?$' || true" |
| 44 | + |
| 45 | + def _collect_syslog_rotations(self) -> list[TextFileArtifact]: |
| 46 | + ret = [] |
| 47 | + list_res = self._run_sut_cmd(self.SYSLOG_CMD, sudo=True) |
| 48 | + paths = [p.strip() for p in (list_res.stdout or "").splitlines() if p.strip()] |
| 49 | + if not paths: |
| 50 | + self._log_event( |
| 51 | + category=EventCategory.OS, |
| 52 | + description="No /var/log/syslog files found (including rotations).", |
| 53 | + data={"list_exit_code": list_res.exit_code}, |
| 54 | + priority=EventPriority.WARNING, |
| 55 | + ) |
| 56 | + return [] |
| 57 | + |
| 58 | + collected_logs, failed_logs = [], [] |
| 59 | + collected = [] |
| 60 | + for p in paths: |
| 61 | + qp = shell_quote(p) |
| 62 | + if p.endswith(".gz"): |
| 63 | + cmd = f"gzip -dc {qp} 2>/dev/null || zcat {qp} 2>/dev/null" |
| 64 | + res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False) |
| 65 | + if res.exit_code == 0 and res.stdout is not None: |
| 66 | + fname = nice_rotated_name(p, "syslog") |
| 67 | + self.logger.info("Collected syslog log: %s", fname) |
| 68 | + collected.append(TextFileArtifact(filename=fname, contents=res.stdout)) |
| 69 | + collected_logs.append(fname) |
| 70 | + else: |
| 71 | + failed_logs.append(p) |
| 72 | + else: |
| 73 | + cmd = f"cat {qp}" |
| 74 | + res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False) |
| 75 | + if res.exit_code == 0 and res.stdout is not None: |
| 76 | + fname = nice_rotated_name(p, "syslog") |
| 77 | + self.logger.info("Collected syslog log: %s", fname) |
| 78 | + collected_logs.append(fname) |
| 79 | + collected.append(TextFileArtifact(filename=fname, contents=res.stdout)) |
| 80 | + else: |
| 81 | + failed_logs.append(p) |
| 82 | + |
| 83 | + if collected_logs: |
| 84 | + self._log_event( |
| 85 | + category=EventCategory.OS, |
| 86 | + description="Collected syslog rotated files", |
| 87 | + data={"collected": collected_logs}, |
| 88 | + priority=EventPriority.INFO, |
| 89 | + ) |
| 90 | + self.result.message = self.result.message or "syslog rotated files collected" |
| 91 | + |
| 92 | + if failed_logs: |
| 93 | + self._log_event( |
| 94 | + category=EventCategory.OS, |
| 95 | + description="Some syslog files could not be collected.", |
| 96 | + data={"failed": failed_logs}, |
| 97 | + priority=EventPriority.WARNING, |
| 98 | + ) |
| 99 | + |
| 100 | + if collected: |
| 101 | + ret = collected |
| 102 | + return ret |
| 103 | + |
| 104 | + def collect_data( |
| 105 | + self, |
| 106 | + args=None, |
| 107 | + ) -> tuple[TaskResult, SyslogData | None]: |
| 108 | + """Collect syslog data from the system |
| 109 | +
|
| 110 | + Returns: |
| 111 | + tuple[TaskResult | None]: tuple containing the result of the task and the syslog data if available |
| 112 | + """ |
| 113 | + syslog_logs = self._collect_syslog_rotations() |
| 114 | + |
| 115 | + if syslog_logs: |
| 116 | + syslog_data = SyslogData(syslog_logs=syslog_logs) |
| 117 | + self.result.message = "Syslog data collected" |
| 118 | + return self.result, syslog_data |
| 119 | + |
| 120 | + return self.result, None |
0 commit comments