feat(host-events): host events, event channels as log sources, how a service's run ended (NetOrk/netork#2897)
- HostEventsMixin.get_host_events(since=None), declared: the events of a fixed set of kinds a host's own log recorded (unexpected shutdowns, system crashes, hardware errors, disk errors, programs and services that crashed), with HostEventDict, HostEventsDict and host_event(). - LogSourceDict: kind "eventlog" for a Windows event channel, named by the new optional channel field (eventlog_source, LOG_SOURCE_KINDS). - ServiceDict: optional result, exit_code, trigger_start and account; exported.
This commit is contained in:
1 parent
2df46741f2
commit
6c273ea346
10 files changed
+458
-7
No files matched your search
@@ -0,0 +1,50 @@
|
||||
"""A Windows event channel as a log source (NetOrk/netork#2897).
|
||||
|
||||
A Windows host keeps its logs in event channels (``System``, ``Security``,
|
||||
``Microsoft-Windows-Sysmon/Operational``), not in a journal or in files a process
|
||||
holds open. A log reader takes a channel by its name -- CrowdSec's
|
||||
``event_channel``, Wazuh's ``eventchannel`` -- so a channel is a source of its own
|
||||
kind, named by its channel; ``path`` is the ``.evtx`` file behind it.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import NotRequired, get_origin, get_type_hints
|
||||
|
||||
from napalm_device_types import EVENTLOG, LOG_SOURCE_KINDS, LogSourceDict, eventlog_source
|
||||
|
||||
|
||||
def test_the_kinds_a_source_can_be():
|
||||
assert LOG_SOURCE_KINDS == ("journald", "file", "container", "eventlog")
|
||||
assert EVENTLOG == "eventlog"
|
||||
|
||||
|
||||
def test_a_channel_is_named_by_its_channel():
|
||||
source = eventlog_source(
|
||||
"Microsoft-Windows-Sysmon/Operational",
|
||||
r"C:\Windows\System32\Winevt\Logs\Microsoft-Windows-Sysmon%4Operational.evtx",
|
||||
1052672,
|
||||
1764583200,
|
||||
)
|
||||
assert source == {
|
||||
"kind": "eventlog",
|
||||
"path": r"C:\Windows\System32\Winevt\Logs\Microsoft-Windows-Sysmon%4Operational.evtx",
|
||||
"unit": None,
|
||||
"container_id": None,
|
||||
"process": None,
|
||||
"size": 1052672,
|
||||
"modified": "2025-12-01T10:00:00+00:00",
|
||||
"channel": "Microsoft-Windows-Sysmon/Operational",
|
||||
}
|
||||
|
||||
|
||||
def test_a_channel_without_a_file_or_a_last_write_says_none():
|
||||
source = eventlog_source("Application", None, None, None)
|
||||
assert (source["path"], source["size"], source["modified"]) == (None, None, None)
|
||||
|
||||
|
||||
def test_the_channel_is_optional_for_the_other_kinds():
|
||||
"""Additive: a journal, a file and a container's stream carry no channel."""
|
||||
hints = get_type_hints(LogSourceDict, include_extras=True)
|
||||
assert get_origin(hints["channel"]) is NotRequired
|
||||
assert get_origin(hints["kind"]) is None
|
||||
@@ -0,0 +1,90 @@
|
||||
"""get_host_events: what a host's own log recorded that says something is wrong with it.
|
||||
|
||||
A crash, a power loss, a hardware error or a program that keeps dying is written
|
||||
down by the host itself, in its event log or its journal; nothing on the outside
|
||||
sees it. The reading is the same shape for every operating system: each event as
|
||||
its kind, when, what it is about and where the host recorded it. Whether that is
|
||||
a problem is the application's to decide (NetOrk/netork#2897).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import get_type_hints
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_device_types import (
|
||||
HOST_EVENT_KINDS,
|
||||
HOST_EVENT_MESSAGE_CHARS,
|
||||
HOST_EVENT_WINDOW_DAYS,
|
||||
HostEventDict,
|
||||
HostEventsDict,
|
||||
HostEventsMixin,
|
||||
host_event,
|
||||
)
|
||||
|
||||
|
||||
class TestTheKinds:
|
||||
def test_every_kind_the_contract_names(self):
|
||||
assert HOST_EVENT_KINDS == (
|
||||
"unexpected_shutdown",
|
||||
"system_crash",
|
||||
"hardware_error_fatal",
|
||||
"hardware_error_corrected",
|
||||
"disk_error",
|
||||
"app_crash",
|
||||
"service_crash",
|
||||
)
|
||||
|
||||
def test_the_window_is_a_week(self):
|
||||
assert HOST_EVENT_WINDOW_DAYS == 7
|
||||
|
||||
|
||||
class TestHostEvent:
|
||||
def test_the_time_is_iso_in_utc(self):
|
||||
event = host_event("system_crash", 1764583200, "System: BugCheck 1001", "0x0000009f")
|
||||
assert event == {
|
||||
"kind": "system_crash",
|
||||
"time": "2025-12-01T10:00:00+00:00",
|
||||
"subject": "0x0000009f",
|
||||
"source": "System: BugCheck 1001",
|
||||
"message": None,
|
||||
}
|
||||
|
||||
def test_the_message_is_cut_to_its_limit(self):
|
||||
event = host_event("disk_error", 0, "System: disk 7", None, "x" * 1000)
|
||||
assert event["message"] == "x" * HOST_EVENT_MESSAGE_CHARS
|
||||
|
||||
def test_the_message_keeps_its_words_without_the_surrounding_space(self):
|
||||
event = host_event("disk_error", 0, "System: disk 7", None, " bad block\r\n ")
|
||||
assert event["message"] == "bad block"
|
||||
|
||||
@pytest.mark.parametrize("empty", ["", " ", None])
|
||||
def test_an_empty_subject_or_message_is_none(self, empty):
|
||||
event = host_event("app_crash", 0, "Application: Application Error 1000", empty, empty)
|
||||
assert event["subject"] is None
|
||||
assert event["message"] is None
|
||||
|
||||
def test_an_unknown_kind_is_refused(self):
|
||||
with pytest.raises(ValueError, match="bluescreen"):
|
||||
host_event("bluescreen", 0, "System", None)
|
||||
|
||||
|
||||
class TestTheContract:
|
||||
def test_the_getter_is_declared_only(self):
|
||||
"""A contract: nothing at runtime until a driver implements it, so
|
||||
``hasattr(driver, "get_host_events")`` stays truthful."""
|
||||
assert not hasattr(HostEventsMixin(), "get_host_events")
|
||||
|
||||
def test_the_reading_says_what_it_could_not_read(self):
|
||||
hints = get_type_hints(HostEventsDict)
|
||||
assert set(hints) == {"since", "events", "capped", "unread"}
|
||||
|
||||
def test_an_event_names_its_kind_time_subject_source_and_message(self):
|
||||
assert set(get_type_hints(HostEventDict)) == {
|
||||
"kind",
|
||||
"time",
|
||||
"subject",
|
||||
"source",
|
||||
"message",
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
"""How a service's last run ended, beside whether it runs (NetOrk/netork#2897, #2703).
|
||||
|
||||
``running=False`` alone does not tell a service that died from one stopped on
|
||||
purpose. The service manager knows: systemd's ``Result`` (``exit-code``,
|
||||
``signal``, ``core-dump``, ``timeout`` …), Windows' exit code of the service. So
|
||||
:class:`ServiceDict` carries the result in systemd's words, the exit code where
|
||||
there is one, whether the service only runs when a trigger starts it, and the
|
||||
account it runs as. All optional: a driver that cannot tell leaves them out.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from napalm_device_types import SERVICE_RESULT_EXIT_CODE, SERVICE_RESULT_SUCCESS, ServiceDict
|
||||
|
||||
|
||||
def test_the_result_is_said_in_systemds_words():
|
||||
assert SERVICE_RESULT_SUCCESS == "success"
|
||||
assert SERVICE_RESULT_EXIT_CODE == "exit-code"
|
||||
|
||||
|
||||
def test_the_new_fields_are_optional():
|
||||
assert ServiceDict.__required_keys__ == {"name", "running", "enabled", "pid"}
|
||||
assert ServiceDict.__optional_keys__ == {"result", "exit_code", "trigger_start", "account"}
|
||||
Reference in new issue
Block a user