Skip to content

Commit 10658cb

Browse files
committed
Cleanup & standardize config file
changes: - -sample-config will now comment out all but a few default plugins. - config file parse errors will output path to bad conf file. - cleanup 80-char line-length and some other style issues. - default package conf file will now have all plugins, but commented out. closes #199 closes #944
1 parent 9347a70 commit 10658cb

File tree

9 files changed

+1292
-112
lines changed

9 files changed

+1292
-112
lines changed

etc/telegraf.conf

Lines changed: 1130 additions & 22 deletions
Large diffs are not rendered by default.

internal/config/config.go

Lines changed: 111 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ import (
2222
"github.com/influxdata/toml/ast"
2323
)
2424

25+
var (
26+
// Default input plugins
27+
inputDefaults = []string{"cpu", "mem", "swap", "system", "kernel",
28+
"processes", "disk", "diskio"}
29+
30+
// Default output plugins
31+
outputDefaults = []string{"influxdb"}
32+
)
33+
2534
// Config specifies the URL/user/password for the database that telegraf
2635
// will be logging to, as well as all the plugins that the user has
2736
// specified
@@ -135,21 +144,23 @@ func (c *Config) ListTags() string {
135144
}
136145

137146
var header = `# Telegraf Configuration
138-
147+
#
139148
# Telegraf is entirely plugin driven. All metrics are gathered from the
140149
# declared inputs, and sent to the declared outputs.
141-
150+
#
142151
# Plugins must be declared in here to be active.
143152
# To deactivate a plugin, comment out the name and any variables.
144-
153+
#
145154
# Use 'telegraf -config telegraf.conf -test' to see what metrics a config
146155
# file would generate.
147156
157+
148158
# Global tags can be specified here in key="value" format.
149159
[global_tags]
150160
# dc = "us-east-1" # will tag all metrics with dc=us-east-1
151161
# rack = "1a"
152162
163+
153164
# Configuration for telegraf agent
154165
[agent]
155166
## Default data collection interval for all inputs
@@ -188,55 +199,72 @@ var header = `# Telegraf Configuration
188199
omit_hostname = false
189200
190201
191-
#
192-
# OUTPUTS:
193-
#
194-
202+
###############################################################################
203+
# OUTPUT PLUGINS #
204+
###############################################################################
195205
`
196206

197-
var pluginHeader = `
198-
#
199-
# INPUTS:
200-
#
207+
var inputHeader = `
208+
209+
###############################################################################
210+
# INPUT PLUGINS #
211+
###############################################################################
201212
`
202213

203214
var serviceInputHeader = `
204-
#
205-
# SERVICE INPUTS:
206-
#
215+
216+
###############################################################################
217+
# SERVICE INPUT PLUGINS #
218+
###############################################################################
207219
`
208220

