Add tags for resolved alerts

Implements: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/8
This commit is contained in:
Thorben Günther 2023-02-12 17:08:58 +01:00
parent df02ee5303
commit 8a387ff834
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
4 changed files with 38 additions and 0 deletions

View file

@ -56,6 +56,11 @@ labels {
} }
} }
# Set tags for resolved alerts
resolved {
tags "resolved,partying_face"
}
ntfy { ntfy {
# URL of the ntfy topic - required # URL of the ntfy topic - required
topic https://ntfy.sh/alertmanager-alerts topic https://ntfy.sh/alertmanager-alerts

View file

@ -26,6 +26,7 @@ type config struct {
labels labels labels labels
cache cacheConfig cache cacheConfig
am alertmanagerConfig am alertmanagerConfig
resolved resolvedConfig
} }
type ntfyConfig struct { type ntfyConfig struct {
@ -56,6 +57,10 @@ type alertmanagerConfig struct {
URL string URL string
} }
type resolvedConfig struct {
Tags []string
}
func readConfig(path string) (*config, error) { func readConfig(path string) (*config, error) {
cfg, err := scfg.Load(path) cfg, err := scfg.Load(path)
if err != nil { if err != nil {
@ -272,5 +277,18 @@ func readConfig(path string) (*config, error) {
} }
} }
resolvedDir := cfg.Get("resolved")
if resolvedDir != nil {
d = resolvedDir.Children.Get("tags")
if d != nil {
var tags string
if err := d.ParseParams(&tags); err != nil {
return nil, err
}
config.resolved.Tags = strings.Split(tags, ",")
}
}
return config, nil return config, nil
} }

View file

@ -41,6 +41,10 @@ labels {
} }
} }
resolved {
tags "resolved,partying_face"
}
ntfy { ntfy {
# URL of the ntfy topic - required # URL of the ntfy topic - required
topic https://ntfy.sh/alertmanager-alerts topic https://ntfy.sh/alertmanager-alerts
@ -93,6 +97,9 @@ cache {
Password: "pass", Password: "pass",
URL: "https://alertmanager.xenrox.net", URL: "https://alertmanager.xenrox.net",
}, },
resolved: resolvedConfig{
Tags: []string{"resolved", "partying_face"},
},
} }
configPath := filepath.Join(t.TempDir(), "config") configPath := filepath.Join(t.TempDir(), "config")

View file

@ -89,6 +89,10 @@ func (rcv *receiver) singleAlertNotifications(p *payload) []*notification {
} }
var tags []string var tags []string
if alert.Status == "resolved" {
tags = append(tags, rcv.cfg.resolved.Tags...)
}
for _, labelName := range rcv.cfg.labels.Order { for _, labelName := range rcv.cfg.labels.Order {
val, ok := alert.Labels[labelName] val, ok := alert.Labels[labelName]
if !ok { if !ok {
@ -176,6 +180,10 @@ func (rcv *receiver) multiAlertNotification(p *payload) *notification {
var priority string var priority string
var tags []string var tags []string
if p.Status == "resolved" {
tags = append(tags, rcv.cfg.resolved.Tags...)
}
for _, labelName := range rcv.cfg.labels.Order { for _, labelName := range rcv.cfg.labels.Order {
val, ok := p.CommonLabels[labelName] val, ok := p.CommonLabels[labelName]
if !ok { if !ok {