diff --git a/server/kernelwatch/status.py b/server/kernelwatch/status.py
index e54b29e..cd9e761 100644
--- a/server/kernelwatch/status.py
+++ b/server/kernelwatch/status.py
@@ -106,8 +106,19 @@ def agent_outdated(version: str | None) -> bool:
return bool(version) and _version_tuple(version) < _version_tuple(AGENT_VERSION)
+def hostname_mismatch(registered: str, report: dict[str, Any]) -> str | None:
+ """The hostname the agent reports, if it can't be the machine `registered` names.
+
+ Registered names are usually `hostname -s` while the agent reports `hostname -f`,
+ so only the first DNS label is compared, ignoring case."""
+ reported = report.get("reported_hostname")
+ if not reported or reported.split(".")[0].lower() == registered.split(".")[0].lower():
+ return None
+ return reported
+
+
def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
- cfg: Config) -> Status:
+ cfg: Config, host: str | None = None) -> Status:
status = Status()
if received_at is None or report is None:
status.raise_to("stale", "never reported")
@@ -142,6 +153,10 @@ def evaluate(received_at: int | None, report: dict[str, Any] | None, now: float,
elif apt_age / 3600 > cfg.apt_stale_after_hours:
status.raise_to("update", f"package lists {apt_age / 86400:.0f}d old; apt update not running?")
+ if host and (reported := hostname_mismatch(host, report)):
+ status.note(f"registered as {host} but reports itself as {reported}; "
+ "token on the wrong machine?")
+
if agent_outdated(report.get("agent_version")):
status.note(f"agent {report['agent_version']} is older than {AGENT_VERSION}; "
"run agent/deploy.sh")
diff --git a/server/kernelwatch/web.py b/server/kernelwatch/web.py
index 9a48b52..6f193c0 100644
--- a/server/kernelwatch/web.py
+++ b/server/kernelwatch/web.py
@@ -408,7 +408,7 @@ class Handler(BaseHTTPRequestHandler):
rows = []
for h in hosts:
- h["status"] = evaluate(h["received_at"], h["report"], now, self.cfg)
+ h["status"] = evaluate(h["received_at"], h["report"], now, self.cfg, h["host"])
hosts.sort(key=lambda h: (h["status"].rank, h["host"]))
counts = {level: 0 for level in LEVELS}
@@ -470,7 +470,7 @@ class Handler(BaseHTTPRequestHandler):
return self.send(HTTPStatus.NOT_FOUND, page("Not found", "Not found.
", user))
latest = history[0] if history else None
- status = evaluate(latest and latest["received_at"], latest and latest["report"], now, self.cfg)
+ status = evaluate(latest and latest["received_at"], latest and latest["report"], now, self.cfg, name)
r = latest["report"] if latest else {}
def field(label: str, value: Any, code: bool = False) -> str:
diff --git a/tests/test_status.py b/tests/test_status.py
index 02f903d..89ef182 100644
--- a/tests/test_status.py
+++ b/tests/test_status.py
@@ -34,6 +34,26 @@ class EvaluateTest(unittest.TestCase):
self.assertEqual(s.level, "ok", s.reasons)
self.assertEqual(s.reasons, [])
+ def test_hostname_mismatch_is_a_note_not_a_level(self):
+ rep = report(reported_hostname="db01.example.internal")
+ s = evaluate(NOW - 60, rep, NOW, CFG, "web01")
+ self.assertEqual(s.level, "ok")
+ self.assertEqual(len(s.reasons), 1)
+ self.assertIn("registered as web01 but reports itself as db01.example.internal", s.reasons[0])
+ # Same worst level as without the check.
+ rep = report(reported_hostname="db01", reboot_required=True)
+ self.assertEqual(evaluate(NOW - 60, rep, NOW, CFG, "web01").level, "reboot")
+
+ def test_hostname_match_is_lenient_and_optional(self):
+ for registered, reported in (("web01", "web01.example.internal"), ("WEB01", "web01"),
+ ("web01.example.internal", "web01"), ("web01", "web01"),
+ ("web01", None)):
+ with self.subTest(registered=registered, reported=reported):
+ rep = report() if reported is None else report(reported_hostname=reported)
+ self.assertEqual(evaluate(NOW - 60, rep, NOW, CFG, registered).reasons, [])
+ # Without a registered name (alerts, history rows) nothing is compared.
+ self.assertEqual(evaluate(NOW - 60, report(reported_hostname="db01"), NOW, CFG).reasons, [])
+
def test_never_reported(self):
self.assertEqual(evaluate(None, None, NOW, CFG).level, "stale")
diff --git a/tests/test_web.py b/tests/test_web.py
index 6bba422..71ef748 100644
--- a/tests/test_web.py
+++ b/tests/test_web.py
@@ -163,6 +163,17 @@ class DashboardTest(ServerCase):
status, _, _ = self.login("admin", "a-long-password", {"Origin": "https://evil.example"})
self.assertEqual(status, 403)
+ def test_hostname_mismatch_shown_on_dashboard_and_host_page(self):
+ self.add_user("admin", "a-long-password")
+ token = self.add_host("web01")
+ self.post_report(token, {"kernel_running": "6.8.0-85-generic", "apt_lists_age_seconds": 60,
+ "reported_hostname": "db01.example.internal "})
+ _, headers, _ = self.login("admin", "a-long-password")
+ cookie = {"Cookie": headers["Set-Cookie"].split(";")[0]}
+ for path in ("/", "/host/web01"):
+ body = self.request("GET", path, headers=cookie)[2]
+ self.assertIn("registered as web01 but reports itself as db01.example.internal <b>", body)
+
def test_login_dashboard_logout(self):
self.add_user("admin", "a-long-password")
token = self.add_host("web01")