opsgenie.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. opsgenieCreateAlertURL string = "https://api.opsgenie.com/v1/json/alert"
  37. opsgenieCloseAlertURL string = "https://api.opsgenie.com/v1/json/alert/close"
  38. )
  39. func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  40. autoClose := model.Settings.Get("autoClose").MustBool(true)
  41. apiKey := model.Settings.Get("apiKey").MustString()
  42. if apiKey == "" {
  43. return nil, alerting.ValidationError{Reason: "Could not find api key property in settings"}
  44. }
  45. return &OpsGenieNotifier{
  46. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  47. ApiKey: apiKey,
  48. AutoClose: autoClose,
  49. log: log.New("alerting.notifier.opsgenie"),
  50. }, nil
  51. }
  52. type OpsGenieNotifier struct {
  53. NotifierBase
  54. ApiKey string
  55. AutoClose bool
  56. log log.Logger
  57. }
  58. func (this *OpsGenieNotifier) Notify(evalContext *alerting.EvalContext) error {
  59. var err error
  60. switch evalContext.Rule.State {
  61. case m.AlertStateOK:
  62. if this.AutoClose {
  63. err = this.closeAlert(evalContext)
  64. }
  65. case m.AlertStateAlerting:
  66. err = this.createAlert(evalContext)
  67. }
  68. return err
  69. }
  70. func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error {
  71. this.log.Info("Creating OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  72. ruleUrl, err := evalContext.GetRuleUrl()
  73. if err != nil {
  74. this.log.Error("Failed get rule link", "error", err)
  75. return err
  76. }
  77. bodyJSON := simplejson.New()
  78. bodyJSON.Set("apiKey", this.ApiKey)
  79. bodyJSON.Set("message", evalContext.Rule.Name)
  80. bodyJSON.Set("source", "Grafana")
  81. bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  82. bodyJSON.Set("description", fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleUrl, evalContext.Rule.Message))
  83. details := simplejson.New()
  84. details.Set("url", ruleUrl)
  85. if evalContext.ImagePublicUrl != "" {
  86. details.Set("image", evalContext.ImagePublicUrl)
  87. }
  88. bodyJSON.Set("details", details)
  89. body, _ := bodyJSON.MarshalJSON()
  90. cmd := &m.SendWebhookSync{
  91. Url: opsgenieCreateAlertURL,
  92. Body: string(body),
  93. HttpMethod: "POST",
  94. }
  95. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  96. this.log.Error("Failed to send notification to OpsGenie", "error", err, "body", string(body))
  97. }
  98. return nil
  99. }
  100. func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) error {
  101. this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  102. bodyJSON := simplejson.New()
  103. bodyJSON.Set("apiKey", this.ApiKey)
  104. bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  105. body, _ := bodyJSON.MarshalJSON()
  106. cmd := &m.SendWebhookSync{
  107. Url: opsgenieCloseAlertURL,
  108. Body: string(body),
  109. HttpMethod: "POST",
  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. return err
  114. }
  115. return nil
  116. }