config_reader.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package notifiers
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/grafana/grafana/pkg/log"
  9. m "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/services/alerting"
  11. "gopkg.in/yaml.v2"
  12. )
  13. type configReader struct {
  14. log log.Logger
  15. }
  16. func (cr *configReader) readConfig(path string) ([]*notificationsAsConfig, error) {
  17. var notifications []*notificationsAsConfig
  18. cr.log.Debug("Looking for alert notification provisioning files", "path", path)
  19. files, err := ioutil.ReadDir(path)
  20. if err != nil {
  21. cr.log.Error("Can't read alert notification provisioning files from directory", "path", path)
  22. return notifications, nil
  23. }
  24. for _, file := range files {
  25. if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
  26. cr.log.Debug("Parsing alert notifications provisioning file", "path", path, "file.Name", file.Name())
  27. notifs, err := cr.parseNotificationConfig(path, file)
  28. if err != nil {
  29. return nil, err
  30. }
  31. if notifs != nil {
  32. notifications = append(notifications, notifs)
  33. }
  34. }
  35. }
  36. cr.log.Debug("Validating alert notifications")
  37. if err = validateRequiredField(notifications); err != nil {
  38. return nil, err
  39. }
  40. checkOrgIdAndOrgName(notifications)
  41. err = validateNotifications(notifications)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return notifications, nil
  46. }
  47. func (cr *configReader) parseNotificationConfig(path string, file os.FileInfo) (*notificationsAsConfig, error) {
  48. filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
  49. yamlFile, err := ioutil.ReadFile(filename)
  50. if err != nil {
  51. return nil, err
  52. }
  53. var cfg *notificationsAsConfig
  54. err = yaml.Unmarshal(yamlFile, &cfg)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return cfg.mapToNotificationFromConfig(), nil
  59. }
  60. func checkOrgIdAndOrgName(notifications []*notificationsAsConfig) {
  61. for i := range notifications {
  62. for _, notification := range notifications[i].Notifications {
  63. if notification.OrgId < 1 {
  64. if notification.OrgName == "" {
  65. notification.OrgId = 1
  66. } else {
  67. notification.OrgId = 0
  68. }
  69. }
  70. }
  71. for _, notification := range notifications[i].DeleteNotifications {
  72. if notification.OrgId < 1 {
  73. if notification.OrgName == "" {
  74. notification.OrgId = 1
  75. } else {
  76. notification.OrgId = 0
  77. }
  78. }
  79. }
  80. }
  81. }
  82. func validateRequiredField(notifications []*notificationsAsConfig) error {
  83. for i := range notifications {
  84. var errStrings []string
  85. for index, notification := range notifications[i].Notifications {
  86. if notification.Name == "" {
  87. errStrings = append(
  88. errStrings,
  89. fmt.Sprintf("Added alert notification item %d in configuration doesn't contain required field name", index+1),
  90. )
  91. }
  92. if notification.Uid == "" {
  93. errStrings = append(
  94. errStrings,
  95. fmt.Sprintf("Added alert notification item %d in configuration doesn't contain required field uid", index+1),
  96. )
  97. }
  98. }
  99. for index, notification := range notifications[i].DeleteNotifications {
  100. if notification.Name == "" {
  101. errStrings = append(
  102. errStrings,
  103. fmt.Sprintf("Deleted alert notification item %d in configuration doesn't contain required field name", index+1),
  104. )
  105. }
  106. if notification.Uid == "" {
  107. errStrings = append(
  108. errStrings,
  109. fmt.Sprintf("Deleted alert notification item %d in configuration doesn't contain required field uid", index+1),
  110. )
  111. }
  112. }
  113. if len(errStrings) != 0 {
  114. return fmt.Errorf(strings.Join(errStrings, "\n"))
  115. }
  116. }
  117. return nil
  118. }
  119. func validateNotifications(notifications []*notificationsAsConfig) error {
  120. for i := range notifications {
  121. if notifications[i].Notifications == nil {
  122. continue
  123. }
  124. for _, notification := range notifications[i].Notifications {
  125. _, err := alerting.InitNotifier(&m.AlertNotification{
  126. Name: notification.Name,
  127. Settings: notification.SettingsToJson(),
  128. Type: notification.Type,
  129. })
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. }
  135. return nil
  136. }