pagerduty.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package notifiers
  2. import (
  3. "os"
  4. "strconv"
  5. "time"
  6. "fmt"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/components/simplejson"
  9. "github.com/grafana/grafana/pkg/infra/log"
  10. m "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/services/alerting"
  12. )
  13. func init() {
  14. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  15. Type: "pagerduty",
  16. Name: "PagerDuty",
  17. Description: "Sends notifications to PagerDuty",
  18. Factory: NewPagerdutyNotifier,
  19. OptionsTemplate: `
  20. <h3 class="page-heading">PagerDuty settings</h3>
  21. <div class="gf-form">
  22. <span class="gf-form-label width-14">Integration Key</span>
  23. <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.integrationKey" placeholder="Pagerduty Integration Key"></input>
  24. </div>
  25. <div class="gf-form">
  26. <gf-form-switch
  27. class="gf-form"
  28. label="Auto resolve incidents"
  29. label-class="width-14"
  30. checked="ctrl.model.settings.autoResolve"
  31. tooltip="Resolve incidents in pagerduty once the alert goes back to ok.">
  32. </gf-form-switch>
  33. </div>
  34. `,
  35. })
  36. }
  37. var (
  38. pagerdutyEventApiUrl = "https://events.pagerduty.com/v2/enqueue"
  39. )
  40. func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  41. autoResolve := model.Settings.Get("autoResolve").MustBool(false)
  42. key := model.Settings.Get("integrationKey").MustString()
  43. if key == "" {
  44. return nil, alerting.ValidationError{Reason: "Could not find integration key property in settings"}
  45. }
  46. return &PagerdutyNotifier{
  47. NotifierBase: NewNotifierBase(model),
  48. Key: key,
  49. AutoResolve: autoResolve,
  50. log: log.New("alerting.notifier.pagerduty"),
  51. }, nil
  52. }
  53. type PagerdutyNotifier struct {
  54. NotifierBase
  55. Key string
  56. AutoResolve bool
  57. log log.Logger
  58. }
  59. func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
  60. if evalContext.Rule.State == m.AlertStateOK && !this.AutoResolve {
  61. this.log.Info("Not sending a trigger to Pagerduty", "state", evalContext.Rule.State, "auto resolve", this.AutoResolve)
  62. return nil
  63. }
  64. eventType := "trigger"
  65. if evalContext.Rule.State == m.AlertStateOK {
  66. eventType = "resolve"
  67. }
  68. customData := triggMetrString
  69. for _, evt := range evalContext.EvalMatches {
  70. customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value)
  71. }
  72. this.log.Info("Notifying Pagerduty", "event_type", eventType)
  73. payloadJSON := simplejson.New()
  74. payloadJSON.Set("summary", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
  75. if hostname, err := os.Hostname(); err == nil {
  76. payloadJSON.Set("source", hostname)
  77. }
  78. payloadJSON.Set("severity", "critical")
  79. payloadJSON.Set("timestamp", time.Now())
  80. payloadJSON.Set("component", "Grafana")
  81. payloadJSON.Set("custom_details", customData)
  82. bodyJSON := simplejson.New()
  83. bodyJSON.Set("routing_key", this.Key)
  84. bodyJSON.Set("event_action", eventType)
  85. bodyJSON.Set("dedup_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  86. bodyJSON.Set("payload", payloadJSON)
  87. ruleUrl, err := evalContext.GetRuleUrl()
  88. if err != nil {
  89. this.log.Error("Failed get rule link", "error", err)
  90. return err
  91. }
  92. links := make([]interface{}, 1)
  93. linkJSON := simplejson.New()
  94. linkJSON.Set("href", ruleUrl)
  95. bodyJSON.Set("client_url", ruleUrl)
  96. bodyJSON.Set("client", "Grafana")
  97. links[0] = linkJSON
  98. bodyJSON.Set("links", links)
  99. if evalContext.ImagePublicUrl != "" {
  100. contexts := make([]interface{}, 1)
  101. imageJSON := simplejson.New()
  102. imageJSON.Set("src", evalContext.ImagePublicUrl)
  103. contexts[0] = imageJSON
  104. bodyJSON.Set("images", contexts)
  105. }
  106. body, _ := bodyJSON.MarshalJSON()
  107. cmd := &m.SendWebhookSync{
  108. Url: pagerdutyEventApiUrl,
  109. Body: string(body),
  110. HttpMethod: "POST",
  111. HttpHeader: map[string]string{
  112. "Content-Type": "application/json",
  113. },
  114. }
  115. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  116. this.log.Error("Failed to send notification to Pagerduty", "error", err, "body", string(body))
  117. return err
  118. }
  119. return nil
  120. }