sensu.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package notifiers
  2. import (
  3. "strconv"
  4. "strings"
  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: "sensu",
  15. Name: "Sensu",
  16. Description: "Sends HTTP POST request to a Sensu API",
  17. Factory: NewSensuNotifier,
  18. OptionsTemplate: `
  19. <h3 class="page-heading">Sensu settings</h3>
  20. <div class="gf-form">
  21. <span class="gf-form-label width-10">Url</span>
  22. <input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url" placeholder="http://sensu-api.local:4567/results"></input>
  23. </div>
  24. <div class="gf-form">
  25. <span class="gf-form-label width-10">Source</span>
  26. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.source" bs-tooltip="'If empty rule id will be used'" data-placement="right"></input>
  27. </div>
  28. <div class="gf-form">
  29. <span class="gf-form-label width-10">Handler</span>
  30. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.handler" placeholder="default"></input>
  31. </div>
  32. <div class="gf-form">
  33. <span class="gf-form-label width-10">Username</span>
  34. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.username"></input>
  35. </div>
  36. <div class="gf-form">
  37. <span class="gf-form-label width-10">Password</span>
  38. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.password"></input>
  39. </div>
  40. `,
  41. })
  42. }
  43. func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  44. url := model.Settings.Get("url").MustString()
  45. if url == "" {
  46. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  47. }
  48. return &SensuNotifier{
  49. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  50. Url: url,
  51. User: model.Settings.Get("username").MustString(),
  52. Source: model.Settings.Get("source").MustString(),
  53. Password: model.Settings.Get("password").MustString(),
  54. Handler: model.Settings.Get("handler").MustString(),
  55. log: log.New("alerting.notifier.sensu"),
  56. }, nil
  57. }
  58. type SensuNotifier struct {
  59. NotifierBase
  60. Url string
  61. Source string
  62. User string
  63. Password string
  64. Handler string
  65. log log.Logger
  66. }
  67. func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
  68. this.log.Info("Sending sensu result")
  69. metrics.M_Alerting_Notification_Sent_Sensu.Inc(1)
  70. bodyJSON := simplejson.New()
  71. bodyJSON.Set("ruleId", evalContext.Rule.Id)
  72. // Sensu alerts cannot have spaces in them
  73. bodyJSON.Set("name", strings.Replace(evalContext.Rule.Name, " ", "_", -1))
  74. // Sensu alerts require a source. We set it to the user-specified value (optional),
  75. // else we fallback and use the grafana ruleID.
  76. if this.Source != "" {
  77. bodyJSON.Set("source", this.Source)
  78. } else {
  79. bodyJSON.Set("source", "grafana_rule_"+strconv.FormatInt(evalContext.Rule.Id, 10))
  80. }
  81. // Finally, sensu expects an output
  82. // We set it to a default output
  83. bodyJSON.Set("output", "Grafana Metric Condition Met")
  84. bodyJSON.Set("evalMatches", evalContext.EvalMatches)
  85. if evalContext.Rule.State == "alerting" {
  86. bodyJSON.Set("status", 2)
  87. } else if evalContext.Rule.State == "no_data" {
  88. bodyJSON.Set("status", 1)
  89. } else {
  90. bodyJSON.Set("status", 0)
  91. }
  92. if this.Handler != "" {
  93. bodyJSON.Set("handler", this.Handler)
  94. }
  95. ruleUrl, err := evalContext.GetRuleUrl()
  96. if err == nil {
  97. bodyJSON.Set("ruleUrl", ruleUrl)
  98. }
  99. if evalContext.ImagePublicUrl != "" {
  100. bodyJSON.Set("imageUrl", evalContext.ImagePublicUrl)
  101. }
  102. if evalContext.Rule.Message != "" {
  103. bodyJSON.Set("message", evalContext.Rule.Message)
  104. }
  105. body, _ := bodyJSON.MarshalJSON()
  106. cmd := &m.SendWebhookSync{
  107. Url: this.Url,
  108. User: this.User,
  109. Password: this.Password,
  110. Body: string(body),
  111. HttpMethod: "POST",
  112. }
  113. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  114. this.log.Error("Failed to send sensu event", "error", err, "sensu", this.Name)
  115. return err
  116. }
  117. return nil
  118. }