"""Findings, incidents and ticket actions. A check turns source data into **findings**. A finding is either a **cause** (something that can own a ticket: a host out of memory) or a **symptom** (something that is usually the result of a cause: scrapes timing out, a new Ceph alert). The engine groups findings into **incidents**, one per root cause, keyed by the incident key that becomes the Tinker ``dedup_key`` (``scope:subject:kind``, thor#1 contract). """ from dataclasses import dataclass, field from typing import Any, Dict, List, Optional # Cause precedence (thor#1, Area 4): when several causes are active, symptoms attach to the # highest. Maintenance, power, network and partitions arrive with later checks. LAYER_MAINTENANCE = 90 LAYER_POWER = 80 LAYER_NETWORK = 70 LAYER_PARTITION = 60 LAYER_HOST_DOWN = 50 LAYER_RESOURCE = 40 LAYER_CEPH = 30 LAYER_SCRAPE = 20 CAUSE = 'cause' SYMPTOM = 'symptom' NOTE = 'note' # shown in Thor, never a ticket; only joins timeline-only incidents (maintenance) # Incident states PENDING = 'pending' # seen, held one cycle so same-moment symptoms land in the first text OPEN = 'open' CLEARED = 'cleared' @dataclass class Finding: kind: str subject: str # canonical host name, or 'cluster' role: str # CAUSE, SYMPTOM or NOTE layer: int start: float end: float key: str # incident key for a cause; a stable symptom key otherwise summary: str priority: int = 3 check: str = '' facts: Dict[str, Any] = field(default_factory=dict) events: List[Dict[str, Any]] = field(default_factory=list) # dated items, deduplicated by 'id' cluster_effect: bool = False # can be the result of trouble on another host # False for causes that explain things but never become tickets (planned maintenance). ticket: bool = True def to_dict(self) -> Dict[str, Any]: return dict(self.__dict__) @dataclass class Incident: key: str kind: str subject: str layer: int check: str state: str = PENDING priority: int = 3 first_seen: float = 0.0 last_seen: float = 0.0 pending_since: float = 0.0 opened_at: Optional[float] = None cleared_at: Optional[float] = None summary: str = '' facts: Dict[str, Any] = field(default_factory=dict) events: List[Dict[str, Any]] = field(default_factory=list) symptoms: Dict[str, Dict[str, Any]] = field(default_factory=dict) ticket_id: Optional[str] = None mode: str = 'shadow' seen_after_the_fact: bool = False ticket: bool = True # A known condition covers this incident: notes go to that ticket instead of a new one. known_ticket: Optional[str] = None known_reason: str = '' @property def active(self) -> bool: return self.state in (PENDING, OPEN) def to_dict(self) -> Dict[str, Any]: return dict(self.__dict__) @classmethod def from_dict(cls, d: Dict[str, Any]) -> 'Incident': known = {k: v for k, v in d.items() if k in cls.__dataclass_fields__} return cls(**known) @dataclass class Action: """Something to tell Tinker. In shadow mode it is recorded and never sent.""" incident_key: str kind: str # 'open', 'escalate', 'refresh', 'also_seen', 'cleared' created: float mode: str # 'shadow' or 'live' payload: Dict[str, Any] = field(default_factory=dict)