opsgenie.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package notifiers
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. )
  11. func init() {
  12. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  13. Type: "opsgenie",
  14. Name: "OpsGenie",
  15. Description: "Sends notifications to OpsGenie",
  16. Factory: NewOpsGenieNotifier,
  17. OptionsTemplate: `
  18. <h3 class="page-heading">OpsGenie settings</h3>
  19. <div class="gf-form">
  20. <span class="gf-form-label width-14">API Key</span>
  21. <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.apiKey" placeholder="OpsGenie API Key"></input>
  22. </div>
  23. <div class="gf-form">
  24. <gf-form-switch
  25. class="gf-form"
  26. label="Auto close incidents"
  27. label-class="width-14"
  28. checked="ctrl.model.settings.autoClose"
  29. tooltip="Automatically close alerts in OpsGenie once the alert goes back to ok.">
  30. </gf-form-switch>
  31. </div>
  32. `,
  33. })
  34. }
  35. var (
  36. opsgenieAlertURL string = "https://api.opsgenie.com/v2/alerts"
  37. )
  38. func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  39. autoClose := model.Settings.Get("autoClose").MustBool(true)
  40. apiKey := model.Settings.Get("apiKey").MustString()
  41. if apiKey == "" {
  42. return nil, alerting.ValidationError{Reason: "Could not find api key property in settings"}
  43. }
  44. return &OpsGenieNotifier{
  45. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  46. ApiKey: apiKey,
  47. AutoClose: autoClose,
  48. log: log.New("alerting.notifier.opsgenie"),
  49. }, nil
  50. }
  51. type OpsGenieNotifier struct {
  52. NotifierBase
  53. ApiKey string
  54. AutoClose bool
  55. log log.Logger
  56. }
  57. func (this *OpsGenieNotifier) ShouldNotify(context *alerting.EvalContext) bool {
  58. return defaultShouldNotify(context)
  59. }
  60. func (this *OpsGenieNotifier) Notify(evalContext *alerting.EvalContext) error {
  61. var err error
  62. switch evalContext.Rule.State {
  63. case m.AlertStateOK:
  64. if this.AutoClose {
  65. err = this.closeAlert(evalContext)
  66. }
  67. case m.AlertStateAlerting:
  68. err = this.createAlert(evalContext)
  69. }
  70. return err
  71. }
  72. func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error {
  73. this.log.Info("Creating OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  74. ruleUrl, err := evalContext.GetRuleUrl()
  75. if err != nil {
  76. this.log.Error("Failed get rule link", "error", err)
  77. return err
  78. }
  79. bodyJSON := simplejson.New()
  80. bodyJSON.Set("message", evalContext.Rule.Name)
  81. bodyJSON.Set("source", "Grafana")
  82. bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  83. bodyJSON.Set("description", fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleUrl, evalContext.Rule.Message))
  84. details := simplejson.New()
  85. details.Set("url", ruleUrl)
  86. if evalContext.ImagePublicUrl != "" {
  87. details.Set("image", evalContext.ImagePublicUrl)
  88. }
  89. bodyJSON.Set("details", details)
  90. body, _ := bodyJSON.MarshalJSON()
  91. cmd := &m.SendWebhookSync{
  92. Url: opsgenieAlertURL,
  93. Body: string(body),
  94. HttpMethod: "POST",
  95. HttpHeader: map[string]string{
  96. "Content-Type": "application/json",
  97. "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
  98. },
  99. }
  100. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  101. this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
  102. }
  103. return nil
  104. }
  105. func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) error {
  106. this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  107. bodyJSON := simplejson.New()
  108. bodyJSON.Set("source", "Grafana")
  109. body, _ := bodyJSON.MarshalJSON()
  110. cmd := &m.SendWebhookSync{
  111. Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", opsgenieAlertURL, evalContext.Rule.Id),
  112. Body: string(body),
  113. HttpMethod: "POST",
  114. HttpHeader: map[string]string{
  115. "Content-Type": "application/json",
  116. "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
  117. },
  118. }
  119. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  120. this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
  121. return err
  122. }
  123. return nil
  124. }