Add cli support for outputting sections of the config (#5585)

This commit is contained in:
Greg 2019-04-25 21:34:40 -06:00 committed by Daniel Nelson
parent c11327ee34
commit 6436005553
2 changed files with 104 additions and 49 deletions

View File

@ -41,6 +41,8 @@ var fVersion = flag.Bool("version", false, "display the version and exit")
var fSampleConfig = flag.Bool("sample-config", false, var fSampleConfig = flag.Bool("sample-config", false,
"print out full sample configuration") "print out full sample configuration")
var fPidfile = flag.String("pidfile", "", "file to write our pid to") var fPidfile = flag.String("pidfile", "", "file to write our pid to")
var fSectionFilters = flag.String("section-filter", "",
"filter the sections to print, separator is ':'. Valid values are 'agent', 'global_tags', 'outputs', 'processors', 'aggregators' and 'inputs'")
var fInputFilters = flag.String("input-filter", "", var fInputFilters = flag.String("input-filter", "",
"filter the inputs to enable, separator is :") "filter the inputs to enable, separator is :")
var fInputList = flag.Bool("input-list", false, var fInputList = flag.Bool("input-list", false,
@ -249,7 +251,10 @@ func main() {
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
inputFilters, outputFilters := []string{}, []string{} sectionFilters, inputFilters, outputFilters := []string{}, []string{}, []string{}
if *fSectionFilters != "" {
sectionFilters = strings.Split(":"+strings.TrimSpace(*fSectionFilters)+":", ":")
}
if *fInputFilters != "" { if *fInputFilters != "" {
inputFilters = strings.Split(":"+strings.TrimSpace(*fInputFilters)+":", ":") inputFilters = strings.Split(":"+strings.TrimSpace(*fInputFilters)+":", ":")
} }
@ -289,6 +294,7 @@ func main() {
return return
case "config": case "config":
config.PrintSampleConfig( config.PrintSampleConfig(
sectionFilters,
inputFilters, inputFilters,
outputFilters, outputFilters,
aggregatorFilters, aggregatorFilters,
@ -317,6 +323,7 @@ func main() {
return return
case *fSampleConfig: case *fSampleConfig:
config.PrintSampleConfig( config.PrintSampleConfig(
sectionFilters,
inputFilters, inputFilters,
outputFilters, outputFilters,
aggregatorFilters, aggregatorFilters,

View File

@ -32,6 +32,10 @@ import (
) )
var ( var (
// Default sections
sectionDefaults = []string{"agent", "global_tags", "outputs",
"processors", "aggregators", "inputs"}
// Default input plugins // Default input plugins
inputDefaults = []string{"cpu", "mem", "swap", "system", "kernel", inputDefaults = []string{"cpu", "mem", "swap", "system", "kernel",
"processes", "disk", "diskio"} "processes", "disk", "diskio"}
@ -212,7 +216,8 @@ var header = `# Telegraf Configuration
# them with ${}. For strings the variable must be within quotes (ie, "${STR_VAR}"), # them with ${}. For strings the variable must be within quotes (ie, "${STR_VAR}"),
# for numbers and booleans they should be plain (ie, ${INT_VAR}, ${BOOL_VAR}) # for numbers and booleans they should be plain (ie, ${INT_VAR}, ${BOOL_VAR})
`
var globalTagsConfig = `
# Global tags can be specified here in key="value" format. # Global tags can be specified here in key="value" format.
[global_tags] [global_tags]
# dc = "us-east-1" # will tag all metrics with dc=us-east-1 # dc = "us-east-1" # will tag all metrics with dc=us-east-1
@ -220,7 +225,8 @@ var header = `# Telegraf Configuration
## Environment variables can be used as tags, and throughout the config file ## Environment variables can be used as tags, and throughout the config file
# user = "$USER" # user = "$USER"
`
var agentConfig = `
# Configuration for telegraf agent # Configuration for telegraf agent
[agent] [agent]
## Default data collection interval for all inputs ## Default data collection interval for all inputs
@ -273,106 +279,137 @@ var header = `# Telegraf Configuration
## If set to true, do no set the "host" tag in the telegraf agent. ## If set to true, do no set the "host" tag in the telegraf agent.
omit_hostname = false omit_hostname = false
`
var outputHeader = `
############################################################################### ###############################################################################
# OUTPUT PLUGINS # # OUTPUT PLUGINS #
############################################################################### ###############################################################################
` `
var processorHeader = ` var processorHeader = `
############################################################################### ###############################################################################
# PROCESSOR PLUGINS # # PROCESSOR PLUGINS #
############################################################################### ###############################################################################
` `
var aggregatorHeader = ` var aggregatorHeader = `
############################################################################### ###############################################################################
# AGGREGATOR PLUGINS # # AGGREGATOR PLUGINS #
############################################################################### ###############################################################################
` `
var inputHeader = ` var inputHeader = `
############################################################################### ###############################################################################
# INPUT PLUGINS # # INPUT PLUGINS #
############################################################################### ###############################################################################
` `
var serviceInputHeader = ` var serviceInputHeader = `
############################################################################### ###############################################################################
# SERVICE INPUT PLUGINS # # SERVICE INPUT PLUGINS #
############################################################################### ###############################################################################
` `
// PrintSampleConfig prints the sample config // PrintSampleConfig prints the sample config
func PrintSampleConfig( func PrintSampleConfig(
sectionFilters []string,
inputFilters []string, inputFilters []string,
outputFilters []string, outputFilters []string,
aggregatorFilters []string, aggregatorFilters []string,
processorFilters []string, processorFilters []string,
) { ) {
// print headers
fmt.Printf(header) fmt.Printf(header)
if len(sectionFilters) == 0 {
sectionFilters = sectionDefaults
}
printFilteredGlobalSections(sectionFilters)
// print output plugins // print output plugins
if len(outputFilters) != 0 { if sliceContains("outputs", sectionFilters) {
printFilteredOutputs(outputFilters, false) if len(outputFilters) != 0 {
} else { if len(outputFilters) >= 3 && outputFilters[1] != "none" {
printFilteredOutputs(outputDefaults, false) fmt.Printf(outputHeader)
// Print non-default outputs, commented
var pnames []string
for pname := range outputs.Outputs {
if !sliceContains(pname, outputDefaults) {
pnames = append(pnames, pname)
} }
printFilteredOutputs(outputFilters, false)
} else {
fmt.Printf(outputHeader)
printFilteredOutputs(outputDefaults, false)
// Print non-default outputs, commented
var pnames []string
for pname := range outputs.Outputs {
if !sliceContains(pname, outputDefaults) {
pnames = append(pnames, pname)
}
}
sort.Strings(pnames)
printFilteredOutputs(pnames, true)
} }
sort.Strings(pnames)
printFilteredOutputs(pnames, true)
} }
// print processor plugins // print processor plugins
fmt.Printf(processorHeader) if sliceContains("processors", sectionFilters) {
if len(processorFilters) != 0 { if len(processorFilters) != 0 {
printFilteredProcessors(processorFilters, false) if len(processorFilters) >= 3 && processorFilters[1] != "none" {
} else { fmt.Printf(processorHeader)
pnames := []string{} }
for pname := range processors.Processors { printFilteredProcessors(processorFilters, false)
pnames = append(pnames, pname) } else {
fmt.Printf(processorHeader)
pnames := []string{}
for pname := range processors.Processors {
pnames = append(pnames, pname)
}
sort.Strings(pnames)
printFilteredProcessors(pnames, true)
} }
sort.Strings(pnames)
printFilteredProcessors(pnames, true)
} }
// pring aggregator plugins // print aggregator plugins
fmt.Printf(aggregatorHeader) if sliceContains("aggregators", sectionFilters) {
if len(aggregatorFilters) != 0 { if len(aggregatorFilters) != 0 {
printFilteredAggregators(aggregatorFilters, false) if len(aggregatorFilters) >= 3 && aggregatorFilters[1] != "none" {
} else { fmt.Printf(aggregatorHeader)
pnames := []string{} }
for pname := range aggregators.Aggregators { printFilteredAggregators(aggregatorFilters, false)
pnames = append(pnames, pname) } else {
fmt.Printf(aggregatorHeader)
pnames := []string{}
for pname := range aggregators.Aggregators {
pnames = append(pnames, pname)
}
sort.Strings(pnames)
printFilteredAggregators(pnames, true)
} }
sort.Strings(pnames)
printFilteredAggregators(pnames, true)
} }
// print input plugins // print input plugins
fmt.Printf(inputHeader) if sliceContains("inputs", sectionFilters) {
if len(inputFilters) != 0 { if len(inputFilters) != 0 {
printFilteredInputs(inputFilters, false) if len(inputFilters) >= 3 && inputFilters[1] != "none" {
} else { fmt.Printf(inputHeader)
printFilteredInputs(inputDefaults, false)
// Print non-default inputs, commented
var pnames []string
for pname := range inputs.Inputs {
if !sliceContains(pname, inputDefaults) {
pnames = append(pnames, pname)
} }
printFilteredInputs(inputFilters, false)
} else {
fmt.Printf(inputHeader)
printFilteredInputs(inputDefaults, false)
// Print non-default inputs, commented
var pnames []string
for pname := range inputs.Inputs {
if !sliceContains(pname, inputDefaults) {
pnames = append(pnames, pname)
}
}
sort.Strings(pnames)
printFilteredInputs(pnames, true)
} }
sort.Strings(pnames)
printFilteredInputs(pnames, true)
} }
} }
@ -447,6 +484,7 @@ func printFilteredInputs(inputFilters []string, commented bool) {
return return
} }
sort.Strings(servInputNames) sort.Strings(servInputNames)
fmt.Printf(serviceInputHeader) fmt.Printf(serviceInputHeader)
for _, name := range servInputNames { for _, name := range servInputNames {
printConfig(name, servInputs[name], "inputs", commented) printConfig(name, servInputs[name], "inputs", commented)
@ -471,6 +509,16 @@ func printFilteredOutputs(outputFilters []string, commented bool) {
} }
} }
func printFilteredGlobalSections(sectionFilters []string) {
if sliceContains("agent", sectionFilters) {
fmt.Printf(agentConfig)
}
if sliceContains("global_tags", sectionFilters) {
fmt.Printf(globalTagsConfig)
}
}
type printer interface { type printer interface {
Description() string Description() string
SampleConfig() string SampleConfig() string