Let a label specify its own ntfy topic
References: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/9
This commit is contained in:
parent
f4483532f5
commit
e85b5e6ea5
5 changed files with 30 additions and 4 deletions
|
@ -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.
|
||||
|
|
|
@ -51,6 +51,7 @@ labels {
|
|||
}
|
||||
|
||||
instance "example.com" {
|
||||
topic https://ntfy.sh/homeserver
|
||||
tags "computer,example"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ labels {
|
|||
|
||||
instance "example.com" {
|
||||
tags "computer,example"
|
||||
topic https://ntfy.sh/homeserver
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,7 +94,9 @@ cache {
|
|||
Call: "yes",
|
||||
},
|
||||
"severity:info": {Priority: "1"},
|
||||
"instance:example.com": {Tags: []string{"computer", "example"}},
|
||||
"instance:example.com": {
|
||||
Tags: []string{"computer", "example"},
|
||||
Topic: "https://ntfy.sh/homeserver"},
|
||||
},
|
||||
},
|
||||
Cache: CacheConfig{
|
||||
|
|
16
main.go
16
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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue