From aad6a7e262ed3c001e9d72f371cb8aebf42348ff Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Wed, 23 Sep 2015 13:54:22 -0700 Subject: [PATCH] Only run the cpu plugin twice when using -test This can be easily extended to other plugins that need it by tacking their name onto the switch statement. Also eliminating the unused TestAllPlugins code and cleaning up some stray Printlns --- CHANGELOG.md | 1 + agent.go | 52 +++++++++++----------------------------- cmd/telegraf/telegraf.go | 8 +------ 3 files changed, 16 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33b3bd220..4eb52eb0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## v0.1.10 [unreleased] ### Release Notes +- The -test flag will now only output 2 collections for plugins that need it ### Features - [#205](https://github.com/influxdb/telegraf/issues/205): Include per-db redis keyspace info diff --git a/agent.go b/agent.go index 06e79a6c5..d6674f885 100644 --- a/agent.go +++ b/agent.go @@ -116,14 +116,15 @@ func (a *Agent) LoadOutputs(filters []string) ([]string, error) { var names []string for _, name := range a.Config.OutputsDeclared() { - fmt.Println(outputs.Outputs) creator, ok := outputs.Outputs[name] if !ok { return nil, fmt.Errorf("Undefined but requested output: %s", name) } if sliceContains(name, filters) || len(filters) == 0 { - fmt.Println("OUTPUT ENABLED: ", name) + if a.Debug { + log.Println("Output Enabled: ", name) + } output := creator() err := a.Config.ApplyOutput(name, output) @@ -306,36 +307,6 @@ func (a *Agent) flush(bp *BatchPoints) error { return outerr } -// TestAllPlugins verifies that we can 'Gather' from all plugins with the -// default configuration -func (a *Agent) TestAllPlugins() error { - var names []string - - for name := range plugins.Plugins { - names = append(names, name) - } - - sort.Strings(names) - - var acc BatchPoints - acc.Debug = true - - fmt.Printf("* Testing all plugins with default configuration\n") - - for _, name := range names { - plugin := plugins.Plugins[name]() - - fmt.Printf("* Plugin: %s\n", name) - - acc.Prefix = name + "_" - if err := plugin.Gather(&acc); err != nil { - return err - } - } - - return nil -} - // Test verifies that we can 'Gather' from all plugins with their configured // Config struct func (a *Agent) Test() error { @@ -347,7 +318,7 @@ func (a *Agent) Test() error { acc.Prefix = plugin.name + "_" acc.Config = plugin.config - fmt.Printf("* Plugin: %s Collection 1\n", plugin.name) + fmt.Printf("* Plugin: %s, Collection 1\n", plugin.name) if plugin.config.Interval != 0 { fmt.Printf("* Internal: %s\n", plugin.config.Interval) } @@ -356,13 +327,18 @@ func (a *Agent) Test() error { return err } - time.Sleep(500 * time.Millisecond) - fmt.Printf("* Plugin: %s Collection 2\n", plugin.name) - if err := plugin.plugin.Gather(&acc); err != nil { - return err + // Special instructions for some plugins. cpu, for example, needs to be + // run twice in order to return cpu usage percentages. + switch plugin.name { + case "cpu": + time.Sleep(500 * time.Millisecond) + fmt.Printf("* Plugin: %s, Collection 2\n", plugin.name) + if err := plugin.plugin.Gather(&acc); err != nil { + return err + } } - } + } return nil } diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 7080f7671..bb1e0121d 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -109,16 +109,10 @@ func main() { } if *fTest { - if *fConfig != "" { - err = ag.Test() - } else { - err = ag.TestAllPlugins() - } - + err = ag.Test() if err != nil { log.Fatal(err) } - return }