teams.go 3.4 KB

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