209221
// PrintSampleConfig prints the sample config
210-
func PrintSampleConfig(pluginFilters []string, outputFilters []string) {
222+
func PrintSampleConfig(inputFilters []string, outputFilters []string) {
211223
fmt.Printf(header)
212224

213-
// Filter outputs
214-
var onames []string
215-
for oname := range outputs.Outputs {
216-
if len(outputFilters) == 0 || sliceContains(oname, outputFilters) {
217-
onames = append(onames, oname)
225+
if len(outputFilters) != 0 {
226+
printFilteredOutputs(outputFilters, false)
227+
} else {
228+
printFilteredOutputs(outputDefaults, false)
229+
// Print non-default outputs, commented
230+
var pnames []string
231+
for pname := range outputs.Outputs {
232+
if !sliceContains(pname, outputDefaults) {
233+
pnames = append(pnames, pname)
234+
}
218235
}
236+
sort.Strings(pnames)
237+
printFilteredOutputs(pnames, true)
219238
}
220-
sort.Strings(onames)
221239

222-
// Print Outputs
223-
for _, oname := range onames {
224-
creator := outputs.Outputs[oname]
225-
output := creator()
226-
printConfig(oname, output, "outputs")
240+
fmt.Printf(inputHeader)
241+
if len(inputFilters) != 0 {
242+
printFilteredInputs(inputFilters, false)
243+
} else {
244+
printFilteredInputs(inputDefaults, false)
245+
// Print non-default inputs, commented
246+
var pnames []string
247+
for pname := range inputs.Inputs {
248+
if !sliceContains(pname, inputDefaults) {
249+
pnames = append(pnames, pname)
250+
}
251+
}
252+
sort.Strings(pnames)
253+
printFilteredInputs(pnames, true)
227254
}
255+
}
228256

257+
func printFilteredInputs(inputFilters []string, commented bool) {
229258
// Filter inputs
230259
var pnames []string
231260
for pname := range inputs.Inputs {
232-
if len(pluginFilters) == 0 || sliceContains(pname, pluginFilters) {
261+
if sliceContains(pname, inputFilters) {
233262
pnames = append(pnames, pname)
234263
}
235264
}
236265
sort.Strings(pnames)
237266

238267
// Print Inputs
239-
fmt.Printf(pluginHeader)
240268
servInputs := make(map[string]telegraf.ServiceInput)
241269
for _, pname := range pnames {
242270
creator := inputs.Inputs[pname]
@@ -248,13 +276,34 @@ func PrintSampleConfig(pluginFilters []string, outputFilters []string) {
248276
continue
249277
}
250278

251-
printConfig(pname, input, "inputs")
279+
printConfig(pname, input, "inputs", commented)
252280
}
253281

254282
// Print Service Inputs
283+
if len(servInputs) == 0 {
284+
return
285+
}
255286
fmt.Printf(serviceInputHeader)
256287
for name, input := range servInputs {
257-
printConfig(name, input, "inputs")
288+
printConfig(name, input, "inputs", commented)
289+
}
290+
}
291+
292+
func printFilteredOutputs(outputFilters []string, commented bool) {
293+
// Filter outputs
294+
var onames []string
295+
for oname := range outputs.Outputs {
296+
if sliceContains(oname, outputFilters) {
297+
onames = append(onames, oname)
298+
}
299+
}
300+
sort.Strings(onames)
301+
302+
// Print Outputs
303+
for _, oname := range onames {
304+
creator := outputs.Outputs[oname]
305+
output := creator()
306+
printConfig(oname, output, "outputs", commented)
258307
}
259308
}
260309

@@ -263,13 +312,26 @@ type printer interface {
263312
SampleConfig() string
264313
}
265314

266-
func printConfig(name string, p printer, op string) {
267-
fmt.Printf("\n# %s\n[[%s.%s]]", p.Description(), op, name)
315+
func printConfig(name string, p printer, op string, commented bool) {
316+
comment := ""
317+
if commented {
318+
comment = "# "
319+
}
320+
fmt.Printf("\n%s# %s\n%s[[%s.%s]]", comment, p.Description(), comment,
321+
op, name)
322+
268323
config := p.SampleConfig()
269324
if config == "" {
270-
fmt.Printf("\n # no configuration\n")
325+
fmt.Printf("\n%s # no configuration\n\n", comment)
271326
} else {
272-
fmt.Printf(config)
327+
lines := strings.Split(config, "\n")
328+
for i, line := range lines {
329+
if i == 0 || i == len(lines)-1 {
330+
fmt.Print("\n")
331+
continue
332+
}
333+
fmt.Print(comment + line + "\n")
334+
}
273335
}
274336
}
275337

@@ -285,7 +347,7 @@ func sliceContains(name string, list []string) bool {
285347
// PrintInputConfig prints the config usage of a single input.
286348
func PrintInputConfig(name string) error {
287349
if creator, ok := inputs.Inputs[name]; ok {
288-
printConfig(name, creator(), "inputs")
350+
printConfig(name, creator(), "inputs", false)
289351
} else {
290352
return errors.New(fmt.Sprintf("Input %s not found", name))
291353
}
@@ -295,7 +357,7 @@ func PrintInputConfig(name string) error {
295357
// PrintOutputConfig prints the config usage of a single output.
296358
func PrintOutputConfig(name string) error {
297359
if creator, ok := outputs.Outputs[name]; ok {
298-
printConfig(name, creator(), "outputs")
360+
printConfig(name, creator(), "outputs", false)
299361
} else {
300362
return errors.New(fmt.Sprintf("Output %s not found", name))
301363
}
@@ -327,67 +389,67 @@ func (c *Config) LoadDirectory(path string) error {
327389
func (c *Config) LoadConfig(path string) error {
328390
tbl, err := config.ParseFile(path)
329391
if err != nil {
330-
return err
392+
return fmt.Errorf("Error parsing %s, %s", path, err)
331393
}
332394

333395
for name, val := range tbl.Fields {
334396
subTable, ok := val.(*ast.Table)
335397
if !ok {
336-
return errors.New("invalid configuration")
398+
return fmt.Errorf("%s: invalid configuration", path)
337399
}
338400

339401
switch name {
340402
case "agent":
341403
if err = config.UnmarshalTable(subTable, c.Agent); err != nil {
342404
log.Printf("Could not parse [agent] config\n")
343-
return err
405+
return fmt.Errorf("Error parsing %s, %s", path, err)
344406
}
345407
case "global_tags", "tags":
346408
if err = config.UnmarshalTable(subTable, c.Tags); err != nil {
347409
log.Printf("Could not parse [global_tags] config\n")
348-
return err
410+
return fmt.Errorf("Error parsing %s, %s", path, err)
349411
}
350412
case "outputs":
351413
for pluginName, pluginVal := range subTable.Fields {
352414
switch pluginSubTable := pluginVal.(type) {
353415
case *ast.Table:
354416
if err = c.addOutput(pluginName, pluginSubTable); err != nil {
355-
return err
417+
return fmt.Errorf("Error parsing %s, %s", path, err)
356418
}
357419
case []*ast.Table:
358420
for _, t := range pluginSubTable {
359421
if err = c.addOutput(pluginName, t); err != nil {
360-
return err
422+
return fmt.Errorf("Error parsing %s, %s", path, err)
361423
}
362424
}
363425
default:
364-
return fmt.Errorf("Unsupported config format: %s",
365-
pluginName)
426+
return fmt.Errorf("Unsupported config format: %s, file %s",
427+
pluginName, path)
366428
}
367429
}
368430
case "inputs", "plugins":
369431
for pluginName, pluginVal := range subTable.Fields {
370432
switch pluginSubTable := pluginVal.(type) {
371433
case *ast.Table:
372434
if err = c.addInput(pluginName, pluginSubTable); err != nil {
373-
return err
435+
return fmt.Errorf("Error parsing %s, %s", path, err)
374436
}
375437
case []*ast.Table:
376438
for _, t := range pluginSubTable {
377439
if err = c.addInput(pluginName, t); err != nil {
378-
return err
440+
return fmt.Errorf("Error parsing %s, %s", path, err)
379441
}
380442
}
381443
default:
382-
return fmt.Errorf("Unsupported config format: %s",
383-
pluginName)
444+
return fmt.Errorf("Unsupported config format: %s, file %s",
445+
pluginName, path)
384446
}
385447
}
386448
// Assume it's an input input for legacy config file support if no other
387449
// identifiers are present
388450
default:
389451
if err = c.addInput(name, subTable); err != nil {
390-
return err
452+
return fmt.Errorf("Error parsing %s, %s", path, err)
391453
}
392454
}
393455
}

plugins/inputs/disque/disque.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ type Disque struct {
2424

2525
var sampleConfig = `
2626
## An array of URI to gather stats about. Specify an ip or hostname
27-
## with optional port and password. ie disque://localhost, disque://10.10.3.33:18832,
28-
## 10.0.0.1:10000, etc.
29-
27+
## with optional port and password.
28+
## ie disque://localhost, disque://10.10.3.33:18832, 10.0.0.1:10000, etc.
3029
## If no servers are specified, then localhost is used as the host.
3130
servers = ["localhost"]
3231
`

plugins/inputs/dns_query/dns_query.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ var sampleConfig = `
3535
## Domains or subdomains to query. "."(root) is default
3636
domains = ["."] # optional
3737
38-
## Query record type. Posible values: A, AAAA, CNAME, MX, NS, PTR, TXT, SOA, SPF, SRV. Default is "NS"
38+
## Query record type. Default is "A"
39+
## Posible values: A, AAAA, CNAME, MX, NS, PTR, TXT, SOA, SPF, SRV.
3940
record_type = "A" # optional
4041
4142
## Dns server port. 53 is default

plugins/inputs/mesos/mesos.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ var sampleConfig = `
3434
# A list of Mesos masters, default value is localhost:5050.
3535
masters = ["localhost:5050"]
3636
# Metrics groups to be collected, by default, all enabled.
37-
master_collections = ["resources","master","system","slaves","frameworks","messages","evqueue","registrar"]
37+
master_collections = [
38+
"resources",
39+
"master",
40+
"system",
41+
"slaves",
42+
"frameworks",
43+
"messages",
44+
"evqueue",
45+
"registrar",
46+
]
3847
`
3948

4049
// SampleConfig returns a sample configuration block

plugins/inputs/postgresql/postgresql.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_rese
2626

2727
var sampleConfig = `
2828
## specify address via a url matching:
29-
## postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full]
29+
## postgres://[pqgotest[:password]]@localhost[/dbname]\
30+
## ?sslmode=[disable|verify-ca|verify-full]
3031
## or a simple string:
3132
## host=localhost user=pqotest password=... sslmode=... dbname=app_production
3233
##

0 commit comments

Comments
 (0)