pagerduty.go 3.6 KB

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