2021-04-23 08:29:28 -05:00
|
|
|
package channels
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-03-14 18:27:10 -05:00
|
|
|
"errors"
|
2021-04-23 08:29:28 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-05-26 00:29:56 -05:00
|
|
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
2022-01-26 09:42:40 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/notifications"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
"github.com/prometheus/common/model"
|
2021-04-23 08:29:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// WebhookNotifier is responsible for sending
|
|
|
|
// alert notifications as webhooks.
|
|
|
|
type WebhookNotifier struct {
|
2021-10-22 04:11:06 -05:00
|
|
|
*Base
|
2021-04-23 08:29:28 -05:00
|
|
|
URL string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
HTTPMethod string
|
|
|
|
MaxAlerts int
|
|
|
|
log log.Logger
|
2022-01-26 09:42:40 -06:00
|
|
|
ns notifications.WebhookSender
|
2022-05-23 03:44:19 -05:00
|
|
|
images ImageStore
|
2021-04-23 08:29:28 -05:00
|
|
|
tmpl *template.Template
|
2021-10-08 07:52:44 -05:00
|
|
|
orgID int64
|
2021-04-23 08:29:28 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 18:27:10 -05:00
|
|
|
type WebhookConfig struct {
|
|
|
|
*NotificationChannelConfig
|
|
|
|
URL string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
HTTPMethod string
|
|
|
|
MaxAlerts int
|
|
|
|
}
|
|
|
|
|
|
|
|
func WebHookFactory(fc FactoryConfig) (NotificationChannel, error) {
|
|
|
|
cfg, err := NewWebHookConfig(fc.Config, fc.DecryptFunc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, receiverInitError{
|
|
|
|
Reason: err.Error(),
|
|
|
|
Cfg: *fc.Config,
|
|
|
|
}
|
2021-08-17 07:49:05 -05:00
|
|
|
}
|
2022-05-23 03:44:19 -05:00
|
|
|
return NewWebHookNotifier(cfg, fc.NotificationService, fc.ImageStore, fc.Template), nil
|
2022-03-14 18:27:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebHookConfig(config *NotificationChannelConfig, decryptFunc GetDecryptedValueFn) (*WebhookConfig, error) {
|
|
|
|
url := config.Settings.Get("url").MustString()
|
2021-04-23 08:29:28 -05:00
|
|
|
if url == "" {
|
2022-03-14 18:27:10 -05:00
|
|
|
return nil, errors.New("could not find url property in settings")
|
2021-04-23 08:29:28 -05:00
|
|
|
}
|
2022-03-14 18:27:10 -05:00
|
|
|
return &WebhookConfig{
|
|
|
|
NotificationChannelConfig: config,
|
|
|
|
URL: url,
|
|
|
|
User: config.Settings.Get("username").MustString(),
|
|
|
|
Password: decryptFunc(context.Background(), config.SecureSettings, "password", config.Settings.Get("password").MustString()),
|
|
|
|
HTTPMethod: config.Settings.Get("httpMethod").MustString("POST"),
|
|
|
|
MaxAlerts: config.Settings.Get("maxAlerts").MustInt(0),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWebHookNotifier is the constructor for
|
|
|
|
// the WebHook notifier.
|
2022-05-23 03:44:19 -05:00
|
|
|
func NewWebHookNotifier(config *WebhookConfig, ns notifications.WebhookSender, images ImageStore, t *template.Template) *WebhookNotifier {
|
2021-04-23 08:29:28 -05:00
|
|
|
return &WebhookNotifier{
|
2021-10-22 04:11:06 -05:00
|
|
|
Base: NewBase(&models.AlertNotification{
|
2022-03-14 18:27:10 -05:00
|
|
|
Uid: config.UID,
|
|
|
|
Name: config.Name,
|
|
|
|
Type: config.Type,
|
|
|
|
DisableResolveMessage: config.DisableResolveMessage,
|
|
|
|
Settings: config.Settings,
|
2021-05-18 03:04:47 -05:00
|
|
|
}),
|
2022-03-14 18:27:10 -05:00
|
|
|
orgID: config.OrgID,
|
|
|
|
URL: config.URL,
|
|
|
|
User: config.User,
|
|
|
|
Password: config.Password,
|
|
|
|
HTTPMethod: config.HTTPMethod,
|
|
|
|
MaxAlerts: config.MaxAlerts,
|
2021-05-18 03:04:47 -05:00
|
|
|
log: log.New("alerting.notifier.webhook"),
|
2022-01-26 09:42:40 -06:00
|
|
|
ns: ns,
|
2022-05-23 03:44:19 -05:00
|
|
|
images: images,
|
2021-05-18 03:04:47 -05:00
|
|
|
tmpl: t,
|
2022-03-14 18:27:10 -05:00
|
|
|
}
|
2021-04-23 08:29:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// webhookMessage defines the JSON object send to webhook endpoints.
|
|
|
|
type webhookMessage struct {
|
2021-05-26 09:49:39 -05:00
|
|
|
*ExtendedData
|
2021-04-23 08:29:28 -05:00
|
|
|
|
|
|
|
// The protocol version.
|
|
|
|
Version string `json:"version"`
|
|
|
|
GroupKey string `json:"groupKey"`
|
|
|
|
TruncatedAlerts int `json:"truncatedAlerts"`
|
2021-10-08 07:52:44 -05:00
|
|
|
OrgID int64 `json:"orgId"`
|
2021-04-23 08:29:28 -05:00
|
|
|
|
|
|
|
// Deprecated, to be removed in 8.1.
|
|
|
|
// These are present to make migration a little less disruptive.
|
|
|
|
Title string `json:"title"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (wn *WebhookNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
|
|
|
groupKey, err := notify.ExtractGroupKey(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
as, numTruncated := truncateAlerts(wn.MaxAlerts, as)
|
|
|
|
var tmplErr error
|
2021-06-03 09:09:32 -05:00
|
|
|
tmpl, data := TmplText(ctx, wn.tmpl, as, wn.log, &tmplErr)
|
2022-05-23 03:44:19 -05:00
|
|
|
|
|
|
|
// Augment our Alert data with ImageURLs if available.
|
2022-05-26 00:29:56 -05:00
|
|
|
_ = withStoredImages(ctx, wn.log, wn.images,
|
|
|
|
func(index int, image *ngmodels.Image) error {
|
|
|
|
if image != nil && len(image.URL) != 0 {
|
|
|
|
data.Alerts[index].ImageURL = image.URL
|
2022-05-23 03:44:19 -05:00
|
|
|
}
|
2022-05-26 00:29:56 -05:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
as...)
|
2022-05-23 03:44:19 -05:00
|
|
|
|
2021-04-23 08:29:28 -05:00
|
|
|
msg := &webhookMessage{
|
|
|
|
Version: "1",
|
2021-05-26 09:49:39 -05:00
|
|
|
ExtendedData: data,
|
2021-04-23 08:29:28 -05:00
|
|
|
GroupKey: groupKey.String(),
|
|
|
|
TruncatedAlerts: numTruncated,
|
2021-10-08 07:52:44 -05:00
|
|
|
OrgID: wn.orgID,
|
2022-01-05 09:47:08 -06:00
|
|
|
Title: tmpl(DefaultMessageTitleEmbed),
|
2021-04-23 08:29:28 -05:00
|
|
|
Message: tmpl(`{{ template "default.message" . }}`),
|
|
|
|
}
|
|
|
|
if types.Alerts(as...).Status() == model.AlertFiring {
|
|
|
|
msg.State = string(models.AlertStateAlerting)
|
|
|
|
} else {
|
|
|
|
msg.State = string(models.AlertStateOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmplErr != nil {
|
2022-01-05 09:47:08 -06:00
|
|
|
wn.log.Warn("failed to template webhook message", "err", tmplErr.Error())
|
2021-04-23 08:29:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &models.SendWebhookSync{
|
|
|
|
Url: wn.URL,
|
|
|
|
User: wn.User,
|
|
|
|
Password: wn.Password,
|
|
|
|
Body: string(body),
|
|
|
|
HttpMethod: wn.HTTPMethod,
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:42:40 -06:00
|
|
|
if err := wn.ns.SendWebhookSync(ctx, cmd); err != nil {
|
2021-04-23 08:29:28 -05:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func truncateAlerts(maxAlerts int, alerts []*types.Alert) ([]*types.Alert, int) {
|
|
|
|
if maxAlerts > 0 && len(alerts) > maxAlerts {
|
|
|
|
return alerts[:maxAlerts], len(alerts) - maxAlerts
|
|
|
|
}
|
|
|
|
|
|
|
|
return alerts, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wn *WebhookNotifier) SendResolved() bool {
|
|
|
|
return !wn.GetDisableResolveMessage()
|
|
|
|
}
|