From f74503087c52efea621966219080712c024eed42 Mon Sep 17 00:00:00 2001 From: prashanthjbabu Date: Tue, 19 Jun 2018 04:03:14 +0530 Subject: [PATCH] Add container status tag to docker input (#4259) --- plugins/inputs/docker/docker.go | 17 +++++++++++++++++ plugins/inputs/docker/docker_test.go | 2 ++ plugins/inputs/docker/docker_testdata.go | 6 ++++++ 3 files changed, 25 insertions(+) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index a59b9f7fa..aa1de7479 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -435,6 +435,23 @@ func (d *Docker) gatherContainer( } } } + if info.State != nil { + tags["container_status"] = info.State.Status + statefields := map[string]interface{}{ + "oomkilled": info.State.OOMKilled, + "pid": info.State.Pid, + "exitcode": info.State.ExitCode, + } + container_time, err := time.Parse(time.RFC3339, info.State.StartedAt) + if err == nil && !container_time.IsZero() { + statefields["started_at"] = container_time.UnixNano() + } + container_time, err = time.Parse(time.RFC3339, info.State.FinishedAt) + if err == nil && !container_time.IsZero() { + statefields["finished_at"] = container_time.UnixNano() + } + acc.AddFields("docker_container_status", statefields, tags, time.Now()) + } if info.State.Health != nil { healthfields := map[string]interface{}{ diff --git a/plugins/inputs/docker/docker_test.go b/plugins/inputs/docker/docker_test.go index 4b9d00c80..d51c61c00 100644 --- a/plugins/inputs/docker/docker_test.go +++ b/plugins/inputs/docker/docker_test.go @@ -653,6 +653,7 @@ func TestDockerGatherInfo(t *testing.T) { "label1": "test_value_1", "label2": "test_value_2", "server_version": "17.09.0-ce", + "container_status": "running", }, ) acc.AssertContainsTaggedFields(t, @@ -676,6 +677,7 @@ func TestDockerGatherInfo(t *testing.T) { "label1": "test_value_1", "label2": "test_value_2", "server_version": "17.09.0-ce", + "container_status": "running", }, ) } diff --git a/plugins/inputs/docker/docker_testdata.go b/plugins/inputs/docker/docker_testdata.go index 42a0b888f..1168048a2 100644 --- a/plugins/inputs/docker/docker_testdata.go +++ b/plugins/inputs/docker/docker_testdata.go @@ -484,6 +484,12 @@ var containerInspect = types.ContainerJSON{ FailingStreak: 1, Status: "Unhealthy", }, + Status: "running", + OOMKilled: false, + Pid: 1234, + ExitCode: 0, + StartedAt: "2018-06-14T05:48:53.266176036Z", + FinishedAt: "0001-01-01T00:00:00Z", }, }, }