Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(agent): add /etc/telegraf/telegraf.d to default config locations #12608

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
63 changes: 48 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down