Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make the command "config" a subcommand #11282

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion docs/COMMANDS_AND_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down
2 changes: 1 addition & 1 deletion docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion internal/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down