From 42602a3f356e54457ef3e411db18e8272076fd79 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Mon, 24 Aug 2015 14:52:46 -0600 Subject: [PATCH] Provide a -usage flag for printing the usage of a single plugin Closes #136 --- cmd/telegraf/telegraf.go | 18 +++++++++++++++--- config.go | 12 ++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 07a61146d..38f323215 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -13,13 +13,18 @@ import ( _ "github.com/influxdb/telegraf/plugins/all" ) -var fDebug = flag.Bool("debug", false, "show metrics as they're generated to stdout") +var fDebug = flag.Bool("debug", false, + "show metrics as they're generated to stdout") var fTest = flag.Bool("test", false, "gather metrics, print them out, and exit") var fConfig = flag.String("config", "", "configuration file to load") var fVersion = flag.Bool("version", false, "display the version") -var fSampleConfig = flag.Bool("sample-config", false, "print out full sample configuration") +var fSampleConfig = flag.Bool("sample-config", false, + "print out full sample configuration") var fPidfile = flag.String("pidfile", "", "file to write our pid to") -var fPLuginsFilter = flag.String("filter", "", "filter the plugins to enable, separator is :") +var fPLuginsFilter = flag.String("filter", "", + "filter the plugins to enable, separator is :") +var fUsage = flag.String("usage", "", + "print usage for a plugin, ie, 'telegraf -usage mysql'") // Telegraf version // -ldflags "-X main.Version=`git describe --always --tags`" @@ -39,6 +44,13 @@ func main() { return } + if *fUsage != "" { + if err := telegraf.PrintPluginConfig(*fUsage); err != nil { + log.Fatal(err) + } + return + } + var ( config *telegraf.Config err error diff --git a/config.go b/config.go index 3ba9d4a49..fe47c6d0d 100644 --- a/config.go +++ b/config.go @@ -421,3 +421,15 @@ func PrintSampleConfig() { } } } + +// PrintPluginConfig prints the config usage of a single plugin. +func PrintPluginConfig(name string) error { + if creator, ok := plugins.Plugins[name]; ok { + plugin := creator() + fmt.Printf("# %s\n[%s]\n", plugin.Description(), name) + fmt.Printf(strings.TrimSpace(plugin.SampleConfig())) + } else { + return errors.New(fmt.Sprintf("Plugin %s not found", name)) + } + return nil +}