Commit d8f6c73a by cglrkn Committed by GitHub

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.
parent d4e5a7d3
...@@ -37,8 +37,7 @@ func init() { ...@@ -37,8 +37,7 @@ func init() {
} }
var ( var (
opsgenieCreateAlertURL string = "https://api.opsgenie.com/v1/json/alert" opsgenieAlertURL string = "https://api.opsgenie.com/v2/alerts"
opsgenieCloseAlertURL string = "https://api.opsgenie.com/v1/json/alert/close"
) )
func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) { func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
...@@ -87,7 +86,6 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err ...@@ -87,7 +86,6 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err
} }
bodyJSON := simplejson.New() bodyJSON := simplejson.New()
bodyJSON.Set("apiKey", this.ApiKey)
bodyJSON.Set("message", evalContext.Rule.Name) bodyJSON.Set("message", evalContext.Rule.Name)
bodyJSON.Set("source", "Grafana") bodyJSON.Set("source", "Grafana")
bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)) bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
...@@ -103,9 +101,13 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err ...@@ -103,9 +101,13 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err
body, _ := bodyJSON.MarshalJSON() body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{ cmd := &m.SendWebhookSync{
Url: opsgenieCreateAlertURL, Url: opsgenieAlertURL,
Body: string(body), Body: string(body),
HttpMethod: "POST", 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 { if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
...@@ -119,14 +121,17 @@ func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) erro ...@@ -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) this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
bodyJSON := simplejson.New() bodyJSON := simplejson.New()
bodyJSON.Set("apiKey", this.ApiKey) bodyJSON.Set("source", "Grafana")
bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
body, _ := bodyJSON.MarshalJSON() body, _ := bodyJSON.MarshalJSON()
cmd := &m.SendWebhookSync{ cmd := &m.SendWebhookSync{
Url: opsgenieCloseAlertURL, Url: fmt.Sprintf("%s/%s/close?identifierType=alias", opsgenieAlertURL,"alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)),
Body: string(body), Body: string(body),
HttpMethod: "POST", 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 { if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment