teams.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package notifiers
  2. import (
  3. "encoding/json"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/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 := ""
  69. if evalContext.ImagePublicUrl != "" {
  70. images = []map[string]interface{}{
  71. {
  72. "image": evalContext.ImagePublicUrl,
  73. },
  74. }
  75. }
  76. body := map[string]interface{}{
  77. "@type": "MessageCard",
  78. "@context": "http://schema.org/extensions",
  79. // summary MUST not be empty or the webhook request fails
  80. // summary SHOULD contain some meaningful information, since it is used for mobile notifications
  81. "summary": evalContext.GetNotificationTitle(),
  82. "title": evalContext.GetNotificationTitle(),
  83. "themeColor": evalContext.GetStateModel().Color,
  84. "sections": []map[string]interface{}{
  85. {
  86. "title": "Details",
  87. "facts": fields,
  88. "images": images,
  89. "text": message,
  90. },
  91. },
  92. "potentialAction": []map[string]interface{}{
  93. {
  94. "@context": "http://schema.org",
  95. "@type": "OpenUri",
  96. "name": "View Rule",
  97. "targets": []map[string]interface{}{
  98. {
  99. "os": "default", "uri": ruleUrl,
  100. },
  101. },
  102. },
  103. {
  104. "@context": "http://schema.org",
  105. "@type": "OpenUri",
  106. "name": "View Graph",
  107. "targets": []map[string]interface{}{
  108. {
  109. "os": "default", "uri": evalContext.ImagePublicUrl,
  110. },
  111. },
  112. },
  113. },
  114. }
  115. data, _ := json.Marshal(&body)
  116. cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
  117. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  118. this.log.Error("Failed to send teams notification", "error", err, "webhook", this.Name)
  119. return err
  120. }
  121. return nil
  122. }