feat(agent): Add /etc/telegraf/telegraf.d to default config locations (#12608)
This commit is contained in:
parent
0ea50fa3b5
commit
ae2eb096cd
|
|
@ -208,13 +208,8 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) {
|
|||
c.SecretStoreFilters = t.secretstoreFilters
|
||||
|
||||
var configFiles []string
|
||||
// providing no "config" flag should load default config
|
||||
if len(t.config) == 0 {
|
||||
configFiles = append(configFiles, "")
|
||||
} else {
|
||||
configFiles = append(configFiles, t.config...)
|
||||
}
|
||||
|
||||
configFiles = append(configFiles, t.config...)
|
||||
for _, fConfigDirectory := range t.configDir {
|
||||
files, err := config.WalkDirectory(fConfigDirectory)
|
||||
if err != nil {
|
||||
|
|
@ -223,6 +218,12 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) {
|
|||
configFiles = append(configFiles, files...)
|
||||
}
|
||||
|
||||
// providing no "config" or "config-directory" flag(s) should load default
|
||||
// configuration files
|
||||
if len(configFiles) == 0 {
|
||||
configFiles = append(configFiles, "")
|
||||
}
|
||||
|
||||
t.configFiles = configFiles
|
||||
if err := c.LoadAll(configFiles...); err != nil {
|
||||
return c, err
|
||||
|
|
|
|||
|
|
@ -362,32 +362,57 @@ func WalkDirectory(path string) ([]string, error) {
|
|||
// Try to find a default config file at these locations (in order):
|
||||
// 1. $TELEGRAF_CONFIG_PATH
|
||||
// 2. $HOME/.telegraf/telegraf.conf
|
||||
// 3. /etc/telegraf/telegraf.conf
|
||||
func getDefaultConfigPath() (string, error) {
|
||||
// 3. /etc/telegraf/telegraf.conf and /etc/telegraf/telegraf.d/*.conf
|
||||
func getDefaultConfigPath() ([]string, error) {
|
||||
envfile := os.Getenv("TELEGRAF_CONFIG_PATH")
|
||||
homefile := os.ExpandEnv("${HOME}/.telegraf/telegraf.conf")
|
||||
etcfile := "/etc/telegraf/telegraf.conf"
|
||||
etcfolder := "/etc/telegraf/telegraf.conf.d"
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
programFiles := os.Getenv("ProgramFiles")
|
||||
if programFiles == "" { // Should never happen
|
||||
programFiles = `C:\Program Files`
|
||||
}
|
||||
etcfile = programFiles + `\Telegraf\telegraf.conf`
|
||||
etcfolder = programFiles + `\Telegraf\telegraf.conf.d\`
|
||||
}
|
||||
for _, path := range []string{envfile, homefile, etcfile} {
|
||||
|
||||
for _, path := range []string{envfile, homefile} {
|
||||
if isURL(path) {
|
||||
log.Printf("I! Using config url: %s", path)
|
||||
return path, nil
|
||||
return []string{path}, nil
|
||||
}
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
log.Printf("I! Using config file: %s", path)
|
||||
return path, nil
|
||||
return []string{path}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// At this point we need to check if the files under /etc/telegraf are
|
||||
// populated and return them all.
|
||||
confFiles := []string{}
|
||||
if _, err := os.Stat(etcfile); err == nil {
|
||||
log.Printf("I! Using config file: %s", etcfile)
|
||||
confFiles = append(confFiles, etcfile)
|
||||
}
|
||||
if _, err := os.Stat(etcfolder); err == nil {
|
||||
files, err := WalkDirectory(etcfolder)
|
||||
if err != nil {
|
||||
log.Printf("W! unable walk '%s': %s", etcfolder, err)
|
||||
}
|
||||
for _, file := range files {
|
||||
log.Printf("I! Using config file: %s", file)
|
||||
}
|
||||
confFiles = append(confFiles, files...)
|
||||
}
|
||||
if len(confFiles) > 0 {
|
||||
return confFiles, nil
|
||||
}
|
||||
|
||||
// if we got here, we didn't find a file in a default location
|
||||
return "", fmt.Errorf("no config file specified, and could not find one"+
|
||||
" in $TELEGRAF_CONFIG_PATH, %s, or %s", homefile, etcfile)
|
||||
return nil, fmt.Errorf("no config file specified, and could not find one"+
|
||||
" in $TELEGRAF_CONFIG_PATH, %s, %s, or %s/*.conf", homefile, etcfile, etcfolder)
|
||||
}
|
||||
|
||||
// isURL checks if string is valid url
|
||||
|
|
@ -396,22 +421,30 @@ func isURL(str string) bool {
|
|||
return err == nil && u.Scheme != "" && u.Host != ""
|
||||
}
|
||||
|
||||
// LoadConfig loads the given config file and applies it to c
|
||||
// LoadConfig loads the given config files and applies it to c
|
||||
func (c *Config) LoadConfig(path string) error {
|
||||
var err error
|
||||
paths := []string{}
|
||||
|
||||
if path == "" {
|
||||
if path, err = getDefaultConfigPath(); err != nil {
|
||||
if paths, err = getDefaultConfigPath(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
data, err := LoadConfigFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error loading config file %s: %w", path, err)
|
||||
} else {
|
||||
paths = append(paths, path)
|
||||
}
|
||||
|
||||
if err = c.LoadConfigData(data); err != nil {
|
||||
return fmt.Errorf("error loading config file %s: %w", path, err)
|
||||
for _, path := range paths {
|
||||
data, err := LoadConfigFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error loading config file %s: %w", path, err)
|
||||
}
|
||||
|
||||
if err = c.LoadConfigData(data); err != nil {
|
||||
return fmt.Errorf("error loading config file %s: %w", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -484,7 +484,7 @@ func TestConfig_getDefaultConfigPathFromEnvURL(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
configPath, err := getDefaultConfigPath()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ts.URL, configPath)
|
||||
require.Equal(t, []string{ts.URL}, configPath)
|
||||
err = c.LoadConfig("")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue