From e85b5e6ea55cf1403d0e9dee86b7244bc1ee0b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 6 Nov 2024 14:15:22 +0100 Subject: [PATCH] Let a label specify its own ntfy topic References: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/9 --- README.md | 2 +- config.scfg | 1 + config/config.go | 8 ++++++++ config/config_test.go | 7 +++++-- main.go | 16 +++++++++++++++- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8136574..0fc225d 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Furthermore you can take a look at [my deployment]. ntfy-alertmanager has support for setting ntfy [priority], [tags], [icon], [action buttons] (which can be used to e.g. create an Alertmanager silence or open the alert's Prometheus URL), [email notifications] and [phone calls]. -Define a decreasing order of labels in the config file and map those labels to tags, priority, an icon or an email address. +Define a decreasing order of labels in the config file and map those labels to tags, priority, an icon, an email address or an alternative ntfy topic. - For priority and icon the first found value will be chosen. Settings for "resolved" alerts will take precedence. - Tags are added together. diff --git a/config.scfg b/config.scfg index b8114bc..a23917d 100644 --- a/config.scfg +++ b/config.scfg @@ -51,6 +51,7 @@ labels { } instance "example.com" { + topic https://ntfy.sh/homeserver tags "computer,example" } } diff --git a/config/config.go b/config/config.go index 20a4e11..4cd40de 100644 --- a/config/config.go +++ b/config/config.go @@ -58,6 +58,7 @@ type labelConfig struct { Icon string EmailAddress string Call string + Topic string } // CacheConfig is the configuration of the cache. @@ -213,6 +214,13 @@ func parseBlock(block scfg.Block, config *Config) error { } } + d = labelDir.Children.Get("topic") + if d != nil { + if err := d.ParseParams(&labelConfig.Topic); err != nil { + return err + } + } + labels[fmt.Sprintf("%s:%s", labelName, name)] = *labelConfig } } diff --git a/config/config_test.go b/config/config_test.go index d72ca40..34c255d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -36,6 +36,7 @@ labels { instance "example.com" { tags "computer,example" + topic https://ntfy.sh/homeserver } } @@ -92,8 +93,10 @@ cache { EmailAddress: "foo@example.com", Call: "yes", }, - "severity:info": {Priority: "1"}, - "instance:example.com": {Tags: []string{"computer", "example"}}, + "severity:info": {Priority: "1"}, + "instance:example.com": { + Tags: []string{"computer", "example"}, + Topic: "https://ntfy.sh/homeserver"}, }, }, Cache: CacheConfig{ diff --git a/main.go b/main.go index 5145c18..53a9d91 100644 --- a/main.go +++ b/main.go @@ -69,6 +69,7 @@ type notification struct { fingerprint string status string generatorURL string + topic string } type ntfyError struct { @@ -154,6 +155,10 @@ func (br *bridge) singleAlertNotifications(p *payload) []*notification { n.call = labelConfig.Call } + if n.topic == "" { + n.topic = labelConfig.Topic + } + for _, val := range labelConfig.Tags { if !slices.Contains(tags, val) { tags = append(tags, val) @@ -264,6 +269,10 @@ func (br *bridge) multiAlertNotification(p *payload) *notification { n.call = labelConfig.Call } + if n.topic == "" { + n.topic = labelConfig.Topic + } + for _, val := range labelConfig.Tags { if !slices.Contains(tags, val) { tags = append(tags, val) @@ -293,7 +302,12 @@ func (br *bridge) multiAlertNotification(p *payload) *notification { } func (br *bridge) publish(n *notification) error { - req, err := http.NewRequest(http.MethodPost, br.cfg.Ntfy.Topic, strings.NewReader(n.body)) + url := br.cfg.Ntfy.Topic + if n.topic != "" { + url = n.topic + } + + req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(n.body)) if err != nil { return err } -- 2.48.1