File size: 5,697 Bytes
f3b0a91 | 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 153 154 | """Alertmanager checks: amtool for config validity and route resolution, a strict simulator for inhibition.
The simulator implements Alertmanager v0.28 inhibition for a documented subset; anything outside the
subset (unknown keys, brace-list matcher strings, invalid regexes) makes the case fail (H8).
"""
from __future__ import annotations
import hashlib
import os
import re
import subprocess
from .promrun import tool
_ROUTE_CACHE: dict[tuple[str, tuple], list[str] | None] = {}
_CHECK_CACHE: dict[str, tuple[bool, str]] = {}
def _digest(text: str) -> str:
return hashlib.sha256(text.encode()).hexdigest()
def check_config(path: str, text: str) -> tuple[bool, str]:
key = _digest(text)
if key not in _CHECK_CACHE:
try:
r = subprocess.run([tool("amtool"), "check-config", path], capture_output=True, text=True, timeout=30)
_CHECK_CACHE[key] = (r.returncode == 0, (r.stdout + r.stderr)[-800:])
except subprocess.TimeoutExpired:
_CHECK_CACHE[key] = (False, "amtool check-config timed out")
return _CHECK_CACHE[key]
def _q(v: str) -> str:
return '"' + str(v).replace("\\", "\\\\").replace('"', '\\"') + '"'
def resolve(path: str, text: str, labels: dict[str, str]) -> list[str] | None:
"""Receivers `amtool config routes test` resolves for a label set (None on tool error)."""
key = (_digest(text), tuple(sorted(labels.items())))
if key not in _ROUTE_CACHE:
args = [f"{k}={_q(v)}" for k, v in sorted(labels.items())]
try:
r = subprocess.run([tool("amtool"), "config", "routes", "test", f"--config.file={path}", *args],
capture_output=True, text=True, timeout=20)
out = r.stdout.strip().splitlines()
_ROUTE_CACHE[key] = sorted(x for x in out[-1].split(",") if x) if r.returncode == 0 and out else None
except subprocess.TimeoutExpired:
_ROUTE_CACHE[key] = None
return _ROUTE_CACHE[key]
def jaccard(a: set, b: set) -> float:
if not a and not b:
return 1.0
return len(a & b) / len(a | b)
# ---------------------------------------------------------------------------- inhibition simulator
class Unsupported(Exception):
pass
_MATCHER = re.compile(r'^\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=~|!~|!=|=)\s*(.*?)\s*$', re.S)
_RULE_KEYS = {"source_matchers", "target_matchers", "source_match", "source_match_re",
"target_match", "target_match_re", "equal"}
def _unquote(v: str) -> str:
if len(v) >= 2 and v[0] == '"' and v[-1] == '"':
out, i, body = [], 0, v[1:-1]
while i < len(body):
if body[i] == "\\" and i + 1 < len(body):
out.append(body[i + 1])
i += 2
else:
out.append(body[i])
i += 1
return "".join(out)
return v
def _compile(op: str, value: str):
if op in ("=~", "!~"):
try:
rx = re.compile(f"^(?:{value})$")
except re.error as e:
raise Unsupported(f"bad regex {value!r}") from e
return (lambda s: bool(rx.match(s))) if op == "=~" else (lambda s: not rx.match(s))
return (lambda s: s == value) if op == "=" else (lambda s: s != value)
def parse_matchers(rule: dict, side: str) -> list[tuple[str, object]]:
out = []
ms = rule.get(f"{side}_matchers")
if ms is not None:
if not isinstance(ms, list):
raise Unsupported(f"{side}_matchers must be a list")
for m in ms:
if not isinstance(m, str) or m.strip().startswith("{"):
raise Unsupported(f"unsupported matcher {m!r}")
mm = _MATCHER.match(m)
if not mm:
raise Unsupported(f"unparsable matcher {m!r}")
name, op, val = mm.group(1), mm.group(2), _unquote(mm.group(3))
out.append((name, _compile(op, val)))
for key, op in ((f"{side}_match", "="), (f"{side}_match_re", "=~")):
legacy = rule.get(key)
if legacy is None:
continue
if not isinstance(legacy, dict):
raise Unsupported(f"{key} must be a mapping")
for name, val in legacy.items():
out.append((str(name), _compile(op, str(val))))
return out
def _matches(ms: list, labels: dict) -> bool:
return all(fn(labels.get(name, "")) for name, fn in ms)
def inhibited(am: dict, source: dict | None, target: dict) -> bool | None:
"""True/False, or None when the config uses constructs outside the supported subset."""
rules = am.get("inhibit_rules") or []
if not isinstance(rules, list):
return None
if source is None:
return False if all(isinstance(r, dict) for r in rules) else None
try:
for r in rules:
if not isinstance(r, dict) or set(r) - _RULE_KEYS:
raise Unsupported(f"unsupported inhibit rule keys: {sorted(set(r) - _RULE_KEYS) if isinstance(r, dict) else r}")
src, tgt = parse_matchers(r, "source"), parse_matchers(r, "target")
equal = r.get("equal") or []
if not isinstance(equal, list):
raise Unsupported("equal must be a list")
if source == target:
continue # an alert cannot inhibit itself
if _matches(tgt, target) and _matches(src, source) and \
all(source.get(str(e), "") == target.get(str(e), "") for e in equal):
return True
except Unsupported:
return None
return False
def route_config_path(workdir: str, text: str) -> str:
p = os.path.join(workdir, "alertmanager.yml")
with open(p, "w") as fh:
fh.write(text)
return p
|