forked from grafana/grafana
/
webhook.go
162 lines (142 loc) · 4.46 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package notifiers
import (
"context"
"encoding/json"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/alerting/models"
"github.com/grafana/grafana/pkg/services/notifications"
"github.com/grafana/grafana/pkg/setting"
)
func init() {
alerting.RegisterNotifier(&alerting.NotifierPlugin{
Type: "webhook",
Name: "webhook",
Description: "Sends HTTP POST request to a URL",
Heading: "Webhook settings",
Factory: NewWebHookNotifier,
Options: []alerting.NotifierOption{
{
Label: "Url",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
PropertyName: "url",
Required: true,
},
{
Label: "Http Method",
Element: alerting.ElementTypeSelect,
SelectOptions: []alerting.SelectOption{
{
Value: "POST",
Label: "POST",
},
{
Value: "PUT",
Label: "PUT",
},
},
PropertyName: "httpMethod",
},
{
Label: "Username",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
PropertyName: "username",
},
{
Label: "Password",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypePassword,
PropertyName: "password",
Secure: true,
},
},
})
}
// NewWebHookNotifier is the constructor for
// the WebHook notifier.
func NewWebHookNotifier(model *models.AlertNotification, fn alerting.GetDecryptedValueFn, ns notifications.Service) (alerting.Notifier, error) {
url := model.Settings.Get("url").MustString()
if url == "" {
return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
}
password := fn(context.Background(), model.SecureSettings, "password", model.Settings.Get("password").MustString(), setting.SecretKey)
return &WebhookNotifier{
NotifierBase: NewNotifierBase(model, ns),
URL: url,
User: model.Settings.Get("username").MustString(),
Password: password,
HTTPMethod: model.Settings.Get("httpMethod").MustString("POST"),
log: log.New("alerting.notifier.webhook"),
}, nil
}
// WebhookNotifier is responsible for sending
// alert notifications as webhooks.
type WebhookNotifier struct {
NotifierBase
URL string
User string
Password string
HTTPMethod string
log log.Logger
}
// WebhookNotifierBody is the body of webhook
// notification channel
type WebhookNotifierBody struct {
Title string `json:"title"`
RuleID int64 `json:"ruleId"`
RuleName string `json:"ruleName"`
State models.AlertStateType `json:"state"`
EvalMatches []*alerting.EvalMatch `json:"evalMatches"`
OrgID int64 `json:"orgId"`
DashboardID int64 `json:"dashboardId"`
PanelID int64 `json:"panelId"`
Tags map[string]string `json:"tags"`
RuleURL string `json:"ruleUrl,omitempty"`
ImageURL string `json:"imageUrl,omitempty"`
Message string `json:"message,omitempty"`
}
// Notify send alert notifications as
// webhook as http requests.
func (wn *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
wn.log.Info("Sending webhook")
body := WebhookNotifierBody{
Title: evalContext.GetNotificationTitle(),
RuleID: evalContext.Rule.ID,
RuleName: evalContext.Rule.Name,
State: evalContext.Rule.State,
EvalMatches: evalContext.EvalMatches,
OrgID: evalContext.Rule.OrgID,
DashboardID: evalContext.Rule.DashboardID,
PanelID: evalContext.Rule.PanelID,
}
tags := make(map[string]string)
for _, tag := range evalContext.Rule.AlertRuleTags {
tags[tag.Key] = tag.Value
}
body.Tags = tags
ruleURL, err := evalContext.GetRuleURL()
if err == nil {
body.RuleURL = ruleURL
}
if wn.NeedsImage() && evalContext.ImagePublicURL != "" {
body.ImageURL = evalContext.ImagePublicURL
}
if evalContext.Rule.Message != "" {
body.Message = evalContext.Rule.Message
}
bodyJSON, _ := json.Marshal(body)
cmd := ¬ifications.SendWebhookSync{
Url: wn.URL,
User: wn.User,
Password: wn.Password,
Body: string(bodyJSON),
HttpMethod: wn.HTTPMethod,
}
if err := wn.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
wn.log.Error("Failed to send webhook", "error", err, "webhook", wn.Name)
return err
}
return nil
}