teams.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package notifiers
  2. import (
  3. "encoding/json"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/infra/log"
  6. m "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. func NewTeamsNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  25. url := model.Settings.Get("url").MustString()
  26. if url == "" {
  27. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  28. }
  29. return &TeamsNotifier{
  30. NotifierBase: NewNotifierBase(model),
  31. Url: url,
  32. log: log.New("alerting.notifier.teams"),
  33. }, nil
  34. }
  35. type TeamsNotifier struct {
  36. NotifierBase
  37. Url string
  38. log log.Logger
  39. }
  40. func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
  41. this.log.Info("Executing teams notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  42. ruleUrl, err := evalContext.GetRuleUrl()
  43. if err != nil {
  44. this.log.Error("Failed get rule link", "error", err)
  45. return err
  46. }
  47. fields := make([]map[string]interface{}, 0)
  48. fieldLimitCount := 4
  49. for index, evt := range evalContext.EvalMatches {
  50. fields = append(fields, map[string]interface{}{
  51. "name": evt.Metric,
  52. "value": evt.Value,
  53. })
  54. if index > fieldLimitCount {
  55. break
  56. }
  57. }
  58. if evalContext.Error != nil {
  59. fields = append(fields, map[string]interface{}{
  60. "name": "Error message",
  61. "value": evalContext.Error.Error(),
  62. })
  63. }
  64. message := ""
  65. if evalContext.Rule.State != m.AlertStateOK { //don't add message when going back to alert state ok.
  66. message = evalContext.Rule.Message
  67. }
  68. images := make([]map[string]interface{}, 0)
  69. if evalContext.ImagePublicUrl != "" {
  70. images = append(images, map[string]interface{}{
  71. "image": evalContext.ImagePublicUrl,
  72. })
  73. }
  74. body := map[string]interface{}{
  75. "@type": "MessageCard",
  76. "@context": "http://schema.org/extensions",
  77. // summary MUST not be empty or the webhook request fails
  78. // summary SHOULD contain some meaningful information, since it is used for mobile notifications
  79. "summary": evalContext.GetNotificationTitle(),
  80. "title": evalContext.GetNotificationTitle(),
  81. "themeColor": evalContext.GetStateModel().Color,
  82. "sections": []map[string]interface{}{
  83. {
  84. "title": "Details",
  85. "facts": fields,
  86. "images": images,
  87. "text": message,
  88. },
  89. },
  90. "potentialAction": []map[string]interface{}{
  91. {
  92. "@context": "http://schema.org",
  93. "@type": "OpenUri",
  94. "name": "View Rule",
  95. "targets": []map[string]interface{}{
  96. {
  97. "os": "default", "uri": ruleUrl,
  98. },
  99. },
  100. },
  101. {
  102. "@context": "http://schema.org",
  103. "@type": "OpenUri",
  104. "name": "View Graph",
  105. "targets": []map[string]interface{}{
  106. {
  107. "os": "default", "uri": evalContext.ImagePublicUrl,
  108. },
  109. },
  110. },
  111. },
  112. }
  113. data, _ := json.Marshal(&body)
  114. cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
  115. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  116. this.log.Error("Failed to send teams notification", "error", err, "webhook", this.Name)
  117. return err
  118. }
  119. return nil
  120. }