slack.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(&alerting.NotifierPlugin{
  14. Type: "slack",
  15. Name: "Slack",
  16. Description: "Sends notifications using Grafana server configured STMP settings",
  17. Factory: NewSlackNotifier,
  18. OptionsTemplate: `
  19. <h3 class="page-heading">Slack settings</h3>
  20. <div class="gf-form max-width-30">
  21. <span class="gf-form-label width-6">Url</span>
  22. <input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.url" placeholder="Slack incoming webhook url"></input>
  23. </div>
  24. <div class="gf-form max-width-30">
  25. <span class="gf-form-label width-6">Recipient</span>
  26. <input type="text"
  27. class="gf-form-input max-width-30"
  28. ng-model="ctrl.model.settings.recipient"
  29. data-placement="right">
  30. </input>
  31. <info-popover mode="right-absolute">
  32. Override default channel or user, use #channel-name or @username
  33. </info-popover>
  34. </div>
  35. <div class="gf-form max-width-30">
  36. <span class="gf-form-label width-6">Mention</span>
  37. <input type="text"
  38. class="gf-form-input max-width-30"
  39. ng-model="ctrl.model.settings.mention"
  40. data-placement="right">
  41. </input>
  42. <info-popover mode="right-absolute">
  43. Mention a user or a group using @ when notifying in a channel
  44. </info-popover>
  45. </div>
  46. `,
  47. })
  48. }
  49. func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  50. url := model.Settings.Get("url").MustString()
  51. if url == "" {
  52. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  53. }
  54. recipient := model.Settings.Get("recipient").MustString()
  55. mention := model.Settings.Get("mention").MustString()
  56. return &SlackNotifier{
  57. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  58. Url: url,
  59. Recipient: recipient,
  60. Mention: mention,
  61. log: log.New("alerting.notifier.slack"),
  62. }, nil
  63. }
  64. type SlackNotifier struct {
  65. NotifierBase
  66. Url string
  67. Recipient string
  68. Mention string
  69. log log.Logger
  70. }
  71. func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
  72. this.log.Info("Executing slack notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  73. metrics.M_Alerting_Notification_Sent_Slack.Inc(1)
  74. ruleUrl, err := evalContext.GetRuleUrl()
  75. if err != nil {
  76. this.log.Error("Failed get rule link", "error", err)
  77. return err
  78. }
  79. fields := make([]map[string]interface{}, 0)
  80. fieldLimitCount := 4
  81. for index, evt := range evalContext.EvalMatches {
  82. fields = append(fields, map[string]interface{}{
  83. "title": evt.Metric,
  84. "value": evt.Value,
  85. "short": true,
  86. })
  87. if index > fieldLimitCount {
  88. break
  89. }
  90. }
  91. if evalContext.Error != nil {
  92. fields = append(fields, map[string]interface{}{
  93. "title": "Error message",
  94. "value": evalContext.Error.Error(),
  95. "short": false,
  96. })
  97. }
  98. message := this.Mention
  99. if evalContext.Rule.State != m.AlertStateOK { //dont add message when going back to alert state ok.
  100. message += " " + evalContext.Rule.Message
  101. }
  102. body := map[string]interface{}{
  103. "attachments": []map[string]interface{}{
  104. {
  105. "fallback": evalContext.GetNotificationTitle(),
  106. "color": evalContext.GetStateModel().Color,
  107. "title": evalContext.GetNotificationTitle(),
  108. "title_link": ruleUrl,
  109. "text": message,
  110. "fields": fields,
  111. "image_url": evalContext.ImagePublicUrl,
  112. "footer": "Grafana v" + setting.BuildVersion,
  113. "footer_icon": "https://grafana.com/assets/img/fav32.png",
  114. "ts": time.Now().Unix(),
  115. },
  116. },
  117. "parse": "full", // to linkify urls, users and channels in alert message.
  118. }
  119. //recipient override
  120. if this.Recipient != "" {
  121. body["channel"] = this.Recipient
  122. }
  123. data, _ := json.Marshal(&body)
  124. cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
  125. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  126. this.log.Error("Failed to send slack notification", "error", err, "webhook", this.Name)
  127. return err
  128. }
  129. return nil
  130. }