config_reader.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package alert_notifications
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. "gopkg.in/yaml.v2"
  11. )
  12. type configReader struct {
  13. log log.Logger
  14. }
  15. func (cr *configReader) readConfig(path string) ([]*notificationsAsConfig, error) {
  16. var notifications []*notificationsAsConfig
  17. cr.log.Debug("Looking for alert notification provisioning files", "path", path)
  18. files, err := ioutil.ReadDir(path)
  19. if err != nil {
  20. cr.log.Error("Can't read alert notification provisioning files from directory", "path", path)
  21. return notifications, nil
  22. }
  23. for _, file := range files {
  24. if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
  25. cr.log.Debug("Parsing alert notifications provisioning file", "path", path, "file.Name", file.Name())
  26. notifs, err := cr.parseNotificationConfig(path, file)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if notifs != nil {
  31. notifications = append(notifications, notifs)
  32. }
  33. }
  34. }
  35. cr.log.Debug("Validating alert notifications")
  36. validateOrgIdAndSet(notifications)
  37. err = validateNotifications(notifications)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return notifications, nil
  42. }
  43. func (cr *configReader) parseNotificationConfig(path string, file os.FileInfo) (*notificationsAsConfig, error) {
  44. filename, _ := filepath.Abs(filepath.Join(path, file.Name()))
  45. yamlFile, err := ioutil.ReadFile(filename)
  46. if err != nil {
  47. return nil, err
  48. }
  49. var cfg *notificationsAsConfig
  50. err = yaml.Unmarshal(yamlFile, &cfg)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return cfg.mapToNotificationFromConfig(), nil
  55. }
  56. func validateOrgIdAndSet(notifications []*notificationsAsConfig) {
  57. for i := range notifications {
  58. for _, notification := range notifications[i].Notifications {
  59. if notification.OrgId < 1 {
  60. if notification.OrgName == "" {
  61. notification.OrgId = 1
  62. } else {
  63. notification.OrgId = 0
  64. }
  65. }
  66. }
  67. for _, notification := range notifications[i].DeleteNotifications {
  68. if notification.OrgId < 1 {
  69. if notification.OrgName == "" {
  70. notification.OrgId = 1
  71. } else {
  72. notification.OrgId = 0
  73. }
  74. }
  75. }
  76. }
  77. }
  78. func validateNotifications(notifications []*notificationsAsConfig) error {
  79. notifierTypes := alerting.GetNotifiers()
  80. for i := range notifications {
  81. if notifications[i].Notifications == nil {
  82. continue
  83. }
  84. for _, notification := range notifications[i].Notifications {
  85. foundNotifier := false
  86. for _, notifier := range notifierTypes {
  87. if notifier.Type == notification.Type {
  88. foundNotifier = true
  89. _, notifierError := notifier.Factory(&m.AlertNotification{
  90. Name: notification.Name,
  91. Settings: notification.SettingsToJson(),
  92. Type: notification.Type,
  93. })
  94. if notifierError != nil {
  95. return notifierError
  96. }
  97. break
  98. }
  99. }
  100. if !foundNotifier {
  101. return ErrInvalidNotifierType
  102. }
  103. }
  104. }
  105. return nil
  106. }