slack.go 4.3 KB

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