2016-12-06 14:48:13 -06:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-01-06 05:04:25 -06:00
|
|
|
alerting.RegisterNotifier(&alerting.NotifierPlugin{
|
|
|
|
Type: "opsgenie",
|
|
|
|
Name: "OpsGenie",
|
|
|
|
Description: "Sends notifications to OpsGenie",
|
|
|
|
Factory: NewOpsGenieNotifier,
|
|
|
|
OptionsTemplate: `
|
|
|
|
<h3 class="page-heading">OpsGenie settings</h3>
|
|
|
|
<div class="gf-form">
|
|
|
|
<span class="gf-form-label width-14">API Key</span>
|
|
|
|
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.apiKey" placeholder="OpsGenie API Key"></input>
|
|
|
|
</div>
|
2018-01-22 08:27:45 -06:00
|
|
|
<div class="gf-form">
|
|
|
|
<span class="gf-form-label width-14">Alert API Url</span>
|
|
|
|
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.apiUrl" placeholder="https://api.opsgenie.com/v2/alerts"></input>
|
|
|
|
</div>
|
2017-01-06 05:04:25 -06:00
|
|
|
<div class="gf-form">
|
|
|
|
<gf-form-switch
|
|
|
|
class="gf-form"
|
|
|
|
label="Auto close incidents"
|
|
|
|
label-class="width-14"
|
|
|
|
checked="ctrl.model.settings.autoClose"
|
2017-03-17 02:27:52 -05:00
|
|
|
tooltip="Automatically close alerts in OpsGenie once the alert goes back to ok.">
|
2017-01-06 05:04:25 -06:00
|
|
|
</gf-form-switch>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
})
|
2016-12-06 14:48:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-04-27 15:14:36 -05:00
|
|
|
opsgenieAlertURL = "https://api.opsgenie.com/v2/alerts"
|
2016-12-06 14:48:13 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
|
|
|
|
autoClose := model.Settings.Get("autoClose").MustBool(true)
|
|
|
|
apiKey := model.Settings.Get("apiKey").MustString()
|
2018-01-22 08:27:45 -06:00
|
|
|
apiUrl := model.Settings.Get("apiUrl").MustString()
|
2016-12-06 14:48:13 -06:00
|
|
|
if apiKey == "" {
|
|
|
|
return nil, alerting.ValidationError{Reason: "Could not find api key property in settings"}
|
|
|
|
}
|
2018-01-22 08:27:45 -06:00
|
|
|
if apiUrl == "" {
|
|
|
|
apiUrl = opsgenieAlertURL
|
|
|
|
}
|
2016-12-06 14:48:13 -06:00
|
|
|
|
|
|
|
return &OpsGenieNotifier{
|
2018-06-05 03:27:29 -05:00
|
|
|
NotifierBase: NewNotifierBase(model),
|
2016-12-06 14:48:13 -06:00
|
|
|
ApiKey: apiKey,
|
2018-01-22 08:27:45 -06:00
|
|
|
ApiUrl: apiUrl,
|
2016-12-06 14:48:13 -06:00
|
|
|
AutoClose: autoClose,
|
|
|
|
log: log.New("alerting.notifier.opsgenie"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OpsGenieNotifier struct {
|
|
|
|
NotifierBase
|
|
|
|
ApiKey string
|
2018-01-22 08:27:45 -06:00
|
|
|
ApiUrl string
|
2016-12-06 14:48:13 -06:00
|
|
|
AutoClose bool
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *OpsGenieNotifier) Notify(evalContext *alerting.EvalContext) error {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
switch evalContext.Rule.State {
|
|
|
|
case m.AlertStateOK:
|
|
|
|
if this.AutoClose {
|
|
|
|
err = this.closeAlert(evalContext)
|
|
|
|
}
|
|
|
|
case m.AlertStateAlerting:
|
|
|
|
err = this.createAlert(evalContext)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error {
|
|
|
|
this.log.Info("Creating OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
|
|
|
|
|
|
|
|
ruleUrl, err := evalContext.GetRuleUrl()
|
|
|
|
if err != nil {
|
|
|
|
this.log.Error("Failed get rule link", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-28 16:26:11 -06:00
|
|
|
customData := "Triggered metrics:\n\n"
|
|
|
|
for _, evt := range evalContext.EvalMatches {
|
|
|
|
customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value)
|
|
|
|
}
|
|
|
|
|
2016-12-06 14:48:13 -06:00
|
|
|
bodyJSON := simplejson.New()
|
|
|
|
bodyJSON.Set("message", evalContext.Rule.Name)
|
|
|
|
bodyJSON.Set("source", "Grafana")
|
|
|
|
bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
|
2018-02-28 16:26:11 -06:00
|
|
|
bodyJSON.Set("description", fmt.Sprintf("%s - %s\n%s\n%s", evalContext.Rule.Name, ruleUrl, evalContext.Rule.Message, customData))
|
2016-12-06 14:48:13 -06:00
|
|
|
|
|
|
|
details := simplejson.New()
|
|
|
|
details.Set("url", ruleUrl)
|
|
|
|
if evalContext.ImagePublicUrl != "" {
|
|
|
|
details.Set("image", evalContext.ImagePublicUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyJSON.Set("details", details)
|
|
|
|
body, _ := bodyJSON.MarshalJSON()
|
|
|
|
|
|
|
|
cmd := &m.SendWebhookSync{
|
2018-01-22 08:27:45 -06:00
|
|
|
Url: this.ApiUrl,
|
2016-12-06 14:48:13 -06:00
|
|
|
Body: string(body),
|
|
|
|
HttpMethod: "POST",
|
2017-10-05 00:42:45 -05:00
|
|
|
HttpHeader: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
|
|
|
|
},
|
2016-12-06 14:48:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
|
|
|
this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) error {
|
|
|
|
this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
|
|
|
|
|
|
|
|
bodyJSON := simplejson.New()
|
2017-10-02 08:42:09 -05:00
|
|
|
bodyJSON.Set("source", "Grafana")
|
2016-12-06 14:48:13 -06:00
|
|
|
body, _ := bodyJSON.MarshalJSON()
|
|
|
|
|
|
|
|
cmd := &m.SendWebhookSync{
|
2018-01-22 08:27:45 -06:00
|
|
|
Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", this.ApiUrl, evalContext.Rule.Id),
|
2016-12-06 14:48:13 -06:00
|
|
|
Body: string(body),
|
|
|
|
HttpMethod: "POST",
|
2017-10-05 00:42:45 -05:00
|
|
|
HttpHeader: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
|
|
|
|
},
|
2016-12-06 14:48:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
|
|
|
|
this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
|
2016-12-12 03:20:50 -06:00
|
|
|
return err
|
2016-12-06 14:48:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|