From a329d4dc52a7141e8526c567996b9776c92f13ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 12 Feb 2023 16:32:34 +0100 Subject: [PATCH] silence: Fix broken ntfy action A silence action was added even when "base-url" was not set. This action would of course fail, since there was no valid URL scheme given. The action button is now removed in that case. --- main.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index ea9396c..35e2102 100644 --- a/main.go +++ b/main.go @@ -113,19 +113,19 @@ func (rcv *receiver) singleAlertNotifications(p *payload) []*notification { if rcv.cfg.am.SilenceDuration != 0 && alert.Status == "firing" { if rcv.cfg.BaseURL == "" { rcv.logger.Error("Failed to create silence action: No base-url set") - } + } else { + // I could not convince ntfy to accept an Action with a body which contains + // a json with more than one key. Instead the json will be base64 encoded + // and sent to the ntfy-alertmanager silences endpoint, that operates as + // a proxy and will do the Alertmanager API request. + s := &silenceBody{AlertManagerURL: p.ExternalURL, Labels: alert.Labels} + b, err := json.Marshal(s) + if err != nil { + rcv.logger.Errorf("Failed to create silence action: %v", err) + } - // I could not convince ntfy to accept an Action with a body which contains - // a json with more than one key. Instead the json will be base64 encoded - // and sent to the ntfy-alertmanager silences endpoint, that operates as - // a proxy and will do the Alertmanager API request. - s := &silenceBody{AlertManagerURL: p.ExternalURL, Labels: alert.Labels} - b, err := json.Marshal(s) - if err != nil { - rcv.logger.Errorf("Failed to create silence action: %v", err) + n.silenceBody = base64.StdEncoding.EncodeToString(b) } - - n.silenceBody = base64.StdEncoding.EncodeToString(b) } notifications = append(notifications, n) @@ -202,15 +202,16 @@ func (rcv *receiver) multiAlertNotification(p *payload) *notification { if rcv.cfg.am.SilenceDuration != 0 && p.Status == "firing" { if rcv.cfg.BaseURL == "" { rcv.logger.Error("Failed to create silence action: No base-url set") - } + } else { - s := &silenceBody{AlertManagerURL: p.ExternalURL, Labels: p.CommonLabels} - b, err := json.Marshal(s) - if err != nil { - rcv.logger.Errorf("Failed to create silence action: %v", err) - } + s := &silenceBody{AlertManagerURL: p.ExternalURL, Labels: p.CommonLabels} + b, err := json.Marshal(s) + if err != nil { + rcv.logger.Errorf("Failed to create silence action: %v", err) + } - n.silenceBody = base64.StdEncoding.EncodeToString(b) + n.silenceBody = base64.StdEncoding.EncodeToString(b) + } } return n