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.
This commit is contained in:
Thorben Günther 2023-02-12 16:32:34 +01:00
parent 38fa5f9f71
commit a329d4dc52
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED

37
main.go
View file

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