예제 #1
0
func TestAgent_LoadPlugin(t *testing.T) {
	c := config.NewConfig()
	c.InputFilters = []string{"mysql"}
	err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ := NewAgent(c)
	assert.Equal(t, 1, len(a.Config.Inputs))

	c = config.NewConfig()
	c.InputFilters = []string{"foo"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 0, len(a.Config.Inputs))

	c = config.NewConfig()
	c.InputFilters = []string{"mysql", "foo"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 1, len(a.Config.Inputs))

	c = config.NewConfig()
	c.InputFilters = []string{"mysql", "redis"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 2, len(a.Config.Inputs))

	c = config.NewConfig()
	c.InputFilters = []string{"mysql", "foo", "redis", "bar"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 2, len(a.Config.Inputs))
}
예제 #2
0
func TestAgent_LoadOutput(t *testing.T) {
	c := config.NewConfig()
	c.OutputFilters = []string{"influxdb"}
	err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ := NewAgent(c)
	assert.Equal(t, 2, len(a.Config.Outputs))

	c = config.NewConfig()
	c.OutputFilters = []string{"kafka"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 1, len(a.Config.Outputs))

	c = config.NewConfig()
	c.OutputFilters = []string{}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 3, len(a.Config.Outputs))

	c = config.NewConfig()
	c.OutputFilters = []string{"foo"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 0, len(a.Config.Outputs))

	c = config.NewConfig()
	c.OutputFilters = []string{"influxdb", "foo"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 2, len(a.Config.Outputs))

	c = config.NewConfig()
	c.OutputFilters = []string{"influxdb", "kafka"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	assert.Equal(t, 3, len(c.Outputs))
	a, _ = NewAgent(c)
	assert.Equal(t, 3, len(a.Config.Outputs))

	c = config.NewConfig()
	c.OutputFilters = []string{"influxdb", "foo", "kafka", "bar"}
	err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
	assert.NoError(t, err)
	a, _ = NewAgent(c)
	assert.Equal(t, 3, len(a.Config.Outputs))
}
예제 #3
0
func main() {
	flag.Usage = usageExit
	flag.Parse()

	if flag.NFlag() == 0 {
		usageExit()
	}

	var inputFilters []string
	if *fInputFiltersLegacy != "" {
		inputFilter := strings.TrimSpace(*fInputFiltersLegacy)
		inputFilters = strings.Split(":"+inputFilter+":", ":")
	}
	if *fInputFilters != "" {
		inputFilter := strings.TrimSpace(*fInputFilters)
		inputFilters = strings.Split(":"+inputFilter+":", ":")
	}

	var outputFilters []string
	if *fOutputFiltersLegacy != "" {
		outputFilter := strings.TrimSpace(*fOutputFiltersLegacy)
		outputFilters = strings.Split(":"+outputFilter+":", ":")
	}
	if *fOutputFilters != "" {
		outputFilter := strings.TrimSpace(*fOutputFilters)
		outputFilters = strings.Split(":"+outputFilter+":", ":")
	}

	if *fVersion {
		v := fmt.Sprintf("Telegraf - Version %s", Version)
		fmt.Println(v)
		return
	}

	if *fSampleConfig {
		config.PrintSampleConfig(inputFilters, outputFilters)
		return
	}

	if *fUsage != "" {
		if err := config.PrintInputConfig(*fUsage); err != nil {
			if err2 := config.PrintOutputConfig(*fUsage); err2 != nil {
				log.Fatalf("%s and %s", err, err2)
			}
		}
		return
	}

	var (
		c   *config.Config
		err error
	)

	if *fConfig != "" {
		c = config.NewConfig()
		c.OutputFilters = outputFilters
		c.InputFilters = inputFilters
		err = c.LoadConfig(*fConfig)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		fmt.Println("Usage: Telegraf")
		flag.PrintDefaults()
		return
	}

	if *fConfigDirectoryLegacy != "" {
		err = c.LoadDirectory(*fConfigDirectoryLegacy)
		if err != nil {
			log.Fatal(err)
		}
	}

	if *fConfigDirectory != "" {
		err = c.LoadDirectory(*fConfigDirectory)
		if err != nil {
			log.Fatal(err)
		}
	}
	if len(c.Outputs) == 0 {
		log.Fatalf("Error: no outputs found, did you provide a valid config file?")
	}
	if len(c.Inputs) == 0 {
		log.Fatalf("Error: no plugins found, did you provide a valid config file?")
	}

	ag, err := telegraf.NewAgent(c)
	if err != nil {
		log.Fatal(err)
	}

	if *fDebug {
		ag.Config.Agent.Debug = true
	}

	if *fTest {
		err = ag.Test()
		if err != nil {
			log.Fatal(err)
		}
		return
	}

	err = ag.Connect()
	if err != nil {
		log.Fatal(err)
	}

	shutdown := make(chan struct{})
	signals := make(chan os.Signal)
	signal.Notify(signals, os.Interrupt)
	go func() {
		<-signals
		close(shutdown)
	}()

	log.Printf("Starting Telegraf (version %s)\n", Version)
	log.Printf("Loaded outputs: %s", strings.Join(c.OutputNames(), " "))
	log.Printf("Loaded plugins: %s", strings.Join(c.InputNames(), " "))
	log.Printf("Tags enabled: %s", c.ListTags())

	if *fPidfile != "" {
		f, err := os.Create(*fPidfile)
		if err != nil {
			log.Fatalf("Unable to create pidfile: %s", err)
		}

		fmt.Fprintf(f, "%d\n", os.Getpid())

		f.Close()
	}

	ag.Run(shutdown)
}