func TestAgent_OmitHostname(t *testing.T) { c := config.NewConfig() c.Agent.OmitHostname = true _, err := NewAgent(c) assert.NoError(t, err) assert.NotContains(t, c.Tags, "host") }
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)) }
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)) }
func main() { reload := make(chan bool, 1) reload <- true for <-reload { reload <- false flag.Usage = func() { usageExit(0) } flag.Parse() args := flag.Args() if flag.NFlag() == 0 && len(args) == 0 { usageExit(0) } 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 len(args) > 0 { switch args[0] { case "version": v := fmt.Sprintf("Telegraf - Version %s", Version) fmt.Println(v) return case "config": config.PrintSampleConfig(inputFilters, outputFilters) return } } if *fOutputList { fmt.Println("Available Output Plugins:") for k, _ := range outputs.Outputs { fmt.Printf(" %s\n", k) } return } if *fInputList { fmt.Println("Available Input Plugins:") for k, _ := range inputs.Inputs { fmt.Printf(" %s\n", k) } return } 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("You must specify a config file. See telegraf --help") os.Exit(1) } 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 inputs found, did you provide a valid config file?") } ag, err := agent.NewAgent(c) if err != nil { log.Fatal(err) } if *fDebug { ag.Config.Agent.Debug = true } if *fQuiet { ag.Config.Agent.Quiet = 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, syscall.SIGHUP) go func() { sig := <-signals if sig == os.Interrupt { close(shutdown) } if sig == syscall.SIGHUP { log.Printf("Reloading Telegraf config\n") <-reload reload <- true close(shutdown) } }() log.Printf("Starting Telegraf (version %s)\n", Version) log.Printf("Loaded outputs: %s", strings.Join(c.OutputNames(), " ")) log.Printf("Loaded inputs: %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) } }
func reloadLoop(stop chan struct{}, s service.Service) { defer func() { if service.Interactive() { os.Exit(0) } return }() reload := make(chan bool, 1) reload <- true for <-reload { reload <- false flag.Usage = func() { usageExit(0) } flag.Parse() args := flag.Args() var inputFilters []string if *fInputFilters != "" { inputFilter := strings.TrimSpace(*fInputFilters) inputFilters = strings.Split(":"+inputFilter+":", ":") } var outputFilters []string if *fOutputFilters != "" { outputFilter := strings.TrimSpace(*fOutputFilters) outputFilters = strings.Split(":"+outputFilter+":", ":") } if len(args) > 0 { switch args[0] { case "version": v := fmt.Sprintf("Telegraf - version %s", version) fmt.Println(v) return case "config": config.PrintSampleConfig(inputFilters, outputFilters) return } } // switch for flags which just do something and exit immediately switch { case *fOutputList: fmt.Println("Available Output Plugins:") for k, _ := range outputs.Outputs { fmt.Printf(" %s\n", k) } return case *fInputList: fmt.Println("Available Input Plugins:") for k, _ := range inputs.Inputs { fmt.Printf(" %s\n", k) } return case *fVersion: v := fmt.Sprintf("Telegraf - version %s", version) fmt.Println(v) return case *fSampleConfig: config.PrintSampleConfig(inputFilters, outputFilters) return case *fUsage != "": if err := config.PrintInputConfig(*fUsage); err != nil { if err2 := config.PrintOutputConfig(*fUsage); err2 != nil { log.Fatalf("%s and %s", err, err2) } } return case *fService != "" && runtime.GOOS == "windows": if *fConfig != "" { (*svcConfig).Arguments = []string{"-config", *fConfig} } err := service.Control(s, *fService) if err != nil { log.Fatal(err) } return } // If no other options are specified, load the config file and run. c := config.NewConfig() c.OutputFilters = outputFilters c.InputFilters = inputFilters err := c.LoadConfig(*fConfig) if err != nil { fmt.Println(err) os.Exit(1) } 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 inputs found, did you provide a valid config file?") } ag, err := agent.NewAgent(c) if err != nil { log.Fatal(err) } if *fDebug { ag.Config.Agent.Debug = true } if *fQuiet { ag.Config.Agent.Quiet = 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, syscall.SIGHUP) go func() { select { case sig := <-signals: if sig == os.Interrupt { close(shutdown) } if sig == syscall.SIGHUP { log.Printf("Reloading Telegraf config\n") <-reload reload <- true close(shutdown) } case <-stop: close(shutdown) } }() log.Printf("Starting Telegraf (version %s)\n", version) log.Printf("Loaded outputs: %s", strings.Join(c.OutputNames(), " ")) log.Printf("Loaded inputs: %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) } }
func reloadLoop(stop chan struct{}, s service.Service) { defer func() { if service.Interactive() { os.Exit(0) } return }() reload := make(chan bool, 1) reload <- true for <-reload { reload <- false flag.Parse() args := flag.Args() var inputFilters []string if *fInputFilters != "" { inputFilter := strings.TrimSpace(*fInputFilters) inputFilters = strings.Split(":"+inputFilter+":", ":") } var outputFilters []string if *fOutputFilters != "" { outputFilter := strings.TrimSpace(*fOutputFilters) outputFilters = strings.Split(":"+outputFilter+":", ":") } var aggregatorFilters []string if *fAggregatorFilters != "" { aggregatorFilter := strings.TrimSpace(*fAggregatorFilters) aggregatorFilters = strings.Split(":"+aggregatorFilter+":", ":") } var processorFilters []string if *fProcessorFilters != "" { processorFilter := strings.TrimSpace(*fProcessorFilters) processorFilters = strings.Split(":"+processorFilter+":", ":") } if len(args) > 0 { switch args[0] { case "version": fmt.Printf("Telegraf v%s (git: %s %s)\n", version, branch, commit) return case "config": config.PrintSampleConfig( inputFilters, outputFilters, aggregatorFilters, processorFilters, ) return } } // switch for flags which just do something and exit immediately switch { case *fOutputList: fmt.Println("Available Output Plugins:") for k, _ := range outputs.Outputs { fmt.Printf(" %s\n", k) } return case *fInputList: fmt.Println("Available Input Plugins:") for k, _ := range inputs.Inputs { fmt.Printf(" %s\n", k) } return case *fVersion: fmt.Printf("Telegraf v%s (git: %s %s)\n", version, branch, commit) return case *fSampleConfig: config.PrintSampleConfig( inputFilters, outputFilters, aggregatorFilters, processorFilters, ) return case *fUsage != "": if err := config.PrintInputConfig(*fUsage); err != nil { if err2 := config.PrintOutputConfig(*fUsage); err2 != nil { log.Fatalf("E! %s and %s", err, err2) } } return } // If no other options are specified, load the config file and run. c := config.NewConfig() c.OutputFilters = outputFilters c.InputFilters = inputFilters err := c.LoadConfig(*fConfig) if err != nil { log.Fatal("E! " + err.Error()) } if *fConfigDirectory != "" { err = c.LoadDirectory(*fConfigDirectory) if err != nil { log.Fatal("E! " + err.Error()) } } if len(c.Outputs) == 0 { log.Fatalf("E! Error: no outputs found, did you provide a valid config file?") } if len(c.Inputs) == 0 { log.Fatalf("E! Error: no inputs found, did you provide a valid config file?") } ag, err := agent.NewAgent(c) if err != nil { log.Fatal("E! " + err.Error()) } // Setup logging logger.SetupLogging( ag.Config.Agent.Debug || *fDebug, ag.Config.Agent.Quiet || *fQuiet, ag.Config.Agent.Logfile, ) if *fTest { err = ag.Test() if err != nil { log.Fatal("E! " + err.Error()) } return } err = ag.Connect() if err != nil { log.Fatal("E! " + err.Error()) } shutdown := make(chan struct{}) signals := make(chan os.Signal) signal.Notify(signals, os.Interrupt, syscall.SIGHUP) go func() { select { case sig := <-signals: if sig == os.Interrupt { close(shutdown) } if sig == syscall.SIGHUP { log.Printf("I! Reloading Telegraf config\n") <-reload reload <- true close(shutdown) } case <-stop: close(shutdown) } }() log.Printf("I! Starting Telegraf (version %s)\n", version) log.Printf("I! Loaded outputs: %s", strings.Join(c.OutputNames(), " ")) log.Printf("I! Loaded inputs: %s", strings.Join(c.InputNames(), " ")) log.Printf("I! Tags enabled: %s", c.ListTags()) if *fPidfile != "" { f, err := os.Create(*fPidfile) if err != nil { log.Fatalf("E! Unable to create pidfile: %s", err) } fmt.Fprintf(f, "%d\n", os.Getpid()) f.Close() } ag.Run(shutdown) } }