pagerduty.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "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. // NewPagerdutyNotifier is the constructor for the PagerDuty notifier
  41. func NewPagerdutyNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  42. autoResolve := model.Settings.Get("autoResolve").MustBool(false)
  43. key := model.Settings.Get("integrationKey").MustString()
  44. if key == "" {
  45. return nil, alerting.ValidationError{Reason: "Could not find integration key property in settings"}
  46. }
  47. return &PagerdutyNotifier{
  48. NotifierBase: NewNotifierBase(model),
  49. Key: key,
  50. AutoResolve: autoResolve,
  51. log: log.New("alerting.notifier.pagerduty"),
  52. }, nil
  53. }
  54. // PagerdutyNotifier is responsible for sending
  55. // alert notifications to pagerduty
  56. type PagerdutyNotifier struct {
  57. NotifierBase
  58. Key string
  59. AutoResolve bool
  60. log log.Logger
  61. }
  62. // Notify sends an alert notification to PagerDuty
  63. func (pn *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
  64. if evalContext.Rule.State == models.AlertStateOK && !pn.AutoResolve {
  65. pn.log.Info("Not sending a trigger to Pagerduty", "state", evalContext.Rule.State, "auto resolve", pn.AutoResolve)
  66. return nil
  67. }
  68. eventType := "trigger"
  69. if evalContext.Rule.State == models.AlertStateOK {
  70. eventType = "resolve"
  71. }
  72. customData := triggMetrString
  73. for _, evt := range evalContext.EvalMatches {
  74. customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value)
  75. }
  76. pn.log.Info("Notifying Pagerduty", "event_type", eventType)
  77. payloadJSON := simplejson.New()
  78. payloadJSON.Set("summary", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
  79. if hostname, err := os.Hostname(); err == nil {
  80. payloadJSON.Set("source", hostname)
  81. }
  82. payloadJSON.Set("severity", "critical")
  83. payloadJSON.Set("timestamp", time.Now())
  84. payloadJSON.Set("component", "Grafana")
  85. payloadJSON.Set("custom_details", customData)
  86. bodyJSON := simplejson.New()
  87. bodyJSON.Set("routing_key", pn.Key)
  88. bodyJSON.Set("event_action", eventType)
  89. bodyJSON.Set("dedup_key", "alertId-"+strconv.FormatInt(evalContext.Rule.ID, 10))
  90. bodyJSON.Set("payload", payloadJSON)
  91. ruleURL, err := evalContext.GetRuleURL()
  92. if err != nil {
  93. pn.log.Error("Failed get rule link", "error", err)
  94. return err
  95. }
  96. links := make([]interface{}, 1)
  97. linkJSON := simplejson.New()
  98. linkJSON.Set("href", ruleURL)
  99. bodyJSON.Set("client_url", ruleURL)
  100. bodyJSON.Set("client", "Grafana")
  101. links[0] = linkJSON
  102. bodyJSON.Set("links", links)
  103. if evalContext.ImagePublicURL != "" {
  104. contexts := make([]interface{}, 1)
  105. imageJSON := simplejson.New()
  106. imageJSON.Set("src", evalContext.ImagePublicURL)
  107. contexts[0] = imageJSON
  108. bodyJSON.Set("images", contexts)
  109. }
  110. body, _ := bodyJSON.MarshalJSON()
  111. cmd := &models.SendWebhookSync{
  112. Url: pagerdutyEventAPIURL,
  113. Body: string(body),
  114. HttpMethod: "POST",
  115. HttpHeader: map[string]string{
  116. "Content-Type": "application/json",
  117. },
  118. }
  119. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  120. pn.log.Error("Failed to send notification to Pagerduty", "error", err, "body", string(body))
  121. return err
  122. }
  123. return nil
  124. }