From c5c6d4753d54f29a3e748255843027efb18aa103 Mon Sep 17 00:00:00 2001 From: Matteo Cerutti Date: Fri, 12 May 2017 15:46:52 +0100 Subject: [PATCH] optionally disable metrics reporting for non-running containers --- plugins/inputs/docker/README.md | 3 +++ plugins/inputs/docker/docker.go | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/docker/README.md b/plugins/inputs/docker/README.md index 849450b33cd40..9e865b8f28428 100644 --- a/plugins/inputs/docker/README.md +++ b/plugins/inputs/docker/README.md @@ -35,6 +35,9 @@ for the stat structure can be found ## Note that an empty array for both will include all labels as tags docker_label_include = [] docker_label_exclude = [] + + ## disable metrics collection for non-running containers + only_running = true ``` diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index a439af0686ed2..c436db40239f9 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -41,6 +41,8 @@ type Docker struct { testing bool labelFiltersCreated bool + + onlyRunning bool `toml:"only_running"` } // infoWrapper wraps client.Client.List for testing. @@ -112,6 +114,10 @@ var sampleConfig = ` ## Note that an empty array for both will include all labels as tags docker_label_include = [] docker_label_exclude = [] + + ## Only monitor running containers + only_running = true + ` // Description returns input description @@ -176,10 +182,13 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { for _, container := range containers { go func(c types.Container) { defer wg.Done() - err := d.gatherContainer(c, acc) - if err != nil { - acc.AddError(fmt.Errorf("E! Error gathering container %s stats: %s\n", - c.Names, err.Error())) + + if (d.onlyRunning && c.State == "running") || (!d.onlyRunning) { + err := d.gatherContainer(c, acc) + if err != nil { + acc.AddError(fmt.Errorf("E! Error gathering container %s stats: %s\n", + c.Names, err.Error())) + } } }(container) }