-
Notifications
You must be signed in to change notification settings - Fork 1
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
alexandraBara
wants to merge
4
commits into
development
Choose a base branch
from
alex_syslog
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Syslog plugin #36
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?)