pagerduty.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package notifiers
  2. import (
  3. "strconv"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/metrics"
  9. m "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/services/alerting"
  11. )
  12. func init() {
  13. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  14. Type: "pagerduty",
  15. Name: "PagerDuty",
  16. Description: "Sends notifications to PagerDuty",
  17. Factory: NewPagerdutyNotifier,
  18. OptionsTemplate: `
  19. <h3 class="page-heading">PagerDuty settings</h3>
  20. <div class="gf-form">
  21. <span class="gf-form-label width-14">Integration Key</span>
  22. <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.integrationKey" placeholder="Pagerduty integeration Key"></input>
  23. </div>
  24. <div class="gf-form">
  25. <gf-form-switch
  26. class="gf-form"
  27. label="Auto resolve incidents"
  28. label-class="width-14"
  29. checked="ctrl.model.settings.autoResolve"
  30. tooltip="Resolve incidents in pagerduty once the alert goes back to ok.">
  31. </gf-form-switch>
  32. </div>
  33. `,
  34. })
  35. }
  36. var (
  37. pagerdutyEventApiUrl string = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
  38. )
  39. func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  40. autoResolve := model.Settings.Get("autoResolve").MustBool(true)
  41. key := model.Settings.Get("integrationKey").MustString()
  42. if key == "" {
  43. return nil, alerting.ValidationError{Reason: "Could not find integration key property in settings"}
  44. }
  45. return &PagerdutyNotifier{
  46. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  47. Key: key,
  48. AutoResolve: autoResolve,
  49. log: log.New("alerting.notifier.pagerduty"),
  50. }, nil
  51. }
  52. type PagerdutyNotifier struct {
  53. NotifierBase
  54. Key string
  55. AutoResolve bool
  56. log log.Logger
  57. }
  58. func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
  59. metrics.M_Alerting_Notification_Sent_PagerDuty.Inc(1)
  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 := "Triggered metrics:\n\n"
  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. bodyJSON := simplejson.New()
  74. bodyJSON.Set("service_key", this.Key)
  75. bodyJSON.Set("description", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
  76. bodyJSON.Set("client", "Grafana")
  77. bodyJSON.Set("details", customData)
  78. bodyJSON.Set("event_type", eventType)
  79. bodyJSON.Set("incident_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  80. ruleUrl, err := evalContext.GetRuleUrl()
  81. if err != nil {
  82. this.log.Error("Failed get rule link", "error", err)
  83. return err
  84. }
  85. bodyJSON.Set("client_url", ruleUrl)
  86. if evalContext.ImagePublicUrl != "" {
  87. contexts := make([]interface{}, 1)
  88. imageJSON := simplejson.New()
  89. imageJSON.Set("type", "image")
  90. imageJSON.Set("src", evalContext.ImagePublicUrl)
  91. contexts[0] = imageJSON
  92. bodyJSON.Set("contexts", contexts)
  93. }
  94. body, _ := bodyJSON.MarshalJSON()
  95. cmd := &m.SendWebhookSync{
  96. Url: pagerdutyEventApiUrl,
  97. Body: string(body),
  98. HttpMethod: "POST",
  99. }
  100. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  101. this.log.Error("Failed to send notification to Pagerduty", "error", err, "body", string(body))
  102. return err
  103. }
  104. return nil
  105. }