Skip to content

Commit

Permalink
feat(agent): add /etc/telegraf/telegraf.d to default config locations
Browse files Browse the repository at this point in the history
This will add /etc/telegraf/telegraf.d to the list of default
configuration locations to load from. Currently, users need to specify
an empty /etc/telegraf/telegraf.conf file and specify they want to
include the configuraiton directory outside of the RPM and DEB.

A common use case is for Docker where users want to upload an entire
directory of configurations and run telegraf. Right now they have to add
a custom launch command, when in reality we should include this
directory by default.

This also means that the --config option can be omitted from the CLI
if a user is only using --config-directory.

fixes: #5656
fixes: #5571
  • Loading branch information
powersj committed Feb 3, 2023
1 parent 17c77df commit 30561c4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
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

0 comments on commit 30561c4

Please sign in to comment.