pagerduty.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package notifiers
  2. import (
  3. "strconv"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/metrics"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. )
  11. func init() {
  12. alerting.RegisterNotifier("pagerduty", NewPagerdutyNotifier)
  13. }
  14. var (
  15. pagerdutyEventApiUrl string = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
  16. )
  17. func NewPagerdutyNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  18. autoResolve := model.Settings.Get("autoResolve").MustBool(true)
  19. key := model.Settings.Get("integrationKey").MustString()
  20. if key == "" {
  21. return nil, alerting.ValidationError{Reason: "Could not find integration key property in settings"}
  22. }
  23. return &PagerdutyNotifier{
  24. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  25. Key: key,
  26. AutoResolve: autoResolve,
  27. log: log.New("alerting.notifier.pagerduty"),
  28. }, nil
  29. }
  30. type PagerdutyNotifier struct {
  31. NotifierBase
  32. Key string
  33. AutoResolve bool
  34. log log.Logger
  35. }
  36. func (this *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
  37. metrics.M_Alerting_Notification_Sent_PagerDuty.Inc(1)
  38. if evalContext.Rule.State == m.AlertStateOK && !this.AutoResolve {
  39. this.log.Info("Not sending a trigger to Pagerduty", "state", evalContext.Rule.State, "auto resolve", this.AutoResolve)
  40. return nil
  41. }
  42. eventType := "trigger"
  43. if evalContext.Rule.State == m.AlertStateOK {
  44. eventType = "resolve"
  45. }
  46. this.log.Info("Notifying Pagerduty", "event_type", eventType)
  47. bodyJSON := simplejson.New()
  48. bodyJSON.Set("service_key", this.Key)
  49. bodyJSON.Set("description", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
  50. bodyJSON.Set("client", "Grafana")
  51. bodyJSON.Set("event_type", eventType)
  52. bodyJSON.Set("incident_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  53. ruleUrl, err := evalContext.GetRuleUrl()
  54. if err != nil {
  55. this.log.Error("Failed get rule link", "error", err)
  56. return err
  57. }
  58. bodyJSON.Set("client_url", ruleUrl)
  59. if evalContext.ImagePublicUrl != "" {
  60. contexts := make([]interface{}, 1)
  61. imageJSON := simplejson.New()
  62. imageJSON.Set("type", "image")
  63. imageJSON.Set("src", evalContext.ImagePublicUrl)
  64. contexts[0] = imageJSON
  65. bodyJSON.Set("contexts", contexts)
  66. }
  67. body, _ := bodyJSON.MarshalJSON()
  68. cmd := &m.SendWebhookSync{
  69. Url: pagerdutyEventApiUrl,
  70. Body: string(body),
  71. HttpMethod: "POST",
  72. }
  73. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  74. this.log.Error("Failed to send notification to Pagerduty", "error", err, "body", string(body))
  75. return err
  76. }
  77. return nil
  78. }