Files
thor/thor/hosts.py
T
jaredandClaude Opus 5.5 983ba27386
CI / Lint (flake8, house style) (pull_request) Successful in 50s
CI / Test (pytest) (pull_request) Successful in 52s
CI / Security (bandit, pip-audit) (pull_request) Successful in 1m1s
CI / Integration (MariaDB) (pull_request) Successful in 1m40s
CI / Scan (secrets, workflows, shell, patterns) (pull_request) Successful in 1m15s
CI / Deploy (pull_request) Skipped
Engine, memory check, symptom checks, outbox, T1 replay
- thor/engine.py: one incident per root cause. New causes held one cycle, symptoms attached
  to the highest cause they relate to (same host, or cluster-wide effects for causes that
  can stall the cluster), priority only rises, rate-limited refresh, cleared after 15 min.
- thor/checks/memory.py: node_vmstat_oom_kill + kernel oom-kill lines; global vs cgroup,
  victim and CT, trigger cpuset; P3 / P2 (loop) / P1 (Ceph or other hosts stalling).
- thor/checks/symptoms.py: scrape timeouts, pvestatd timeouts, Ceph alerts switching on.
- thor/outbox.py: live actions to Tinker with backoff; shadow actions are only recorded.
- Migration 2: incidents, actions, observations.
- PromQL/LogQL regex escaping fixed (found by recording against the real Prometheus).
- tests/replay.py + tests/fixtures/t1.json: 2026-11-23 replayed through the real monitor:
  exactly one ticket (P1 from the start: pvestatd timed out on three other hosts with the
  first kill), 15 symptoms as Also seen, cleared 06:18, Loki's 06:00 outage delays only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:02:10 -05:00

88 lines
3.5 KiB
Python

"""Which hosts Thor watches, and how each source names them.
Sources disagree: Prometheus labels hosts by ``instance`` (IP:port) and a job name that can
be stale (compute-storage-02's is still ``largeoneexporter``), and Loki's ``host`` label
used the old names (``micro1``, ``large1``) before 2026-11-18. Thor maps everything to one
canonical name: by IP for Prometheus, through an alias table for Loki.
The node list comes from the Proxmox API when it answers (``/cluster/status``); the
defaults below cover the time it doesn't. PBS isn't a cluster node but is watched too.
"""
from dataclasses import dataclass, field
from typing import Dict, Iterable, List, Optional
@dataclass
class Host:
name: str
ip: str
cluster_node: bool = True
# Hosts running Ceph OSDs: trouble on them can stall storage for the whole cluster.
ceph_osds: bool = False
aliases: List[str] = field(default_factory=list)
DEFAULT_HOSTS = [
Host('compute-storage-01', '10.10.10.4', ceph_osds=True),
Host('compute-storage-02', '10.10.10.2', ceph_osds=True, aliases=['large1']),
Host('compute-storage-gpu-01', '10.10.10.10', ceph_osds=True),
Host('storage-01', '10.10.10.11', ceph_osds=True),
Host('monitor-01', '10.10.10.8', aliases=['micro1']),
Host('monitor-02', '10.10.10.9'),
Host('pbs', '10.10.10.3', cluster_node=False),
]
class HostRegistry:
def __init__(self, hosts: Iterable[Host] = DEFAULT_HOSTS):
self._hosts: Dict[str, Host] = {}
for h in hosts:
self._hosts[h.name] = Host(h.name, h.ip, h.cluster_node, h.ceph_osds, list(h.aliases))
def all(self) -> List[Host]:
return sorted(self._hosts.values(), key=lambda h: h.name)
def get(self, name: str) -> Optional[Host]:
return self._hosts.get(self.canonical(name) or '')
def canonical(self, name: str) -> Optional[str]:
"""The current name for a host name or old alias, or None if not watched."""
if name in self._hosts:
return name
for h in self._hosts.values():
if name in h.aliases:
return h.name
return None
def by_instance(self, instance: str) -> Optional[str]:
"""Host for a Prometheus ``instance`` like ``10.10.10.4:9100``."""
ip = instance.rsplit(':', 1)[0].strip('[]')
for h in self._hosts.values():
if h.ip == ip:
return h.name
return None
def loki_names(self, name: str) -> List[str]:
"""Every name a host's journal lines may carry in Loki (current first)."""
h = self.get(name)
return [h.name] + h.aliases if h else [name]
def update_from_proxmox(self, cluster_status: List[dict]) -> None:
"""Pick up node renames, IP changes and new nodes from ``/cluster/status``."""
for entry in cluster_status:
if entry.get('type') != 'node' or not entry.get('name') or not entry.get('ip'):
continue
name, ip = entry['name'], entry['ip']
existing = self._hosts.get(name)
if existing:
existing.ip = ip
continue
# Same IP under a new name: a rename. Keep the old name as an alias.
for old in list(self._hosts.values()):
if old.ip == ip and old.cluster_node:
del self._hosts[old.name]
self._hosts[name] = Host(name, ip, True, old.ceph_osds, old.aliases + [old.name])
break
else:
self._hosts[name] = Host(name, ip, True)