Let a label specify its own ntfy topic

References: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/9
This commit is contained in:
Thorben Günther 2024-11-06 14:15:22 +01:00
parent f4483532f5
commit e85b5e6ea5
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
5 changed files with 30 additions and 4 deletions

View file

@ -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.

View file

@ -51,6 +51,7 @@ labels {
}
instance "example.com" {
topic https://ntfy.sh/homeserver
tags "computer,example"
}
}

View file

@ -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
}
}

View file

@ -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{

16
main.go
View file

@ -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
}