From 8a387ff8344de19cb21e2b5d0059d458a54e2978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 12 Feb 2023 17:08:58 +0100 Subject: [PATCH] Add tags for resolved alerts Implements: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/8 --- README.md | 5 +++++ config.go | 18 ++++++++++++++++++ config_test.go | 7 +++++++ main.go | 8 ++++++++ 4 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 87211f1..5f611ff 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,11 @@ labels { } } +# Set tags for resolved alerts +resolved { + tags "resolved,partying_face" +} + ntfy { # URL of the ntfy topic - required topic https://ntfy.sh/alertmanager-alerts diff --git a/config.go b/config.go index ef80b24..de285a6 100644 --- a/config.go +++ b/config.go @@ -26,6 +26,7 @@ type config struct { labels labels cache cacheConfig am alertmanagerConfig + resolved resolvedConfig } type ntfyConfig struct { @@ -56,6 +57,10 @@ type alertmanagerConfig struct { URL string } +type resolvedConfig struct { + Tags []string +} + func readConfig(path string) (*config, error) { cfg, err := scfg.Load(path) 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 } diff --git a/config_test.go b/config_test.go index 0c03fed..0fcdc3d 100644 --- a/config_test.go +++ b/config_test.go @@ -41,6 +41,10 @@ labels { } } +resolved { + tags "resolved,partying_face" +} + ntfy { # URL of the ntfy topic - required topic https://ntfy.sh/alertmanager-alerts @@ -93,6 +97,9 @@ cache { Password: "pass", URL: "https://alertmanager.xenrox.net", }, + resolved: resolvedConfig{ + Tags: []string{"resolved", "partying_face"}, + }, } configPath := filepath.Join(t.TempDir(), "config") diff --git a/main.go b/main.go index d158722..cd0500c 100644 --- a/main.go +++ b/main.go @@ -89,6 +89,10 @@ func (rcv *receiver) singleAlertNotifications(p *payload) []*notification { } var tags []string + if alert.Status == "resolved" { + tags = append(tags, rcv.cfg.resolved.Tags...) + } + for _, labelName := range rcv.cfg.labels.Order { val, ok := alert.Labels[labelName] if !ok { @@ -176,6 +180,10 @@ func (rcv *receiver) multiAlertNotification(p *payload) *notification { var priority string var tags []string + if p.Status == "resolved" { + tags = append(tags, rcv.cfg.resolved.Tags...) + } + for _, labelName := range rcv.cfg.labels.Order { val, ok := p.CommonLabels[labelName] if !ok { -- 2.44.0