sensu.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "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. // NewSensuNotifier is the constructor for the Sensu Notifier.
  43. func NewSensuNotifier(model *models.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),
  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. // SensuNotifier is responsible for sending
  59. // alert notifications to Sensu.
  60. type SensuNotifier struct {
  61. NotifierBase
  62. URL string
  63. Source string
  64. User string
  65. Password string
  66. Handler string
  67. log log.Logger
  68. }
  69. // Notify send alert notification to Sensu
  70. func (sn *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
  71. sn.log.Info("Sending sensu result")
  72. bodyJSON := simplejson.New()
  73. bodyJSON.Set("ruleId", evalContext.Rule.ID)
  74. // Sensu alerts cannot have spaces in them
  75. bodyJSON.Set("name", strings.Replace(evalContext.Rule.Name, " ", "_", -1))
  76. // Sensu alerts require a source. We set it to the user-specified value (optional),
  77. // else we fallback and use the grafana ruleID.
  78. if sn.Source != "" {
  79. bodyJSON.Set("source", sn.Source)
  80. } else {
  81. bodyJSON.Set("source", "grafana_rule_"+strconv.FormatInt(evalContext.Rule.ID, 10))
  82. }
  83. // Finally, sensu expects an output
  84. // We set it to a default output
  85. bodyJSON.Set("output", "Grafana Metric Condition Met")
  86. bodyJSON.Set("evalMatches", evalContext.EvalMatches)
  87. if evalContext.Rule.State == "alerting" {
  88. bodyJSON.Set("status", 2)
  89. } else if evalContext.Rule.State == "no_data" {
  90. bodyJSON.Set("status", 1)
  91. } else {
  92. bodyJSON.Set("status", 0)
  93. }
  94. if sn.Handler != "" {
  95. bodyJSON.Set("handler", sn.Handler)
  96. }
  97. ruleURL, err := evalContext.GetRuleURL()
  98. if err == nil {
  99. bodyJSON.Set("ruleUrl", ruleURL)
  100. }
  101. if evalContext.ImagePublicURL != "" {
  102. bodyJSON.Set("imageUrl", evalContext.ImagePublicURL)
  103. }
  104. if evalContext.Rule.Message != "" {
  105. bodyJSON.Set("output", evalContext.Rule.Message)
  106. }
  107. body, _ := bodyJSON.MarshalJSON()
  108. cmd := &models.SendWebhookSync{
  109. Url: sn.URL,
  110. User: sn.User,
  111. Password: sn.Password,
  112. Body: string(body),
  113. HttpMethod: "POST",
  114. }
  115. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  116. sn.log.Error("Failed to send sensu event", "error", err, "sensu", sn.Name)
  117. return err
  118. }
  119. return nil
  120. }