opsgenie.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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) Notify(evalContext *alerting.EvalContext) error {
  58. var err error
  59. switch evalContext.Rule.State {
  60. case m.AlertStateOK:
  61. if this.AutoClose {
  62. err = this.closeAlert(evalContext)
  63. }
  64. case m.AlertStateAlerting:
  65. err = this.createAlert(evalContext)
  66. }
  67. return err
  68. }
  69. func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error {
  70. this.log.Info("Creating OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  71. ruleUrl, err := evalContext.GetRuleUrl()
  72. if err != nil {
  73. this.log.Error("Failed get rule link", "error", err)
  74. return err
  75. }
  76. bodyJSON := simplejson.New()
  77. bodyJSON.Set("message", evalContext.Rule.Name)
  78. bodyJSON.Set("source", "Grafana")
  79. bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  80. bodyJSON.Set("description", fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleUrl, evalContext.Rule.Message))
  81. details := simplejson.New()
  82. details.Set("url", ruleUrl)
  83. if evalContext.ImagePublicUrl != "" {
  84. details.Set("image", evalContext.ImagePublicUrl)
  85. }
  86. bodyJSON.Set("details", details)
  87. body, _ := bodyJSON.MarshalJSON()
  88. cmd := &m.SendWebhookSync{
  89. Url: opsgenieAlertURL,
  90. Body: string(body),
  91. HttpMethod: "POST",
  92. HttpHeader: map[string]string{
  93. "Content-Type": "application/json",
  94. "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
  95. },
  96. }
  97. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  98. this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
  99. }
  100. return nil
  101. }
  102. func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) error {
  103. this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  104. bodyJSON := simplejson.New()
  105. bodyJSON.Set("source", "Grafana")
  106. body, _ := bodyJSON.MarshalJSON()
  107. cmd := &m.SendWebhookSync{
  108. Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", opsgenieAlertURL, evalContext.Rule.Id),
  109. Body: string(body),
  110. HttpMethod: "POST",
  111. HttpHeader: map[string]string{
  112. "Content-Type": "application/json",
  113. "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey),
  114. },
  115. }
  116. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  117. this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
  118. return err
  119. }
  120. return nil
  121. }