teams.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Incomming 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.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  31. Url: url,
  32. log: log.New("alerting.notifier.teams"),
  33. }, nil
  34. }
  35. type TeamsNotifier struct {
  36. NotifierBase
  37. Url string
  38. Recipient string
  39. Mention string
  40. log log.Logger
  41. }
  42. func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
  43. this.log.Info("Executing teams notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  44. ruleUrl, err := evalContext.GetRuleUrl()
  45. if err != nil {
  46. this.log.Error("Failed get rule link", "error", err)
  47. return err
  48. }
  49. fields := make([]map[string]interface{}, 0)
  50. fieldLimitCount := 4
  51. for index, evt := range evalContext.EvalMatches {
  52. fields = append(fields, map[string]interface{}{
  53. "name": evt.Metric,
  54. "value": evt.Value,
  55. })
  56. if index > fieldLimitCount {
  57. break
  58. }
  59. }
  60. if evalContext.Error != nil {
  61. fields = append(fields, map[string]interface{}{
  62. "name": "Error message",
  63. "value": evalContext.Error.Error(),
  64. })
  65. }
  66. message := this.Mention
  67. if evalContext.Rule.State != m.AlertStateOK { //dont add message when going back to alert state ok.
  68. message += " " + evalContext.Rule.Message
  69. } else {
  70. message += " " // summary must not be empty
  71. }
  72. body := map[string]interface{}{
  73. "@type": "MessageCard",
  74. "@context": "http://schema.org/extensions",
  75. "summary": message,
  76. "title": evalContext.GetNotificationTitle(),
  77. "themeColor": evalContext.GetStateModel().Color,
  78. "sections": []map[string]interface{}{
  79. {
  80. "title": "Details",
  81. "facts": fields,
  82. "images": []map[string]interface{}{
  83. {
  84. "image": evalContext.ImagePublicUrl,
  85. },
  86. },
  87. "text": message,
  88. "potentialAction": []map[string]interface{}{
  89. {
  90. "@context": "http://schema.org",
  91. "@type": "ViewAction",
  92. "name": "View Rule",
  93. "target": []string{
  94. ruleUrl,
  95. },
  96. },
  97. },
  98. },
  99. },
  100. }
  101. data, _ := json.Marshal(&body)
  102. cmd := &m.SendWebhookSync{Url: this.Url, Body: string(data)}
  103. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  104. this.log.Error("Failed to send teams notification", "error", err, "webhook", this.Name)
  105. return err
  106. }
  107. return nil
  108. }