teams.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package notifiers
  2. import (
  3. "encoding/json"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/infra/log"
  6. "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting"
  8. )
  9. func init() {
  10. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  11. Type: "teams",
  12. Name: "Microsoft Teams",
  13. Description: "Sends notifications using Incoming Webhook connector to Microsoft Teams",
  14. Factory: NewTeamsNotifier,
  15. OptionsTemplate: `
  16. <h3 class="page-heading">Teams settings</h3>
  17. <div class="gf-form max-width-30">
  18. <span class="gf-form-label width-6">Url</span>
  19. <input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.url" placeholder="Teams incoming webhook url"></input>
  20. </div>
  21. `,
  22. })
  23. }
  24. // NewTeamsNotifier is the constructor for Teams notifier.
  25. func NewTeamsNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  26. url := model.Settings.Get("url").MustString()
  27. if url == "" {
  28. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  29. }
  30. return &TeamsNotifier{
  31. NotifierBase: NewNotifierBase(model),
  32. URL: url,
  33. log: log.New("alerting.notifier.teams"),
  34. }, nil
  35. }
  36. // TeamsNotifier is responsible for sending
  37. // alert notifications to Microsoft teams.
  38. type TeamsNotifier struct {
  39. NotifierBase
  40. URL string
  41. log log.Logger
  42. }
  43. // Notify send an alert notification to Microsoft teams.
  44. func (tn *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
  45. tn.log.Info("Executing teams notification", "ruleId", evalContext.Rule.ID, "notification", tn.Name)
  46. ruleURL, err := evalContext.GetRuleURL()
  47. if err != nil {
  48. tn.log.Error("Failed get rule link", "error", err)
  49. return err
  50. }
  51. fields := make([]map[string]interface{}, 0)
  52. fieldLimitCount := 4
  53. for index, evt := range evalContext.EvalMatches {
  54. fields = append(fields, map[string]interface{}{
  55. "name": evt.Metric,
  56. "value": evt.Value,
  57. })
  58. if index > fieldLimitCount {
  59. break
  60. }
  61. }
  62. if evalContext.Error != nil {
  63. fields = append(fields, map[string]interface{}{
  64. "name": "Error message",
  65. "value": evalContext.Error.Error(),
  66. })
  67. }
  68. message := ""
  69. if evalContext.Rule.State != models.AlertStateOK { //don't add message when going back to alert state ok.
  70. message = evalContext.Rule.Message
  71. }
  72. images := make([]map[string]interface{}, 0)
  73. if evalContext.ImagePublicURL != "" {
  74. images = append(images, map[string]interface{}{
  75. "image": evalContext.ImagePublicURL,
  76. })
  77. }
  78. body := map[string]interface{}{
  79. "@type": "MessageCard",
  80. "@context": "http://schema.org/extensions",
  81. // summary MUST not be empty or the webhook request fails
  82. // summary SHOULD contain some meaningful information, since it is used for mobile notifications
  83. "summary": evalContext.GetNotificationTitle(),
  84. "title": evalContext.GetNotificationTitle(),
  85. "themeColor": evalContext.GetStateModel().Color,
  86. "sections": []map[string]interface{}{
  87. {
  88. "title": "Details",
  89. "facts": fields,
  90. "images": images,
  91. "text": message,
  92. },
  93. },
  94. "potentialAction": []map[string]interface{}{
  95. {
  96. "@context": "http://schema.org",
  97. "@type": "OpenUri",
  98. "name": "View Rule",
  99. "targets": []map[string]interface{}{
  100. {
  101. "os": "default", "uri": ruleURL,
  102. },
  103. },
  104. },
  105. {
  106. "@context": "http://schema.org",
  107. "@type": "OpenUri",
  108. "name": "View Graph",
  109. "targets": []map[string]interface{}{
  110. {
  111. "os": "default", "uri": evalContext.ImagePublicURL,
  112. },
  113. },
  114. },
  115. },
  116. }
  117. data, _ := json.Marshal(&body)
  118. cmd := &models.SendWebhookSync{Url: tn.URL, Body: string(data)}
  119. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  120. tn.log.Error("Failed to send teams notification", "error", err, "webhook", tn.Name)
  121. return err
  122. }
  123. return nil
  124. }