From 286dcab4f9bac428a63c4858bc83f62ea2efc536 Mon Sep 17 00:00:00 2001 From: Jacob McCann Date: Fri, 12 Jan 2018 19:43:51 -0600 Subject: [PATCH] Add container health metrics to docker input (#3666) --- plugins/inputs/docker/README.md | 5 ++++- plugins/inputs/docker/docker.go | 17 +++++++++++++---- plugins/inputs/docker/docker_testdata.go | 8 ++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/docker/README.md b/plugins/inputs/docker/README.md index faee9a57b..7e9907588 100644 --- a/plugins/inputs/docker/README.md +++ b/plugins/inputs/docker/README.md @@ -199,6 +199,9 @@ based on the availability of per-cpu stats on your system. - network - docker_container_blkio specific: - device +- docker_container_health specific: + - health_status + - failing_streak - docker_swarm specific: - service_id - service_name @@ -257,4 +260,4 @@ io_serviced_recursive_total=6599i,io_serviced_recursive_write=107i 1453409536840 >docker_swarm, service_id=xaup2o9krw36j2dy1mjx1arjw,service_mode=replicated,service_name=test,\ tasks_desired=3,tasks_running=3 1508968160000000000 -``` \ No newline at end of file +``` diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index 595bcc518..7178eb497 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -391,12 +391,13 @@ func (d *Docker) gatherContainer( } } + info, err := d.client.ContainerInspect(ctx, container.ID) + if err != nil { + return fmt.Errorf("Error inspecting docker container: %s", err.Error()) + } + // Add whitelisted environment variables to tags if len(d.TagEnvironment) > 0 { - info, err := d.client.ContainerInspect(ctx, container.ID) - if err != nil { - return fmt.Errorf("Error inspecting docker container: %s", err.Error()) - } for _, envvar := range info.Config.Env { for _, configvar := range d.TagEnvironment { dock_env := strings.SplitN(envvar, "=", 2) @@ -408,6 +409,14 @@ func (d *Docker) gatherContainer( } } + if info.State.Health != nil { + healthfields := map[string]interface{}{ + "health_status": info.State.Health.Status, + "failing_streak": info.ContainerJSONBase.State.Health.FailingStreak, + } + acc.AddFields("docker_container_health", healthfields, tags, time.Now()) + } + gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType) return nil diff --git a/plugins/inputs/docker/docker_testdata.go b/plugins/inputs/docker/docker_testdata.go index 929119fcb..8042461db 100644 --- a/plugins/inputs/docker/docker_testdata.go +++ b/plugins/inputs/docker/docker_testdata.go @@ -477,4 +477,12 @@ var containerInspect = types.ContainerJSON{ "PATH=/bin:/sbin", }, }, + ContainerJSONBase: &types.ContainerJSONBase{ + State: &types.ContainerState{ + Health: &types.Health{ + FailingStreak: 1, + Status: "Unhealthy", + }, + }, + }, }