opsgenie.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/infra/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. <span class="gf-form-label width-14">Alert API Url</span>
  25. <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>
  26. </div>
  27. <div class="gf-form">
  28. <gf-form-switch
  29. class="gf-form"
  30. label="Auto close incidents"
  31. label-class="width-14"
  32. checked="ctrl.model.settings.autoClose"
  33. tooltip="Automatically close alerts in OpsGenie once the alert goes back to ok.">
  34. </gf-form-switch>
  35. </div>
  36. `,
  37. })
  38. }
  39. var (
  40. opsgenieAlertURL = "https://api.opsgenie.com/v2/alerts"
  41. )
  42. func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  43. autoClose := model.Settings.Get("autoClose").MustBool(true)
  44. apiKey := model.Settings.Get("apiKey").MustString()
  45. apiUrl := model.Settings.Get("apiUrl").MustString()
  46. if apiKey == "" {
  47. return nil, alerting.ValidationError{Reason: "Could not find api key property in settings"}
  48. }
  49. if apiUrl == "" {
  50. apiUrl = opsgenieAlertURL
  51. }
  52. return &OpsGenieNotifier{
  53. NotifierBase: NewNotifierBase(model),
  54. ApiKey: apiKey,
  55. ApiUrl: apiUrl,
  56. AutoClose: autoClose,
  57. log: log.New("alerting.notifier.opsgenie"),
  58. }, nil
  59. }
  60. type OpsGenieNotifier struct {
  61. NotifierBase
  62. ApiKey string
  63. ApiUrl string
  64. AutoClose bool
  65. log log.Logger
  66. }
  67. func (this *OpsGenieNotifier) Notify(evalContext *alerting.EvalContext) error {
  68. var err error
  69. switch evalContext.Rule.State {
  70. case m.AlertStateOK:
  71. if this.AutoClose {
  72. err = this.closeAlert(evalContext)
  73. }
  74. case m.AlertStateAlerting:
  75. err = this.createAlert(evalContext)
  76. }
  77. return err
  78. }
  79. func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error {
  80. this.log.Info("Creating OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  81. ruleUrl, err := evalContext.GetRuleUrl()
  82. if err != nil {
  83. this.log.Error("Failed get rule link", "error", err)
  84. return err
  85. }
  86. customData := triggMetrString
  87. for _, evt := range evalContext.EvalMatches {
  88. customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value)
  89. }
  90. bodyJSON := simplejson.New()
  91. bodyJSON.Set("message", evalContext.Rule.Name)
  92. bodyJSON.Set("source", "Grafana")
  93. bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  94. bodyJSON.Set("description", fmt.Sprintf("%s - %s\n%s\n%s", evalContext.Rule.Name, ruleUrl, evalContext.Rule.Message, customData))
  95. details := simplejson.New()
  96. details.Set("url", ruleUrl)
  97. if evalContext.ImagePublicUrl != "" {
  98. details.Set("image", evalContext.ImagePublicUrl)
  99. }
  100. bodyJSON.Set("details", details)
  101. body, _ := bodyJSON.MarshalJSON()
  102. cmd := &m.SendWebhookSync{
  103. Url: this.ApiUrl,
  104. Body: string(body),
  105. HttpMethod: "POST",
  106. HttpHeader: map[string]string{
  107. "Content-Type": "application/json",
  108. "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
  109. },
  110. }
  111. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  112. this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
  113. }
  114. return nil
  115. }
  116. func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) error {
  117. this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  118. bodyJSON := simplejson.New()
  119. bodyJSON.Set("source", "Grafana")
  120. body, _ := bodyJSON.MarshalJSON()
  121. cmd := &m.SendWebhookSync{
  122. Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", this.ApiUrl, evalContext.Rule.Id),
  123. Body: string(body),
  124. HttpMethod: "POST",
  125. HttpHeader: map[string]string{
  126. "Content-Type": "application/json",
  127. "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
  128. },
  129. }
  130. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  131. this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
  132. return err
  133. }
  134. return nil
  135. }