File size: 6,002 Bytes
44084af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""Delivered notifications for the expert tier: inhibition, route walk with group_by, notification sets.

- `inhibited(am, alerts)`: Alertmanager 0.28 inhibition over the alerts firing together. A target matching
  a rule's target side is muted by a firing source that matches the source side and agrees on `equal`,
  except that when the target also matches the source side, sources that match the target side are skipped
  (Alertmanager's two-sided exclusion: two alerts that match both sides never mute each other). Inhibited
  alerts still act as sources.
- `walk(am, labels)`: the matching leaf routes (first match wins unless `continue`), with the effective
  receiver and group_by. A child's unset or empty `group_by` inherits the parent's; `'...'` groups on every
  label.
- `notifications(am, path, text, snapshots)`: distinct (receiver, route, group key) over snapshot times,
  after inhibition. Receivers come from amtool; a route walk that disagrees with amtool fails closed.

Timers (group_wait, group_interval, repeat_interval) are out of scope: a notification is a group that has
at least one delivered alert at some snapshot.
"""

from __future__ import annotations

from .amcheck import Unsupported, resolve
from .matchers import compile_list, compile_value

ALL = "..."


def _parse(ms, legacy: dict | None, legacy_re: dict | None) -> list:
    """`matchers` strings (Alertmanager's own grammar, see matchers.py) plus the legacy `match`/`match_re`."""
    out = compile_list(ms)
    for d, op in ((legacy, "="), (legacy_re, "=~")):
        if d is None:
            continue
        if not isinstance(d, dict):
            raise Unsupported("match must be a mapping")
        for k, v in d.items():
            out.append((str(k), compile_value(op, str(v))))
    return out


def _ok(ms: list, labels: dict) -> bool:
    return all(fn(labels.get(n, "")) for n, fn in ms)


# ---------------------------------------------------------------------------- inhibition

def _rules(am: dict) -> list:
    out = []
    for r in am.get("inhibit_rules") or []:
        if not isinstance(r, dict):
            raise Unsupported("inhibit rule is not a mapping")
        src = _parse(r.get("source_matchers"), r.get("source_match"), r.get("source_match_re"))
        tgt = _parse(r.get("target_matchers"), r.get("target_match"), r.get("target_match_re"))
        eq = r.get("equal") or []
        if not isinstance(eq, list):
            raise Unsupported("equal must be a list")
        out.append((src, tgt, [str(e) for e in eq]))
    return out


def inhibited(am: dict, alerts: list[dict]) -> list[bool] | None:
    """For each alert (label dict), whether the others firing with it mute it. None if unsupported."""
    try:
        rules = _rules(am)
    except (Unsupported, AttributeError):
        return None
    out = []
    for t in alerts:
        muted = False
        for src, tgt, eq in rules:
            if not _ok(tgt, t):
                continue
            two_sided = _ok(src, t)
            for s in alerts:
                if s is t or not _ok(src, s):
                    continue
                if any(s.get(e, "") != t.get(e, "") for e in eq):
                    continue
                if two_sided and _ok(tgt, s):
                    continue
                muted = True
                break
            if muted:
                break
        out.append(muted)
    return out


def delivered(am: dict, alerts: list[dict]) -> list[dict] | None:
    mask = inhibited(am, alerts)
    if mask is None:
        return None
    return [a for a, m in zip(alerts, mask) if not m]


# ---------------------------------------------------------------------------- routing

def walk(am: dict, labels: dict) -> list[tuple[str, object, str]] | None:
    """[(receiver, group_by list or ALL, route path)] for the matching leaves; None if unsupported."""
    root = am.get("route")
    if not isinstance(root, dict):
        return None
    try:
        return _walk(root, labels, root.get("receiver"), root.get("group_by") or [], "0", True)
    except (Unsupported, AttributeError, TypeError):
        return None


def _walk(node: dict, labels: dict, recv, gb, path: str, is_root: bool) -> list | None:
    if not is_root:
        ms = _parse(node.get("matchers"), node.get("match"), node.get("match_re"))
        if not _ok(ms, labels):
            return None
    recv = node.get("receiver") or recv
    if node.get("group_by"):
        gb = node["group_by"]
    gb = ALL if ALL in (gb if isinstance(gb, list) else [gb]) else list(gb)
    found: list = []
    for i, child in enumerate(node.get("routes") or []):
        if not isinstance(child, dict):
            raise Unsupported("route is not a mapping")
        got = _walk(child, labels, recv, gb, f"{path}.{i}", False)
        if got is not None:
            found += got
            if not child.get("continue"):
                break
    if not found:
        found = [(str(recv), gb, path)]
    return found


def group_key(gb, labels: dict) -> tuple:
    if gb == ALL:
        return tuple(sorted(labels.items()))
    return tuple((k, labels.get(k, "")) for k in sorted(gb))


def notifications(am: dict, am_path: str, am_text: str, snapshots: list[list[dict]]) -> set | None:
    """Distinct (receiver, route path, group key, frozenset(services)) over snapshots; None if unsupported."""
    groups: dict[tuple, set] = {}
    for alerts in snapshots:
        live = delivered(am, alerts)
        if live is None:
            return None
        for a in live:
            leaves = walk(am, a)
            want = resolve(am_path, am_text, a)
            if leaves is None or want is None or sorted({r for r, _, _ in leaves}) != sorted(set(want)):
                return None
            for recv, gb, path in leaves:
                key = (recv, path, group_key(gb, a))
                groups.setdefault(key, set()).add(a.get("service", ""))
    return {(r, p, k, frozenset(s)) for (r, p, k), s in groups.items()}