forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
130 lines (101 loc) · 3.13 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package config
import (
"log"
"os"
"path/filepath"
"time"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/paths"
)
// Defaults for config variables which are not set
const (
DefaultInputType = "log"
)
type Config struct {
Prospectors []*common.Config `config:"prospectors"`
SpoolSize uint64 `config:"spool_size" validate:"min=1"`
PublishAsync bool `config:"publish_async"`
IdleTimeout time.Duration `config:"idle_timeout" validate:"nonzero,min=0s"`
RegistryFile string `config:"registry_file"`
ConfigDir string `config:"config_dir"`
ShutdownTimeout time.Duration `config:"shutdown_timeout"`
Modules []*common.Config `config:"modules"`
ProspectorReload *common.Config `config:"config.prospectors"`
}
var (
DefaultConfig = Config{
RegistryFile: "registry",
SpoolSize: 2048,
IdleTimeout: 5 * time.Second,
ShutdownTimeout: 0,
}
)
const (
LogInputType = "log"
StdinInputType = "stdin"
)
// List of valid input types
var ValidInputType = map[string]struct{}{
StdinInputType: {},
LogInputType: {},
}
// getConfigFiles returns list of config files.
// In case path is a file, it will be directly returned.
// In case it is a directory, it will fetch all .yml files inside this directory
func getConfigFiles(path string) (configFiles []string, err error) {
// Check if path is valid file or dir
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
// Create empty slice for config file list
configFiles = make([]string, 0)
if stat.IsDir() {
files, err := filepath.Glob(path + "/*.yml")
if err != nil {
return nil, err
}
configFiles = append(configFiles, files...)
} else {
// Only 1 config file
configFiles = append(configFiles, path)
}
return configFiles, nil
}
// mergeConfigFiles reads in all config files given by list configFiles and merges them into config
func mergeConfigFiles(configFiles []string, config *Config) error {
for _, file := range configFiles {
logp.Info("Additional configs loaded from: %s", file)
tmpConfig := struct {
Filebeat Config
}{}
cfgfile.Read(&tmpConfig, file)
config.Prospectors = append(config.Prospectors, tmpConfig.Filebeat.Prospectors...)
}
return nil
}
// Fetches and merges all config files given by configDir. All are put into one config object
func (config *Config) FetchConfigs() error {
configDir := config.ConfigDir
// If option not set, do nothing
if configDir == "" {
return nil
}
// If configDir is relative, consider it relative to the config path
configDir = paths.Resolve(paths.Config, configDir)
// Check if optional configDir is set to fetch additional config files
logp.Info("Additional config files are fetched from: %s", configDir)
configFiles, err := getConfigFiles(configDir)
if err != nil {
log.Fatal("Could not use config_dir of: ", configDir, err)
return err
}
err = mergeConfigFiles(configFiles, config)
if err != nil {
log.Fatal("Error merging config files: ", err)
return err
}
return nil
}