notifier.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package alerting
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. type RootNotifier struct {
  12. NotifierBase
  13. log log.Logger
  14. }
  15. func NewRootNotifier() *RootNotifier {
  16. return &RootNotifier{
  17. log: log.New("alerting.notifier"),
  18. }
  19. }
  20. func (n *RootNotifier) Notify(context *AlertResultContext) {
  21. notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications)
  22. if err != nil {
  23. n.log.Error("Failed to read notifications", "error", err)
  24. return
  25. }
  26. for _, notifier := range notifiers {
  27. n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
  28. go notifier.Notify(context)
  29. }
  30. }
  31. func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64) ([]Notifier, error) {
  32. query := &m.GetAlertNotificationsQuery{OrgId: orgId, Ids: notificationIds}
  33. if err := bus.Dispatch(query); err != nil {
  34. return nil, err
  35. }
  36. var result []Notifier
  37. for _, notification := range query.Result {
  38. if not, err := NewNotificationFromDBModel(notification); err != nil {
  39. return nil, err
  40. } else {
  41. result = append(result, not)
  42. }
  43. }
  44. return result, nil
  45. }
  46. type NotifierBase struct {
  47. Name string
  48. Type string
  49. }
  50. func (n *NotifierBase) GetType() string {
  51. return n.Type
  52. }
  53. type EmailNotifier struct {
  54. NotifierBase
  55. Addresses []string
  56. log log.Logger
  57. }
  58. func (this *EmailNotifier) Notify(context *AlertResultContext) {
  59. this.log.Info("Sending alert notification to %v", this.Addresses)
  60. slugQuery := &m.GetDashboardSlugByIdQuery{Id: context.Rule.DashboardId}
  61. if err := bus.Dispatch(slugQuery); err != nil {
  62. this.log.Error("Failed to load dashboard", "error", err)
  63. return
  64. }
  65. dashboardSlug := slugQuery.Result
  66. cmd := &m.SendEmailCommand{
  67. Data: map[string]interface{}{
  68. "RuleName": context.Rule.Name,
  69. "Severity": context.Rule.Severity,
  70. "RuleLink": setting.ToAbsUrl("dashboard/db/" + dashboardSlug),
  71. },
  72. To: this.Addresses,
  73. Template: "alert_notification.html",
  74. }
  75. err := bus.Dispatch(cmd)
  76. if err != nil {
  77. this.log.Error("Failed tosend alert notification email", "error", err)
  78. }
  79. }
  80. // type WebhookNotifier struct {
  81. // Url string
  82. // User string
  83. // Password string
  84. // log log.Logger
  85. // }
  86. //
  87. // func (this *WebhookNotifier) Dispatch(context *AlertResultContext) {
  88. // this.log.Info("Sending webhook")
  89. //
  90. // bodyJSON := simplejson.New()
  91. // bodyJSON.Set("name", context.AlertJob.Rule.Name)
  92. // bodyJSON.Set("state", context.State)
  93. // bodyJSON.Set("trigged", context.TriggeredAlerts)
  94. //
  95. // body, _ := bodyJSON.MarshalJSON()
  96. //
  97. // cmd := &m.SendWebhook{
  98. // Url: this.Url,
  99. // User: this.User,
  100. // Password: this.Password,
  101. // Body: string(body),
  102. // }
  103. //
  104. // bus.Dispatch(cmd)
  105. // }
  106. func NewNotificationFromDBModel(model *m.AlertNotification) (Notifier, error) {
  107. if model.Type == "email" {
  108. addressesString := model.Settings.Get("addresses").MustString()
  109. if addressesString == "" {
  110. return nil, fmt.Errorf("Could not find addresses in settings")
  111. }
  112. return &EmailNotifier{
  113. NotifierBase: NotifierBase{
  114. Name: model.Name,
  115. Type: model.Type,
  116. },
  117. Addresses: strings.Split(addressesString, "\n"),
  118. log: log.New("alerting.notification.email"),
  119. }, nil
  120. }
  121. return nil, errors.New("Unsupported notification type")
  122. // url := settings.Get("url").MustString()
  123. // if url == "" {
  124. // return nil, fmt.Errorf("Could not find url propertie in settings")
  125. // }
  126. //
  127. // return &WebhookNotifier{
  128. // Url: url,
  129. // User: settings.Get("user").MustString(),
  130. // Password: settings.Get("password").MustString(),
  131. // log: log.New("alerting.notification.webhook"),
  132. // }, nil
  133. }