webhook.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package notifiers
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/log"
  6. "github.com/grafana/grafana/pkg/metrics"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. )
  10. func init() {
  11. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  12. Type: "webhook",
  13. Name: "webhook",
  14. Description: "Sends HTTP POST request to a URL",
  15. Factory: NewWebHookNotifier,
  16. OptionsTemplate: `
  17. <h3 class="page-heading">Webhook settings</h3>
  18. <div class="gf-form">
  19. <span class="gf-form-label width-10">Url</span>
  20. <input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url"></input>
  21. </div>
  22. <div class="gf-form">
  23. <span class="gf-form-label width-10">Http Method</span>
  24. <div class="gf-form-select-wrapper width-14">
  25. <select class="gf-form-input" ng-model="ctrl.model.settings.httpMethod" ng-options="t for t in ['POST', 'PUT']">
  26. </select>
  27. </div>
  28. </div>
  29. <div class="gf-form">
  30. <span class="gf-form-label width-10">Username</span>
  31. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.username"></input>
  32. </div>
  33. <div class="gf-form">
  34. <span class="gf-form-label width-10">Password</span>
  35. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.password"></input>
  36. </div>
  37. `,
  38. })
  39. }
  40. func NewWebHookNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  41. url := model.Settings.Get("url").MustString()
  42. if url == "" {
  43. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  44. }
  45. return &WebhookNotifier{
  46. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  47. Url: url,
  48. User: model.Settings.Get("username").MustString(),
  49. Password: model.Settings.Get("password").MustString(),
  50. HttpMethod: model.Settings.Get("httpMethod").MustString("POST"),
  51. log: log.New("alerting.notifier.webhook"),
  52. }, nil
  53. }
  54. type WebhookNotifier struct {
  55. NotifierBase
  56. Url string
  57. User string
  58. Password string
  59. HttpMethod string
  60. log log.Logger
  61. }
  62. func (this *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
  63. this.log.Info("Sending webhook")
  64. metrics.M_Alerting_Notification_Sent_Webhook.Inc(1)
  65. bodyJSON := simplejson.New()
  66. bodyJSON.Set("title", evalContext.GetNotificationTitle())
  67. bodyJSON.Set("ruleId", evalContext.Rule.Id)
  68. bodyJSON.Set("ruleName", evalContext.Rule.Name)
  69. bodyJSON.Set("state", evalContext.Rule.State)
  70. bodyJSON.Set("evalMatches", evalContext.EvalMatches)
  71. ruleUrl, err := evalContext.GetRuleUrl()
  72. if err == nil {
  73. bodyJSON.Set("ruleUrl", ruleUrl)
  74. }
  75. if evalContext.ImagePublicUrl != "" {
  76. bodyJSON.Set("imageUrl", evalContext.ImagePublicUrl)
  77. }
  78. if evalContext.Rule.Message != "" {
  79. bodyJSON.Set("message", evalContext.Rule.Message)
  80. }
  81. body, _ := bodyJSON.MarshalJSON()
  82. cmd := &m.SendWebhookSync{
  83. Url: this.Url,
  84. User: this.User,
  85. Password: this.Password,
  86. Body: string(body),
  87. HttpMethod: this.HttpMethod,
  88. }
  89. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  90. this.log.Error("Failed to send webhook", "error", err, "webhook", this.Name)
  91. return err
  92. }
  93. return nil
  94. }