feat(check): alertmanager monitors with Prometheus label matchers
Refs #91 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
235bd7db42
commit
98bdb3c282
3 files changed
+155
-5
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// AlertmanagerConfig configures a monitor fed by Alertmanager: it is down
|
||||
// while an alert matching every matcher fires.
|
||||
type AlertmanagerConfig struct {
|
||||
Matchers []Matcher `json:"matchers"`
|
||||
// StaleHours ends a firing alert that Alertmanager has not repeated for
|
||||
// this long, so a lost "resolved" message cannot keep a monitor down
|
||||
// forever. It must exceed Alertmanager's repeat_interval (4h by default).
|
||||
StaleHours int `json:"stale_hours"`
|
||||
}
|
||||
|
||||
// Matcher compares one label the way Prometheus and Alertmanager do:
|
||||
// = and != compare text, =~ and !~ match a fully anchored regular
|
||||
// expression. A missing label counts as empty.
|
||||
type Matcher struct {
|
||||
Label string `json:"label"`
|
||||
Op string `json:"op"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// DefaultStaleHours applies when no stale period is set.
|
||||
const DefaultStaleHours = 24
|
||||
|
||||
var (
|
||||
labelName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
|
||||
matcherOps = []string{"=", "!=", "=~", "!~"}
|
||||
)
|
||||
|
||||
type alertmanagerCheck struct{}
|
||||
|
||||
func (alertmanagerCheck) validate(_ string, config json.RawMessage) (json.RawMessage, error) {
|
||||
c, err := decode[AlertmanagerConfig](config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Forms send empty rows for unused matcher slots.
|
||||
c.Matchers = slices.DeleteFunc(c.Matchers, func(m Matcher) bool { return m.Label == "" })
|
||||
if len(c.Matchers) == 0 || len(c.Matchers) > 20 {
|
||||
return nil, invalid("invalid_matchers")
|
||||
}
|
||||
for _, m := range c.Matchers {
|
||||
if !labelName.MatchString(m.Label) || !slices.Contains(matcherOps, m.Op) || len(m.Value) > 500 {
|
||||
return nil, invalid("invalid_matchers")
|
||||
}
|
||||
if _, err := m.compile(); err != nil {
|
||||
return nil, invalid("invalid_matchers")
|
||||
}
|
||||
}
|
||||
if c.StaleHours == 0 {
|
||||
c.StaleHours = DefaultStaleHours
|
||||
}
|
||||
if c.StaleHours < 1 || c.StaleHours > 24*30 {
|
||||
return nil, invalid("invalid_stale_hours")
|
||||
}
|
||||
return encode(c), nil
|
||||
}
|
||||
|
||||
func (alertmanagerCheck) run(context.Context, Spec) Outcome {
|
||||
return Outcome{Error: "alertmanager monitors are not run by checkers"}
|
||||
}
|
||||
|
||||
func (m Matcher) compile() (*regexp.Regexp, error) {
|
||||
if m.Op != "=~" && m.Op != "!~" {
|
||||
return nil, nil
|
||||
}
|
||||
return regexp.Compile("^(?:" + m.Value + ")$")
|
||||
}
|
||||
|
||||
// Matches reports whether labels satisfy every matcher of c.
|
||||
func (c AlertmanagerConfig) Matches(labels map[string]string) bool {
|
||||
for _, m := range c.Matchers {
|
||||
v := labels[m.Label]
|
||||
var ok bool
|
||||
switch m.Op {
|
||||
case "=":
|
||||
ok = v == m.Value
|
||||
case "!=":
|
||||
ok = v != m.Value
|
||||
default:
|
||||
re, err := m.compile()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
ok = re.MatchString(v) == (m.Op == "=~")
|
||||
}
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user