From d40086ca32948402471e81d302f965d747ad0c4c Mon Sep 17 00:00:00 2001 From: "valentin.daviot" Date: Wed, 21 Nov 2018 14:16:17 +0100 Subject: [PATCH] percpu --- plugins/inputs/docker/docker.go | 47 ++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index a59b9f7fa955c..fc6172790cc83 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -34,6 +34,7 @@ type Docker struct { Timeout internal.Duration PerDevice bool `toml:"perdevice"` Total bool `toml:"total"` + PerCPU bool `toml:"percpu"` TagEnvironment []string `toml:"tag_env"` LabelInclude []string `toml:"docker_label_include"` LabelExclude []string `toml:"docker_label_exclude"` @@ -44,6 +45,7 @@ type Docker struct { ContainerStateInclude []string `toml:"container_state_include"` ContainerStateExclude []string `toml:"container_state_exclude"` + tlsint.ClientConfig newEnvClient func() (Client, error) @@ -443,8 +445,12 @@ func (d *Docker) gatherContainer( } acc.AddFields("docker_container_health", healthfields, tags, time.Now()) } + per := []bool{ + d.PerDevice, + d.PerCPU, + } - gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType) + gatherContainerStats(v, acc, tags, container.ID, per, d.Total, daemonOSType) return nil } @@ -454,12 +460,14 @@ func gatherContainerStats( acc telegraf.Accumulator, tags map[string]string, id string, - perDevice bool, + per []bool, total bool, daemonOSType string, ) { tm := stat.Read + perDevice, perCpu := per[0], per[1] + if tm.Before(time.Unix(0, 0)) { tm = time.Now() } @@ -551,21 +559,36 @@ func gatherContainerStats( // If we have OnlineCPUs field, then use it to restrict stats gathering to only Online CPUs // (https://github.com/moby/moby/commit/115f91d7575d6de6c7781a96a082f144fd17e400) - var percpuusage []uint64 - if stat.CPUStats.OnlineCPUs > 0 { - percpuusage = stat.CPUStats.CPUUsage.PercpuUsage[:stat.CPUStats.OnlineCPUs] + if perCpu { + var percpuusage []uint64 + if stat.CPUStats.OnlineCPUs > 0 { + percpuusage = stat.CPUStats.CPUUsage.PercpuUsage[:stat.CPUStats.OnlineCPUs] + } else { + percpuusage = stat.CPUStats.CPUUsage.PercpuUsage + } + + for i, percpu := range percpuusage { + percputags := copyTags(tags) + percputags["cpu"] = fmt.Sprintf("cpu%d", i) + fields := map[string]interface{}{ + "usage_total": percpu, + "container_id": id, + } + acc.AddFields("docker_container_cpu", fields, percputags, tm) + } } else { - percpuusage = stat.CPUStats.CPUUsage.PercpuUsage - } + var cpuUsage uint64 - for i, percpu := range percpuusage { - percputags := copyTags(tags) - percputags["cpu"] = fmt.Sprintf("cpu%d", i) + for _, usage := range stat.CPUStats.CPUUsage.PercpuUsage { + cpuUsage += usage + } + aggcputags := copyTags(tags) + aggcputags["cpu"] = "cpus" fields := map[string]interface{}{ - "usage_total": percpu, + "usage_total": cpuUsage, "container_id": id, } - acc.AddFields("docker_container_cpu", fields, percputags, tm) + acc.AddFields("docker_container_cpu", fields, aggcputags, tm) } totalNetworkStatMap := make(map[string]interface{})