diff --git a/README.md b/README.md index a85b3dde5dd39..4ded217a5c2a8 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ telegraf config > telegraf.conf ### Generate config with only cpu input & influxdb output plugins defined ```shell -telegraf --section-filter agent:inputs:outputs --input-filter cpu --output-filter influxdb config +telegraf config --section-filter agent:inputs:outputs --input-filter cpu --output-filter influxdb ``` ### Run a single telegraf collection, outputting metrics to stdout diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 8284ab5ae874b..8990769954a25 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -84,6 +84,20 @@ var fProcessorFilters = flag.String("processor-filter", "", var fUsage = flag.String("usage", "", "print usage for a plugin, ie, 'telegraf --usage mysql'") +// Initialize the subcommand `telegraf config` +// This duplicates the above filters which are used for `telegraf --sample-config` and `telegraf --deprecation-list` +var configCmd = flag.NewFlagSet("config", flag.ExitOnError) +var fSubSectionFilters = configCmd.String("section-filter", "", + "filter the sections to print, separator is ':'. Valid values are 'agent', 'global_tags', 'outputs', 'processors', 'aggregators' and 'inputs'") +var fSubInputFilters = configCmd.String("input-filter", "", + "filter the inputs to enable, separator is :") +var fSubOutputFilters = configCmd.String("output-filter", "", + "filter the outputs to enable, separator is :") +var fsubAggregatorFilters = configCmd.String("aggregator-filter", "", + "filter the aggregators to enable, separator is :") +var fSubProcessorFilters = configCmd.String("processor-filter", "", + "filter the processors to enable, separator is :") + //nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows var fService = flag.String("service", "", "operate on the service (windows only)") @@ -356,6 +370,16 @@ func formatFullVersion() string { return strings.Join(parts, " ") } +func deleteEmpty(s []string) []string { + var r []string + for _, str := range s { + if str != "" { + r = append(r, str) + } + } + return r +} + func main() { flag.Var(&fConfigs, "config", "configuration file to load") flag.Var(&fConfigDirs, "config-directory", "directory containing additional *.conf files") @@ -421,6 +445,37 @@ func main() { fmt.Println(formatFullVersion()) return case "config": + err := configCmd.Parse(args[1:]) + if err != nil { + log.Fatal("E! " + err.Error()) + } + + // The sub_Filters are populated when the filter flags are set after the subcommand config + // e.g. telegraf config --section-filter inputs + subSectionFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubSectionFilters)+":", ":")) + subInputFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubInputFilters)+":", ":")) + subOutputFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubOutputFilters)+":", ":")) + subAggregatorFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fsubAggregatorFilters)+":", ":")) + subProcessorFilters := deleteEmpty(strings.Split(":"+strings.TrimSpace(*fSubProcessorFilters)+":", ":")) + + // Overwrite the global filters if the subfilters are defined, this allows for backwards compatibility + // Now you can still filter the sample config like so: telegraf --section-filter inputs config + if len(subSectionFilters) > 0 { + sectionFilters = subSectionFilters + } + if len(subInputFilters) > 0 { + inputFilters = subInputFilters + } + if len(subOutputFilters) > 0 { + outputFilters = subOutputFilters + } + if len(subAggregatorFilters) > 0 { + aggregatorFilters = subAggregatorFilters + } + if len(subProcessorFilters) > 0 { + processorFilters = subProcessorFilters + } + config.PrintSampleConfig( sectionFilters, inputFilters, diff --git a/docs/COMMANDS_AND_FLAGS.md b/docs/COMMANDS_AND_FLAGS.md index 4579e84488058..d906fd651f1c9 100644 --- a/docs/COMMANDS_AND_FLAGS.md +++ b/docs/COMMANDS_AND_FLAGS.md @@ -49,7 +49,7 @@ telegraf [flags] **Generate config with only cpu input & influxdb output plugins defined:** -`telegraf --input-filter cpu --output-filter influxdb config` +`telegraf config --input-filter cpu --output-filter influxdb` **Run a single telegraf collection, outputting metrics to stdout:** diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 06644d12bbe56..e1b22023b6aa3 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -19,7 +19,7 @@ To generate a file with specific inputs and outputs, you can use the --input-filter and --output-filter flags: ```sh -telegraf --input-filter cpu:mem:net:swap --output-filter influxdb:kafka config +telegraf config --input-filter cpu:mem:net:swap --output-filter influxdb:kafka ``` [View the full list][flags] of Telegraf commands and flags or by running `telegraf --help`. diff --git a/internal/usage.go b/internal/usage.go index 3e6e4fb14fc80..65fb378669df7 100644 --- a/internal/usage.go +++ b/internal/usage.go @@ -51,7 +51,7 @@ Examples: telegraf config > telegraf.conf # generate config with only cpu input & influxdb output plugins defined - telegraf --input-filter cpu --output-filter influxdb config + telegraf config --input-filter cpu --output-filter influxdb # run a single telegraf collection, outputting metrics to stdout telegraf --config telegraf.conf --test