From 7efc6cdbc23099963878a9c271c780079593455d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Fri, 7 Jul 2023 21:37:11 +0200 Subject: [PATCH] Add phone call support Implements: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/18 --- README.md | 7 ++++++- config.go | 16 ++++++++++++++++ config_test.go | 2 ++ main.go | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bac826a..5ea7e07 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ the configuration file will be read from `/etc/ntfy-alertmanager/config`. The fo of this file is [scfg]. ntfy-alertmanager has support for setting ntfy [priority], [tags], [icon], [action buttons] -(which can be used to create an Alertmanager silence) and [email notifications]. +(which can be used to create an Alertmanager silence), [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. - For priority and icon the first found value will be chosen. An icon for "resolved" alerts will take precedence. @@ -49,6 +49,8 @@ labels { icon "https://foo.com/critical.png" # Forward messages which severity "critical" to the specified email address. email-address foo@bar.com + # Call the specified number. Use `yes` to pick the first of your verified numbers. + call yes } severity "info" { @@ -77,6 +79,8 @@ ntfy { access-token foobar # Forward all messages to the specified email address. email-address foo@bar.com + # Call the specified number for all alerts. Use `yes` to pick the first of your verified numbers. + call +123456789 } alertmanager { @@ -140,6 +144,7 @@ or write to me directly on matrix [@xenrox:xenrox.net]. [icon]: https://docs.ntfy.sh/publish/#icons [action buttons]: https://docs.ntfy.sh/publish/#action-buttons [email notifications]: https://docs.ntfy.sh/publish/#e-mail-notifications +[phone calls]: https://ntfy.xenrox.net/docs/publish/#phone-calls [issue tracker]: https://todo.xenrox.net/~xenrox/ntfy-alertmanager [mailing list]: https://lists.xenrox.net/~xenrox/public-inbox [@xenrox:xenrox.net]: https://matrix.to/#/@xenrox:xenrox.net diff --git a/config.go b/config.go index 4488bb0..6bf6f24 100644 --- a/config.go +++ b/config.go @@ -43,6 +43,7 @@ type ntfyConfig struct { Password string AccessToken string emailAddress string + call string } type labels struct { @@ -55,6 +56,7 @@ type labelConfig struct { Tags []string Icon string emailAddress string + call string } type cacheConfig struct { @@ -210,6 +212,13 @@ func readConfig(path string) (*config, error) { } } + d = labelDir.Children.Get("call") + if d != nil { + if err := d.ParseParams(&labelConfig.call); err != nil { + return nil, err + } + } + labels[fmt.Sprintf("%s:%s", labelName, name)] = *labelConfig } } @@ -267,6 +276,13 @@ func readConfig(path string) (*config, error) { } } + d = ntfyDir.Children.Get("call") + if d != nil { + if err := d.ParseParams(&config.ntfy.call); err != nil { + return nil, err + } + } + cacheDir := cfg.Get("cache") if cacheDir != nil { diff --git a/config_test.go b/config_test.go index 8114ab4..774479c 100644 --- a/config_test.go +++ b/config_test.go @@ -25,6 +25,7 @@ labels { tags "rotating_light" icon "https://foo.com/critical.png" email-address foo@bar.com + call yes } severity "info" { @@ -76,6 +77,7 @@ cache { Tags: []string{"rotating_light"}, Icon: "https://foo.com/critical.png", emailAddress: "foo@bar.com", + call: "yes", }, "severity:info": {Priority: "1"}, "instance:example.com": {Tags: []string{"computer", "example"}}, diff --git a/main.go b/main.go index 3a577b0..defc73e 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,7 @@ type notification struct { tags string icon string emailAddress string + call string silenceBody string fingerprint string status string @@ -102,6 +103,7 @@ func (br *bridge) singleAlertNotifications(p *payload) []*notification { } n.emailAddress = br.cfg.ntfy.emailAddress + n.call = br.cfg.ntfy.call for _, labelName := range br.cfg.labels.Order { val, ok := alert.Labels[labelName] @@ -126,6 +128,10 @@ func (br *bridge) singleAlertNotifications(p *payload) []*notification { n.emailAddress = labelConfig.emailAddress } + if n.call == "" { + n.call = labelConfig.call + } + for _, val := range labelConfig.Tags { if !sliceContains(tags, val) { tags = append(tags, val) @@ -205,6 +211,7 @@ func (br *bridge) multiAlertNotification(p *payload) *notification { } n.emailAddress = br.cfg.ntfy.emailAddress + n.call = br.cfg.ntfy.call for _, labelName := range br.cfg.labels.Order { val, ok := p.CommonLabels[labelName] @@ -229,6 +236,10 @@ func (br *bridge) multiAlertNotification(p *payload) *notification { n.emailAddress = labelConfig.emailAddress } + if n.call == "" { + n.call = labelConfig.call + } + for _, val := range labelConfig.Tags { if !sliceContains(tags, val) { tags = append(tags, val) @@ -287,6 +298,10 @@ func (br *bridge) publish(n *notification) error { req.Header.Set("X-Email", n.emailAddress) } + if n.call != "" { + req.Header.Set("X-Call", n.call) + } + if n.silenceBody != "" { url := br.cfg.BaseURL + "/silences"