2016-10-29 00:19:51 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
2018-01-16 11:12:42 -06:00
|
|
|
"os"
|
2016-11-08 13:37:11 -06:00
|
|
|
"strconv"
|
2020-01-23 06:20:07 -06:00
|
|
|
"strings"
|
2018-01-16 12:20:31 -06:00
|
|
|
"time"
|
2016-11-08 13:37:11 -06:00
|
|
|
|
2016-10-29 00:19:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-05-14 01:15:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-10-29 00:19:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-01-06 05:04:25 -06:00
|
|
|
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
|
|
|
Type: "pagerduty",
|
|
|
|
Name: "PagerDuty",
|
|
|
|
Description: "Sends notifications to PagerDuty",
|
|
|
|
Factory: NewPagerdutyNotifier,
|
|
|
|
OptionsTemplate: `
|
|
|
|
<h3 class="page-heading">PagerDuty settings</h3>
|
|
|
|
<div class="gf-form">
|
|
|
|
<span class="gf-form-label width-14">Integration Key</span>
|
2017-08-03 01:57:59 -05:00
|
|
|
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.integrationKey" placeholder="Pagerduty Integration Key"></input>
|
2017-01-06 05:04:25 -06:00
|
|
|
</div>
|
2019-12-24 01:32:05 -06:00
|
|
|
<div class="gf-form">
|
2020-03-13 08:19:35 -05:00
|
|
|
<span class="gf-form-label width-14">Severity</span>
|
2019-12-24 01:32:05 -06:00
|
|
|
<div class="gf-form-select-wrapper width-14">
|
|
|
|
<select
|
|
|
|
class="gf-form-input"
|
|
|
|
ng-model="ctrl.model.settings.severity"
|
|
|
|
ng-options="s for s in ['critical', 'error', 'warning', 'info']">
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-01-06 05:04:25 -06:00
|
|
|
<div class="gf-form">
|
|
|
|
<gf-form-switch
|
|
|
|
class="gf-form"
|
|
|
|
label="Auto resolve incidents"
|
|
|
|
label-class="width-14"
|
|
|
|
checked="ctrl.model.settings.autoResolve"
|
|
|
|
tooltip="Resolve incidents in pagerduty once the alert goes back to ok.">
|
|
|
|
</gf-form-switch>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
})
|
2016-10-29 00:19:51 -05:00
|
|
|
}
|
|
|
|
|
2016-11-08 00:50:58 -06:00
|
|
|
var (
|
2019-05-20 08:23:06 -05:00
|
|
|
pagerdutyEventAPIURL = "https://events.pagerduty.com/v2/enqueue"
|
2016-11-08 00:50:58 -06:00
|
|
|
)
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// NewPagerdutyNotifier is the constructor for the PagerDuty notifier
|
2019-05-14 01:15:05 -05:00
|
|
|
func NewPagerdutyNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
|
2019-12-24 01:32:05 -06:00
|
|
|
severity := model.Settings.Get("severity").MustString("critical")
|
2017-12-22 03:54:48 -06:00
|
|
|
autoResolve := model.Settings.Get("autoResolve").MustBool(false)
|
2016-10-29 00:19:51 -05:00
|
|
|
key := model.Settings.Get("integrationKey").MustString()
|
|
|
|
if key == "" {
|
|
|
|
return nil, alerting.ValidationError{Reason: "Could not find integration key property in settings"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PagerdutyNotifier{
|
2018-06-05 03:27:29 -05:00
|
|
|
NotifierBase: NewNotifierBase(model),
|
2016-11-08 00:50:58 -06:00
|
|
|
Key: key,
|
2019-12-24 01:32:05 -06:00
|
|
|
Severity: severity,
|
2016-11-08 13:37:11 -06:00
|
|
|
AutoResolve: autoResolve,
|
2016-11-08 00:50:58 -06:00
|
|
|
log: log.New("alerting.notifier.pagerduty"),
|
2016-10-29 00:19:51 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
// PagerdutyNotifier is responsible for sending
|
|
|
|
// alert notifications to pagerduty
|
2016-10-29 00:19:51 -05:00
|
|
|
type PagerdutyNotifier struct {
|
|
|
|
NotifierBase
|
2016-11-08 13:37:11 -06:00
|
|
|
Key string
|
2019-12-24 01:32:05 -06:00
|
|
|
Severity string
|
2016-11-08 13:37:11 -06:00
|
|
|
AutoResolve bool
|
|
|
|
log log.Logger
|
2016-10-29 00:19:51 -05:00
|
|
|
}
|
|
|
|
|
2020-01-23 06:20:07 -06:00
|
|
|
// buildEventPayload is responsible for building the event payload body for sending to Pagerduty v2 API
|
|
|
|
func (pn *PagerdutyNotifier) buildEventPayload(evalContext *alerting.EvalContext) ([]byte, error) {
|
2016-11-08 13:37:11 -06:00
|
|
|
|
|
|
|
eventType := "trigger"
|
2019-05-14 01:15:05 -05:00
|
|
|
if evalContext.Rule.State == models.AlertStateOK {
|
2016-11-08 13:37:11 -06:00
|
|
|
eventType = "resolve"
|
|
|
|
}
|
2019-12-18 03:42:25 -06:00
|
|
|
customData := simplejson.New()
|
2017-09-20 05:03:18 -05:00
|
|
|
for _, evt := range evalContext.EvalMatches {
|
2019-12-18 03:42:25 -06:00
|
|
|
customData.Set(evt.Metric, evt.Value)
|
2017-05-26 06:30:56 -05:00
|
|
|
}
|
2016-11-08 13:37:11 -06:00
|
|
|
|
2019-05-20 08:23:06 -05:00
|
|
|
pn.log.Info("Notifying Pagerduty", "event_type", eventType)
|
2016-11-08 13:37:11 -06:00
|
|
|
|
2018-01-16 11:12:42 -06:00
|
|
|
payloadJSON := simplejson.New()
|
2019-09-20 03:39:27 -05:00
|
|
|
|
2020-01-23 06:20:07 -06:00
|
|
|
// set default, override in following case switch if defined
|
|
|
|
payloadJSON.Set("component", "Grafana")
|
|
|
|
|
|
|
|
for _, tag := range evalContext.Rule.AlertRuleTags {
|
|
|
|
customData.Set(tag.Key, tag.Value)
|
|
|
|
|
|
|
|
// Override tags appropriately if they are in the PagerDuty v2 API
|
|
|
|
switch strings.ToLower(tag.Key) {
|
|
|
|
case "group":
|
|
|
|
payloadJSON.Set("group", tag.Value)
|
|
|
|
case "class":
|
|
|
|
payloadJSON.Set("class", tag.Value)
|
|
|
|
case "component":
|
|
|
|
payloadJSON.Set("component", tag.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 03:39:27 -05:00
|
|
|
summary := evalContext.Rule.Name + " - " + evalContext.Rule.Message
|
|
|
|
if len(summary) > 1024 {
|
|
|
|
summary = summary[0:1024]
|
|
|
|
}
|
|
|
|
payloadJSON.Set("summary", summary)
|
|
|
|
|
2018-01-16 11:12:42 -06:00
|
|
|
if hostname, err := os.Hostname(); err == nil {
|
|
|
|
payloadJSON.Set("source", hostname)
|
|
|
|
}
|
2019-12-24 01:32:05 -06:00
|
|
|
payloadJSON.Set("severity", pn.Severity)
|
2018-01-16 11:12:42 -06:00
|
|
|
payloadJSON.Set("timestamp", time.Now())
|
|
|
|
payloadJSON.Set("custom_details", customData)
|
2016-11-08 13:37:11 -06:00
|
|
|
bodyJSON := simplejson.New()
|
2019-05-20 08:23:06 -05:00
|
|
|
bodyJSON.Set("routing_key", pn.Key)
|
2018-01-16 11:12:42 -06:00
|
|
|
bodyJSON.Set("event_action", eventType)
|
2019-06-03 03:25:58 -05:00
|
|
|
bodyJSON.Set("dedup_key", "alertId-"+strconv.FormatInt(evalContext.Rule.ID, 10))
|
2018-01-16 11:12:42 -06:00
|
|
|
bodyJSON.Set("payload", payloadJSON)
|
2016-11-08 13:37:11 -06:00
|
|
|
|
2019-06-03 03:25:58 -05:00
|
|
|
ruleURL, err := evalContext.GetRuleURL()
|
2016-11-08 13:37:11 -06:00
|
|
|
if err != nil {
|
2019-05-20 08:23:06 -05:00
|
|
|
pn.log.Error("Failed get rule link", "error", err)
|
2020-01-23 06:20:07 -06:00
|
|
|
return []byte{}, err
|
2016-11-08 13:37:11 -06:00
|
|
|
}
|
2018-01-16 11:12:42 -06:00
|
|
|
links := make([]interface{}, 1)
|
|
|
|
linkJSON := simplejson.New()
|
2019-05-20 08:23:06 -05:00
|
|
|
linkJSON.Set("href", ruleURL)
|
|
|
|
bodyJSON.Set("client_url", ruleURL)
|
2018-03-13 07:10:55 -05:00
|
|
|
bodyJSON.Set("client", "Grafana")
|
2020-01-23 06:20:07 -06:00
|
|
|
|
2018-01-16 11:12:42 -06:00
|
|
|
links[0] = linkJSON
|
|
|
|
bodyJSON.Set("links", links)
|
2016-11-08 13:37:11 -06:00
|
|
|
|
2019-06-03 03:25:58 -05:00
|
|
|
if evalContext.ImagePublicURL != "" {
|
2016-11-08 13:37:11 -06:00
|
|
|
contexts := make([]interface{}, 1)
|
|
|
|
imageJSON := simplejson.New()
|
2019-06-03 03:25:58 -05:00
|
|
|
imageJSON.Set("src", evalContext.ImagePublicURL)
|
2016-11-08 13:37:11 -06:00
|
|
|
contexts[0] = imageJSON
|
2018-01-16 11:12:42 -06:00
|
|
|
bodyJSON.Set("images", contexts)
|
2016-11-08 13:37:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
body, _ := bodyJSON.MarshalJSON()
|
|
|
|
|
2020-01-23 06:20:07 -06:00
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify sends an alert notification to PagerDuty
|
|
|
|
func (pn *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
|
|
|
|
|
|
|
|
if evalContext.Rule.State == models.AlertStateOK && !pn.AutoResolve {
|
|
|
|
pn.log.Info("Not sending a trigger to Pagerduty", "state", evalContext.Rule.State, "auto resolve", pn.AutoResolve)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := pn.buildEventPayload(evalContext)
|
|
|
|
if err != nil {
|
|
|
|
pn.log.Error("Unable to build PagerDuty event payload", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-14 01:15:05 -05:00
|
|
|
cmd := &models.SendWebhookSync{
|
2019-05-20 08:23:06 -05:00
|
|
|
Url: pagerdutyEventAPIURL,
|
2016-11-08 13:37:11 -06:00
|
|
|
Body: string(body),
|
|
|
|
HttpMethod: "POST",
|
2018-01-16 11:12:42 -06:00
|
|
|
HttpHeader: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2016-11-08 13:37:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
2019-05-20 08:23:06 -05:00
|
|
|
pn.log.Error("Failed to send notification to Pagerduty", "error", err, "body", string(body))
|
2016-12-12 03:20:50 -06:00
|
|
|
return err
|
2016-10-29 00:19:51 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|