sensu.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "strconv"
  10. "strings"
  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">Username</span>
  26. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.username"></input>
  27. </div>
  28. <div class="gf-form">
  29. <span class="gf-form-label width-10">Password</span>
  30. <input type="text" class="gf-form-input max-width-14" ng-model="ctrl.model.settings.password"></input>
  31. </div>
  32. `,
  33. })
  34. }
  35. func NewSensuNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  36. url := model.Settings.Get("url").MustString()
  37. if url == "" {
  38. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  39. }
  40. return &SensuNotifier{
  41. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  42. Url: url,
  43. User: model.Settings.Get("username").MustString(),
  44. Password: model.Settings.Get("password").MustString(),
  45. log: log.New("alerting.notifier.sensu"),
  46. }, nil
  47. }
  48. type SensuNotifier struct {
  49. NotifierBase
  50. Url string
  51. User string
  52. Password string
  53. log log.Logger
  54. }
  55. func (this *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
  56. this.log.Info("Sending sensu result")
  57. metrics.M_Alerting_Notification_Sent_Sensu.Inc(1)
  58. bodyJSON := simplejson.New()
  59. bodyJSON.Set("ruleId", evalContext.Rule.Id)
  60. // Sensu alerts cannot have spaces in them
  61. bodyJSON.Set("name", strings.Replace(evalContext.Rule.Name, " ", "_", -1))
  62. // Sensu alerts require a command
  63. // We set it to the grafana ruleID
  64. bodyJSON.Set("source", "grafana_rule_"+strconv.FormatInt(evalContext.Rule.Id, 10))
  65. // Finally, sensu expects an output
  66. // We set it to a default output
  67. bodyJSON.Set("output", "Grafana Metric Condition Met")
  68. bodyJSON.Set("evalMatches", evalContext.EvalMatches)
  69. if evalContext.Rule.State == "alerting" {
  70. bodyJSON.Set("status", 2)
  71. } else if evalContext.Rule.State == "no_data" {
  72. bodyJSON.Set("status", 1)
  73. } else {
  74. bodyJSON.Set("status", 0)
  75. }
  76. ruleUrl, err := evalContext.GetRuleUrl()
  77. if err == nil {
  78. bodyJSON.Set("ruleUrl", ruleUrl)
  79. }
  80. if evalContext.ImagePublicUrl != "" {
  81. bodyJSON.Set("imageUrl", evalContext.ImagePublicUrl)
  82. }
  83. if evalContext.Rule.Message != "" {
  84. bodyJSON.Set("message", evalContext.Rule.Message)
  85. }
  86. body, _ := bodyJSON.MarshalJSON()
  87. cmd := &m.SendWebhookSync{
  88. Url: this.Url,
  89. User: this.User,
  90. Password: this.Password,
  91. Body: string(body),
  92. HttpMethod: "POST",
  93. }
  94. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  95. this.log.Error("Failed to send sensu event", "error", err, "sensu", this.Name)
  96. return err
  97. }
  98. return nil
  99. }