slack.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package notifiers
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/metrics"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. "github.com/grafana/grafana/pkg/setting"
  11. )
  12. func init() {
  13. alerting.RegisterNotifier("slack", NewSlackNotifier)
  14. }
  15. func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  16. url := model.Settings.Get("url").MustString()
  17. if url == "" {
  18. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  19. }
  20. recipient := model.Settings.Get("recipient").MustString()
  21. return &SlackNotifier{
  22. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  23. Url: url,
  24. Recipient: recipient,
  25. log: log.New("alerting.notifier.slack"),
  26. }, nil
  27. }
  28. type SlackNotifier struct {
  29. NotifierBase
  30. Url string
  31. Recipient string
  32. log log.Logger
  33. }
  34. func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
  35. this.log.Info("Executing slack notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  36. metrics.M_Alerting_Notification_Sent_Slack.Inc(1)
  37. ruleUrl, err := evalContext.GetRuleUrl()
  38. if err != nil {
  39. this.log.Error("Failed get rule link", "error", err)
  40. return err
  41. }
  42. fields := make([]map[string]interface{}, 0)
  43. fieldLimitCount := 4
  44. for index, evt := range evalContext.EvalMatches {
  45. fields = append(fields, map[string]interface{}{
  46. "title": evt.Metric,
  47. "value": evt.Value,
  48. "short": true,
  49. })
  50. if index > fieldLimitCount {
  51. break
  52. }
  53. }
  54. if evalContext.Error != nil {
  55. fields = append(fields, map[string]interface{}{
  56. "title": "Error message",
  57. "value": evalContext.Error.Error(),
  58. "short": false,
  59. })
  60. }
  61. message := ""
  62. if evalContext.Rule.State != m.AlertStateOK { //dont add message when going back to alert state ok.
  63. message = evalContext.Rule.Message
  64. }
  65. body := map[string]interface{}{
  66. "attachments": []map[string]interface{}{
  67. {
  68. "color": evalContext.GetStateModel().Color,
  69. "title": evalContext.GetNotificationTitle(),
  70. "title_link": ruleUrl,
  71. "text": message,
  72. "fields": fields,
  73. "image_url": evalContext.ImagePublicUrl,
  74. "footer": "Grafana v" + setting.BuildVersion,
  75. "footer_icon": "http://grafana.org/assets/img/fav32.png",
  76. "ts": time.Now().Unix(),
  77. },
  78. },
  79. "parse": "full", // to linkify urls, users and channels in alert message.
  80. }
  81. //recipient override
  82. if this.Recipient != "" {
  83. body["channel"] = this.Recipient
  84. }
  85. data, _ := json.Marshal(&body)
  86. cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
  87. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  88. this.log.Error("Failed to send slack notification", "error", err, "webhook", this.Name)
  89. }
  90. return nil
  91. }