From d8f6c73aabdacc9db959cbcf12f01720fb66e62f Mon Sep 17 00:00:00 2001 From: cglrkn Date: Mon, 2 Oct 2017 16:42:09 +0300 Subject: [PATCH 1/4] Update script to make it use OpsGenie's REST API The script is making API calls to the deprecated OpsGenie Web API, we updated the script to make it use new OpsGenie's REST API. --- pkg/services/alerting/notifiers/opsgenie.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/services/alerting/notifiers/opsgenie.go b/pkg/services/alerting/notifiers/opsgenie.go index e67ccfb10e5..5af9fa69611 100644 --- a/pkg/services/alerting/notifiers/opsgenie.go +++ b/pkg/services/alerting/notifiers/opsgenie.go @@ -37,8 +37,7 @@ func init() { } var ( - opsgenieCreateAlertURL string = "https://api.opsgenie.com/v1/json/alert" - opsgenieCloseAlertURL string = "https://api.opsgenie.com/v1/json/alert/close" + opsgenieAlertURL string = "https://api.opsgenie.com/v2/alerts" ) func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) { @@ -87,7 +86,6 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err } bodyJSON := simplejson.New() - bodyJSON.Set("apiKey", this.ApiKey) bodyJSON.Set("message", evalContext.Rule.Name) bodyJSON.Set("source", "Grafana") bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)) @@ -103,9 +101,13 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err body, _ := bodyJSON.MarshalJSON() cmd := &m.SendWebhookSync{ - Url: opsgenieCreateAlertURL, + Url: opsgenieAlertURL, Body: string(body), HttpMethod: "POST", + HttpHeader: map[string]string{ + "Content-Type": "application/json", + "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), + }, } if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { @@ -119,14 +121,17 @@ func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) erro this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name) bodyJSON := simplejson.New() - bodyJSON.Set("apiKey", this.ApiKey) - bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)) + bodyJSON.Set("source", "Grafana") body, _ := bodyJSON.MarshalJSON() cmd := &m.SendWebhookSync{ - Url: opsgenieCloseAlertURL, + Url: fmt.Sprintf("%s/%s/close?identifierType=alias", opsgenieAlertURL,"alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)), Body: string(body), HttpMethod: "POST", + HttpHeader: map[string]string{ + "Content-Type": "application/json", + "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), + }, } if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { From ef518f6ecdaf15417e4a18751dfa84c669a7ca93 Mon Sep 17 00:00:00 2001 From: cglrkn Date: Tue, 3 Oct 2017 15:13:05 +0300 Subject: [PATCH 2/4] Build URL for close alert request differently --- pkg/services/alerting/notifiers/opsgenie.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/services/alerting/notifiers/opsgenie.go b/pkg/services/alerting/notifiers/opsgenie.go index 5af9fa69611..75ddfc3cf78 100644 --- a/pkg/services/alerting/notifiers/opsgenie.go +++ b/pkg/services/alerting/notifiers/opsgenie.go @@ -125,7 +125,7 @@ func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) erro body, _ := bodyJSON.MarshalJSON() cmd := &m.SendWebhookSync{ - Url: fmt.Sprintf("%s/%s/close?identifierType=alias", opsgenieAlertURL,"alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)), + Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", opsgenieAlertURL, evalContext.Rule.Id) Body: string(body), HttpMethod: "POST", HttpHeader: map[string]string{ From 548bc6b7c4af31567150b1544e219aea35a62039 Mon Sep 17 00:00:00 2001 From: cglrkn Date: Wed, 4 Oct 2017 10:15:39 +0300 Subject: [PATCH 3/4] Add the missing comma --- pkg/services/alerting/notifiers/opsgenie.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/services/alerting/notifiers/opsgenie.go b/pkg/services/alerting/notifiers/opsgenie.go index 75ddfc3cf78..9cc11da674b 100644 --- a/pkg/services/alerting/notifiers/opsgenie.go +++ b/pkg/services/alerting/notifiers/opsgenie.go @@ -125,7 +125,7 @@ func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) erro body, _ := bodyJSON.MarshalJSON() cmd := &m.SendWebhookSync{ - Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", opsgenieAlertURL, evalContext.Rule.Id) + Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", opsgenieAlertURL, evalContext.Rule.Id), Body: string(body), HttpMethod: "POST", HttpHeader: map[string]string{ From 5a3f32521ea26c0275aac8ecae233a8dd0834112 Mon Sep 17 00:00:00 2001 From: Cagla Arikan Date: Thu, 5 Oct 2017 08:42:45 +0300 Subject: [PATCH 4/4] Fix formatting issue --- pkg/services/alerting/notifiers/opsgenie.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/services/alerting/notifiers/opsgenie.go b/pkg/services/alerting/notifiers/opsgenie.go index 9cc11da674b..aea02465177 100644 --- a/pkg/services/alerting/notifiers/opsgenie.go +++ b/pkg/services/alerting/notifiers/opsgenie.go @@ -104,10 +104,10 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err Url: opsgenieAlertURL, Body: string(body), HttpMethod: "POST", - HttpHeader: map[string]string{ - "Content-Type": "application/json", - "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), - }, + HttpHeader: map[string]string{ + "Content-Type": "application/json", + "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), + }, } if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { @@ -128,10 +128,10 @@ func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) erro Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", opsgenieAlertURL, evalContext.Rule.Id), Body: string(body), HttpMethod: "POST", - HttpHeader: map[string]string{ - "Content-Type": "application/json", - "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), - }, + HttpHeader: map[string]string{ + "Content-Type": "application/json", + "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), + }, } if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {