feat: which logs a host writes, and who writes each (4.30.0)
driver / standard (pull_request) Failing after 19s
driver / lint (pull_request) Successful in 20s
driver / types (pull_request) Successful in 1m14s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / test (3.12) (pull_request) Successful in 1m18s
driver / lowest (pull_request) Successful in 38s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m14s
CI / driver (pull_request) Failing after 2m45s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Failing after 19s
driver / lint (pull_request) Successful in 20s
driver / types (pull_request) Successful in 1m14s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / test (3.12) (pull_request) Successful in 1m18s
driver / lowest (pull_request) Successful in 38s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m14s
CI / driver (pull_request) Failing after 2m45s
driver / publish (pull_request) Skipped
LogSourcesMixin.get_log_sources() reads, in one sh -c call as root, the units journald holds entries for, the log files running processes hold open (/proc/<pid>/fd: *.log, *.log.N, anything under /var/log) with size and last write from stat -L on the descriptor, and the engine's own file of each container's output as that container's stream. Each file names the service or container of its first writer from /proc/<pid>/cgroup; a file a container writes keeps the container's own path. journald's store, deleted files and a database's write-ahead log (LevelDB's 000004.log) are left out. Without root it reads again and says so (attributed: false); a host whose find cannot list open files (busybox) raises LogSourcesUnavailable rather than reading as no files. Refs NetOrk/netork#2049
This commit is contained in:
1 parent
dacd10fcce
commit
5daba5d37c
7 files changed
+545
-1
No files matched your search
@@ -0,0 +1,243 @@
|
||||
"""get_log_sources: which logs a host writes, and who writes each of them.
|
||||
|
||||
Before anything can watch a host's logs -- a person in a log window, CrowdSec,
|
||||
a syslog forwarder -- something has to say which logs there are. Reading that
|
||||
is the same on every Linux host: journald names the units it holds entries for,
|
||||
``/proc/<pid>/fd`` shows the log files running processes have open, and
|
||||
``/proc/<pid>/cgroup`` says which systemd service or container a writer runs in.
|
||||
So all of it lives here once, and a driver only carries the command across
|
||||
(NetOrk/netork#2049).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_device_types.log_sources import (
|
||||
LOG_SOURCES_COMMAND,
|
||||
LogSourcesMixin,
|
||||
LogSourcesUnavailable,
|
||||
parse_log_sources,
|
||||
)
|
||||
|
||||
CID = "4f1c" + "0" * 60
|
||||
OTHER = "9a2b" + "1" * 60
|
||||
JOURNALD = """nginx.service
|
||||
ssh.service
|
||||
postgresql@16-main.service
|
||||
session-3.scope
|
||||
init.scope
|
||||
"""
|
||||
OPEN = f"""/proc/610/fd/7 /var/log/syslog
|
||||
/proc/610/fd/8 /var/log/auth.log
|
||||
/proc/620/fd/9 /var/log/journal/0123abcd/system.journal
|
||||
/proc/900/fd/5 /var/log/nginx/access.log
|
||||
/proc/900/fd/6 /var/log/nginx/error.log
|
||||
/proc/901/fd/5 /var/log/nginx/access.log
|
||||
/proc/700/fd/4 /var/log/old.log (deleted)
|
||||
/proc/1100/fd/30 /var/lib/docker/containers/{CID}/{CID}-json.log
|
||||
/proc/1100/fd/31 /var/lib/docker/containers/{OTHER}/local-logs/container.log
|
||||
/proc/1400/fd/3 /usr/src/paperless/data/log/paperless.log
|
||||
/proc/1500/fd/3 /srv/app/log with space.log
|
||||
/proc/1500/fd/4 /srv/app/Local Storage/leveldb/000004.log
|
||||
/proc/1500/fd/5 /var/lib/rocksdb/012345.log
|
||||
"""
|
||||
STAT = """/proc/610/fd/7 99000 1762250000
|
||||
/proc/610/fd/8 4100 1762250100
|
||||
/proc/900/fd/5 2048 1762250200
|
||||
/proc/900/fd/6 512 1762250300
|
||||
/proc/901/fd/5 2048 1762250200
|
||||
/proc/1100/fd/30 7000 1762250400
|
||||
/proc/1100/fd/31 300 1762250500
|
||||
/proc/1400/fd/3 0 1762250600
|
||||
"""
|
||||
CGROUPS = f"""610 0::/system.slice/rsyslog.service
|
||||
900 0::/system.slice/nginx.service
|
||||
901 0::/system.slice/nginx.service
|
||||
1100 0::/system.slice/docker.service
|
||||
1400 0::/system.slice/docker-{CID}.scope
|
||||
1500 0::/user.slice/user-1000.slice/session-3.scope
|
||||
"""
|
||||
COMM = """610 rsyslogd
|
||||
900 nginx
|
||||
901 nginx
|
||||
1100 dockerd
|
||||
1400 python3
|
||||
1500 app
|
||||
"""
|
||||
|
||||
|
||||
def _wire(
|
||||
*,
|
||||
journald: str | None = JOURNALD,
|
||||
open_: str = OPEN,
|
||||
stat: str = STAT,
|
||||
cgroups: str = CGROUPS,
|
||||
comm: str = COMM,
|
||||
noise: str = "",
|
||||
) -> str:
|
||||
"""The report as the command prints it, framed."""
|
||||
parts = [noise, "LOGS_BEGIN\n"]
|
||||
if journald is not None:
|
||||
parts += ["[journald]\n", journald]
|
||||
parts += ["[open]\n", open_, "[stat]\n", stat, "[cgroups]\n", cgroups, "[comm]\n", comm]
|
||||
parts.append("LOGS_END\n")
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def _sources(output: str | None = None) -> list:
|
||||
return parse_log_sources(_wire() if output is None else output)[1]
|
||||
|
||||
|
||||
def _by_key(sources: list) -> dict:
|
||||
return {(s["kind"], s["container_id"], s["path"] or s["unit"]): s for s in sources}
|
||||
|
||||
|
||||
class TestParse:
|
||||
def test_journald_names_each_service_unit_once_without_its_suffix(self) -> None:
|
||||
journald, sources = parse_log_sources(_wire())
|
||||
units = [s["unit"] for s in sources if s["kind"] == "journald"]
|
||||
assert units == ["nginx", "postgresql@16-main", "ssh"]
|
||||
assert journald is True
|
||||
|
||||
def test_a_host_without_journald_says_so(self) -> None:
|
||||
journald, sources = parse_log_sources(_wire(journald=None))
|
||||
assert journald is False
|
||||
assert not [s for s in sources if s["kind"] == "journald"]
|
||||
|
||||
def test_an_open_file_names_its_writer_size_and_last_write(self) -> None:
|
||||
sources = _by_key(_sources())
|
||||
access = sources[("file", None, "/var/log/nginx/access.log")]
|
||||
assert access == {
|
||||
"kind": "file",
|
||||
"path": "/var/log/nginx/access.log",
|
||||
"unit": "nginx",
|
||||
"container_id": None,
|
||||
"process": "nginx",
|
||||
"size": 2048,
|
||||
"modified": "2025-11-04T09:56:40+00:00",
|
||||
}
|
||||
|
||||
def test_a_file_two_processes_hold_is_one_source(self) -> None:
|
||||
paths = [s["path"] for s in _sources() if s["kind"] == "file"]
|
||||
assert paths.count("/var/log/nginx/access.log") == 1
|
||||
|
||||
def test_a_file_without_a_suffix_under_var_log_counts(self) -> None:
|
||||
sources = _by_key(_sources())
|
||||
assert sources[("file", None, "/var/log/syslog")]["unit"] == "rsyslog"
|
||||
|
||||
def test_a_file_a_container_writes_carries_the_container_and_its_own_path(self) -> None:
|
||||
sources = _by_key(_sources())
|
||||
paperless = sources[("file", CID, "/usr/src/paperless/data/log/paperless.log")]
|
||||
assert paperless["unit"] is None
|
||||
assert paperless["process"] == "python3"
|
||||
assert paperless["size"] == 0
|
||||
|
||||
def test_the_engines_own_log_file_is_the_containers_stream(self) -> None:
|
||||
sources = _by_key(_sources())
|
||||
json_file = sources[("container", CID, f"/var/lib/docker/containers/{CID}/{CID}-json.log")]
|
||||
assert json_file["process"] == "dockerd"
|
||||
assert json_file["size"] == 7000
|
||||
local = sources[
|
||||
("container", OTHER, f"/var/lib/docker/containers/{OTHER}/local-logs/container.log")
|
||||
]
|
||||
assert local["modified"] == "2025-11-04T10:01:40+00:00"
|
||||
assert not [s for s in _sources() if "docker" in (s["unit"] or "")]
|
||||
|
||||
def test_journal_files_and_deleted_files_are_not_sources(self) -> None:
|
||||
paths = [s["path"] or "" for s in _sources()]
|
||||
assert not [p for p in paths if "/var/log/journal/" in p or p.startswith("/var/log/old")]
|
||||
|
||||
def test_a_databases_write_ahead_log_is_not_a_source(self) -> None:
|
||||
# LevelDB and RocksDB name theirs 000004.log; it holds records, not lines.
|
||||
paths = [s["path"] or "" for s in _sources()]
|
||||
assert not [p for p in paths if p.endswith(("000004.log", "012345.log"))]
|
||||
|
||||
def test_a_path_with_a_space_survives(self) -> None:
|
||||
sources = _by_key(_sources())
|
||||
spaced = sources[("file", None, "/srv/app/log with space.log")]
|
||||
# The writer runs in a login session, not in a service; stat did not answer.
|
||||
assert spaced["unit"] is None
|
||||
assert spaced["process"] == "app"
|
||||
assert spaced["size"] is None
|
||||
assert spaced["modified"] is None
|
||||
|
||||
def test_sources_come_sorted_by_kind_then_container_then_name(self) -> None:
|
||||
sources = _sources()
|
||||
order = [(s["kind"], s["container_id"] or "", s["path"] or s["unit"]) for s in sources]
|
||||
assert order == sorted(order)
|
||||
|
||||
def test_noise_before_the_frame_is_ignored(self) -> None:
|
||||
assert _sources(_wire(noise="Last login: today\r\n\x1b[0m"))
|
||||
|
||||
def test_a_host_without_gnu_find_raises_rather_than_reading_as_no_files(self) -> None:
|
||||
# busybox find has neither -lname nor -printf: no files would read as none.
|
||||
with pytest.raises(LogSourcesUnavailable, match="find"):
|
||||
parse_log_sources("LOGS_BEGIN\n[journald]\nnginx.service\n[no-find]\nLOGS_END\n")
|
||||
|
||||
def test_a_report_cut_short_raises_rather_than_reading_as_fewer_logs(self) -> None:
|
||||
cut = _wire().replace("LOGS_END\n", "")
|
||||
with pytest.raises(ValueError, match="log source report"):
|
||||
parse_log_sources(cut)
|
||||
|
||||
|
||||
class _Host(LogSourcesMixin):
|
||||
"""Carries the command the way a driver does; *answers* per privilege."""
|
||||
|
||||
def __init__(self, answers: dict[bool, str]) -> None:
|
||||
self.answers = answers
|
||||
self.calls: list[tuple[str, bool]] = []
|
||||
|
||||
def _run_log_sources_command(self, command: str, *, privileged: bool) -> str:
|
||||
self.calls.append((command, privileged))
|
||||
return self.answers[privileged]
|
||||
|
||||
|
||||
class TestMixin:
|
||||
def test_it_reads_as_root_in_one_sh_call(self) -> None:
|
||||
host = _Host({True: _wire()})
|
||||
reading = host.get_log_sources()
|
||||
assert reading["attributed"] is True
|
||||
command, privileged = host.calls[0]
|
||||
assert privileged is True
|
||||
assert command.startswith("sh -c ")
|
||||
assert len(host.calls) == 1
|
||||
|
||||
def test_without_root_it_reads_again_and_says_so(self) -> None:
|
||||
host = _Host({True: "sudo: a password is required\n", False: _wire()})
|
||||
reading = host.get_log_sources()
|
||||
assert reading["attributed"] is False
|
||||
assert [privileged for _, privileged in host.calls] == [True, False]
|
||||
assert reading["sources"]
|
||||
|
||||
def test_a_host_without_gnu_find_is_not_read_again_unprivileged(self) -> None:
|
||||
host = _Host({True: "LOGS_BEGIN\n[no-find]\nLOGS_END\n"})
|
||||
with pytest.raises(LogSourcesUnavailable):
|
||||
host.get_log_sources()
|
||||
assert len(host.calls) == 1
|
||||
|
||||
def test_a_host_that_answers_neither_way_raises(self) -> None:
|
||||
host = _Host({True: "", False: "garbage"})
|
||||
with pytest.raises(ValueError):
|
||||
host.get_log_sources()
|
||||
|
||||
|
||||
class TestCommand:
|
||||
def test_it_is_valid_posix_sh(self) -> None:
|
||||
assert subprocess.run(["sh", "-n", "-c", LOG_SOURCES_COMMAND]).returncode == 0
|
||||
|
||||
@pytest.mark.skipif(shutil.which("find") is None, reason="needs find on this host")
|
||||
def test_it_runs_here_and_parses(self, tmp_path) -> None:
|
||||
log = tmp_path / "held.log"
|
||||
log.write_text("one line\n")
|
||||
with log.open() as held:
|
||||
out = subprocess.run(
|
||||
["sh", "-c", LOG_SOURCES_COMMAND], capture_output=True, text=True, timeout=60
|
||||
).stdout
|
||||
assert held.readable()
|
||||
# This test's own process holds the file, so it must be among the sources.
|
||||
mine = [s for s in _sources(out) if s["path"] == str(log)]
|
||||
assert mine and mine[0]["size"] == len("one line\n")
|
||||
Reference in new issue
Block a user