sensu.go 4.2 KB

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