pagerduty.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. summary := evalContext.Rule.Name + " - " + evalContext.Rule.Message
  79. if len(summary) > 1024 {
  80. summary = summary[0:1024]
  81. }
  82. payloadJSON.Set("summary", summary)
  83. if hostname, err := os.Hostname(); err == nil {
  84. payloadJSON.Set("source", hostname)
  85. }
  86. payloadJSON.Set("severity", "critical")
  87. payloadJSON.Set("timestamp", time.Now())
  88. payloadJSON.Set("component", "Grafana")
  89. payloadJSON.Set("custom_details", customData)
  90. bodyJSON := simplejson.New()
  91. bodyJSON.Set("routing_key", pn.Key)
  92. bodyJSON.Set("event_action", eventType)
  93. bodyJSON.Set("dedup_key", "alertId-"+strconv.FormatInt(evalContext.Rule.ID, 10))
  94. bodyJSON.Set("payload", payloadJSON)
  95. ruleURL, err := evalContext.GetRuleURL()
  96. if err != nil {
  97. pn.log.Error("Failed get rule link", "error", err)
  98. return err
  99. }
  100. links := make([]interface{}, 1)
  101. linkJSON := simplejson.New()
  102. linkJSON.Set("href", ruleURL)
  103. bodyJSON.Set("client_url", ruleURL)
  104. bodyJSON.Set("client", "Grafana")
  105. links[0] = linkJSON
  106. bodyJSON.Set("links", links)
  107. if evalContext.ImagePublicURL != "" {
  108. contexts := make([]interface{}, 1)
  109. imageJSON := simplejson.New()
  110. imageJSON.Set("src", evalContext.ImagePublicURL)
  111. contexts[0] = imageJSON
  112. bodyJSON.Set("images", contexts)
  113. }
  114. body, _ := bodyJSON.MarshalJSON()
  115. cmd := &models.SendWebhookSync{
  116. Url: pagerdutyEventAPIURL,
  117. Body: string(body),
  118. HttpMethod: "POST",
  119. HttpHeader: map[string]string{
  120. "Content-Type": "application/json",
  121. },
  122. }
  123. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  124. pn.log.Error("Failed to send notification to Pagerduty", "error", err, "body", string(body))
  125. return err
  126. }
  127. return nil
  128. }