pagerduty.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package notifiers
  2. import (
  3. "os"
  4. "time"
  5. "strconv"
  6. "fmt"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/components/simplejson"
  9. "github.com/grafana/grafana/pkg/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 string = "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.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  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) ShouldNotify(context *alerting.EvalContext) bool {
  60. return defaultShouldNotify(context)
  61. }
  62. func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
  63. if evalContext.Rule.State == m.AlertStateOK && !this.AutoResolve {
  64. this.log.Info("Not sending a trigger to Pagerduty", "state", evalContext.Rule.State, "auto resolve", this.AutoResolve)
  65. return nil
  66. }
  67. eventType := "trigger"
  68. if evalContext.Rule.State == m.AlertStateOK {
  69. eventType = "resolve"
  70. }
  71. customData := "Triggered metrics:\n\n"
  72. for _, evt := range evalContext.EvalMatches {
  73. customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value)
  74. }
  75. this.log.Info("Notifying Pagerduty", "event_type", eventType)
  76. payloadJSON := simplejson.New()
  77. payloadJSON.Set("summary", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
  78. if hostname, err := os.Hostname(); err == nil {
  79. payloadJSON.Set("source", hostname)
  80. }
  81. payloadJSON.Set("severity", "critical")
  82. payloadJSON.Set("timestamp", time.Now())
  83. payloadJSON.Set("component", "Grafana")
  84. payloadJSON.Set("custom_details", customData)
  85. bodyJSON := simplejson.New()
  86. bodyJSON.Set("routing_key", this.Key)
  87. bodyJSON.Set("event_action", eventType)
  88. bodyJSON.Set("dedup_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  89. bodyJSON.Set("payload", payloadJSON)
  90. ruleUrl, err := evalContext.GetRuleUrl()
  91. if err != nil {
  92. this.log.Error("Failed get rule link", "error", err)
  93. return err
  94. }
  95. links := make([]interface{}, 1)
  96. linkJSON := simplejson.New()
  97. linkJSON.Set("href", ruleUrl)
  98. links[0] = linkJSON
  99. bodyJSON.Set("links", links)
  100. if evalContext.ImagePublicUrl != "" {
  101. contexts := make([]interface{}, 1)
  102. imageJSON := simplejson.New()
  103. imageJSON.Set("src", evalContext.ImagePublicUrl)
  104. contexts[0] = imageJSON
  105. bodyJSON.Set("images", contexts)
  106. }
  107. body, _ := bodyJSON.MarshalJSON()
  108. cmd := &m.SendWebhookSync{
  109. Url: pagerdutyEventApiUrl,
  110. Body: string(body),
  111. HttpMethod: "POST",
  112. HttpHeader: map[string]string{
  113. "Content-Type": "application/json",
  114. },
  115. }
  116. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  117. this.log.Error("Failed to send notification to Pagerduty", "error", err, "body", string(body))
  118. return err
  119. }
  120. return nil
  121. }