Skip to content

Syslog plugin #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions nodescraper/plugins/inband/syslog/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
###############################################################################
#
# MIT License
#
# Copyright (c) 2025 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
from .syslog_plugin import SyslogPlugin

__all__ = ["SyslogPlugin"]
159 changes: 159 additions & 0 deletions nodescraper/plugins/inband/syslog/syslog_collector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
###############################################################################
#
# MIT License
#
# Copyright (c) 2025 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
import re

from nodescraper.base import InBandDataCollector
from nodescraper.connection.inband.inband import TextFileArtifact
from nodescraper.enums import EventCategory, EventPriority, OSFamily
from nodescraper.models import TaskResult

from .syslogdata import SyslogData


class SyslogCollector(InBandDataCollector[SyslogData, None]):
"""Read syslog log"""

SUPPORTED_OS_FAMILY = {OSFamily.LINUX}

DATA_MODEL = SyslogData

SYSLOG_CMD = r"ls -1 /var/log/syslog* 2>/dev/null | grep -E '^/var/log/syslog(\.[0-9]+(\.gz)?)?$' || true"

def _shell_quote(self, s: str) -> str:
"""single-quote fix."""
return "'" + s.replace("'", "'\"'\"'") + "'"

def _nice_syslog_name(self, path: str) -> str:
"""Map path to filename
Args:
path (str): file path
Returns:
str: new local filename
"""
prefix = "rotated_"
base = path.rstrip("/").rsplit("/", 1)[-1]

if base == "syslog":
return f"{prefix}syslog.log"

m = re.fullmatch(r"syslog\.(\d+)\.gz", base)
if m:
return f"{prefix}syslog.{m.group(1)}.gz.log"

m = re.fullmatch(r"syslog\.(\d+)", base)
if m:
return f"{prefix}syslog.{m.group(1)}.log"

middle = base[:-3] if base.endswith(".gz") else base
return f"{prefix}{middle}.log"

def _collect_syslog_rotations(self) -> int:
ret = 0
list_res = self._run_sut_cmd(self.SYSLOG_CMD, sudo=True)
paths = [p.strip() for p in (list_res.stdout or "").splitlines() if p.strip()]
if not paths:
self._log_event(
category=EventCategory.OS,
description="No /var/log/syslog files found (including rotations).",
data={"list_exit_code": list_res.exit_code},
priority=EventPriority.WARNING,
)
return 0

collected_logs, failed_logs = [], []
for p in paths:
qp = self._shell_quote(p)
if p.endswith(".gz"):
cmd = f"gzip -dc {qp} 2>/dev/null || zcat {qp} 2>/dev/null"
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False)
if res.exit_code == 0 and res.stdout is not None:
fname = self._nice_syslog_name(p)
self.logger.info("Collected syslog log: %s", fname)
self.result.artifacts.append(
TextFileArtifact(filename=fname, contents=res.stdout)
)
collected_logs.append(
{"path": p, "as": fname, "bytes": len(res.stdout.encode("utf-8", "ignore"))}
)
else:
failed_logs.append(
{"path": p, "exit_code": res.exit_code, "stderr": res.stderr, "cmd": cmd}
)
else:
cmd = f"cat {qp}"
res = self._run_sut_cmd(cmd, sudo=True, log_artifact=False)
if res.exit_code == 0 and res.stdout is not None:
fname = self._nice_syslog_name(p)
self.logger.info("Collected syslog log: %s", fname)
self.result.artifacts.append(
TextFileArtifact(filename=fname, contents=res.stdout)
)
collected_logs.append(
{"path": p, "as": fname, "bytes": len(res.stdout.encode("utf-8", "ignore"))}
)
else:
failed_logs.append(
{"path": p, "exit_code": res.exit_code, "stderr": res.stderr, "cmd": cmd}
)

if collected_logs:
self._log_event(
category=EventCategory.OS,
description="Collected syslog rotated files",
data={"collected": collected_logs},
priority=EventPriority.INFO,
)
self.result.message = self.result.message or "syslog rotated files collected"

if failed_logs:
self._log_event(
category=EventCategory.OS,
description="Some syslog files could not be collected.",
data={"failed": failed_logs},
priority=EventPriority.WARNING,
)

if collected_logs:
ret = len(collected_logs)
return ret

def collect_data(
self,
args=None,
) -> tuple[TaskResult, SyslogData | None]:
"""Collect syslog data from the system

Returns:
tuple[TaskResult | None]: tuple containing the result of the task and the syslog data if available
"""
syslog_logs = self._collect_syslog_rotations()

if syslog_logs:
syslog_data = SyslogData(syslog_logs=syslog_logs)
self.result.message = "Syslog data collected"
return self.result, syslog_data

return self.result, None
37 changes: 37 additions & 0 deletions nodescraper/plugins/inband/syslog/syslog_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
###############################################################################
#
# MIT License
#
# Copyright (c) 2025 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
from nodescraper.base import InBandDataPlugin

from .syslog_collector import SyslogCollector
from .syslogdata import SyslogData


class SyslogPlugin(InBandDataPlugin[SyslogData, None, None]):
"""Plugin for collection of syslog data"""

DATA_MODEL = SyslogData

COLLECTOR = SyslogCollector
32 changes: 32 additions & 0 deletions nodescraper/plugins/inband/syslog/syslogdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
##############################################################
#
# MIT License
#
# Copyright (c) 2025 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
from nodescraper.models import DataModel


class SyslogData(DataModel):
"""Data model for in band syslog logs"""

syslog_logs: int = 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@landrews-amd should i be keeping track of the files collected rather than the number of logs collected? (similar to the dmesg PR?)

Loading