2017-02-17 19:36:44 +00:00
|
|
|
package docker
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
2017-02-17 19:36:44 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-02-24 04:58:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-20 23:21:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDockerGatherContainerStats(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
stats := testStats()
|
|
|
|
|
|
|
|
tags := map[string]string{
|
2016-04-12 20:59:19 +00:00
|
|
|
"container_name": "redis",
|
|
|
|
"container_image": "redis/image",
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
2016-07-21 15:50:12 +00:00
|
|
|
gatherContainerStats(stats, &acc, tags, "123456789", true, true)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
2016-04-12 20:59:19 +00:00
|
|
|
// test docker_container_net measurement
|
2016-01-20 23:21:19 +00:00
|
|
|
netfields := map[string]interface{}{
|
2016-04-12 20:59:19 +00:00
|
|
|
"rx_dropped": uint64(1),
|
|
|
|
"rx_bytes": uint64(2),
|
|
|
|
"rx_errors": uint64(3),
|
|
|
|
"tx_packets": uint64(4),
|
|
|
|
"tx_dropped": uint64(1),
|
|
|
|
"rx_packets": uint64(2),
|
|
|
|
"tx_errors": uint64(3),
|
|
|
|
"tx_bytes": uint64(4),
|
|
|
|
"container_id": "123456789",
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
nettags := copyTags(tags)
|
|
|
|
nettags["network"] = "eth0"
|
2016-04-12 20:59:19 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_net", netfields, nettags)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
2016-07-21 15:50:12 +00:00
|
|
|
netfields = map[string]interface{}{
|
|
|
|
"rx_dropped": uint64(6),
|
|
|
|
"rx_bytes": uint64(8),
|
|
|
|
"rx_errors": uint64(10),
|
|
|
|
"tx_packets": uint64(12),
|
|
|
|
"tx_dropped": uint64(6),
|
|
|
|
"rx_packets": uint64(8),
|
|
|
|
"tx_errors": uint64(10),
|
|
|
|
"tx_bytes": uint64(12),
|
|
|
|
"container_id": "123456789",
|
|
|
|
}
|
|
|
|
nettags = copyTags(tags)
|
|
|
|
nettags["network"] = "total"
|
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_net", netfields, nettags)
|
|
|
|
|
2016-01-20 23:21:19 +00:00
|
|
|
// test docker_blkio measurement
|
|
|
|
blkiotags := copyTags(tags)
|
|
|
|
blkiotags["device"] = "6:0"
|
|
|
|
blkiofields := map[string]interface{}{
|
|
|
|
"io_service_bytes_recursive_read": uint64(100),
|
|
|
|
"io_serviced_recursive_write": uint64(101),
|
2016-05-04 12:17:23 +00:00
|
|
|
"container_id": "123456789",
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
2016-04-18 22:20:46 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_blkio", blkiofields, blkiotags)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
2016-07-21 15:50:12 +00:00
|
|
|
blkiotags = copyTags(tags)
|
|
|
|
blkiotags["device"] = "total"
|
|
|
|
blkiofields = map[string]interface{}{
|
|
|
|
"io_service_bytes_recursive_read": uint64(100),
|
|
|
|
"io_serviced_recursive_write": uint64(302),
|
|
|
|
"container_id": "123456789",
|
|
|
|
}
|
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_blkio", blkiofields, blkiotags)
|
|
|
|
|
2016-04-12 20:59:19 +00:00
|
|
|
// test docker_container_mem measurement
|
2016-02-11 10:49:48 +00:00
|
|
|
memfields := map[string]interface{}{
|
|
|
|
"max_usage": uint64(1001),
|
|
|
|
"usage": uint64(1111),
|
|
|
|
"fail_count": uint64(1),
|
|
|
|
"limit": uint64(2000),
|
|
|
|
"total_pgmafault": uint64(0),
|
|
|
|
"cache": uint64(0),
|
|
|
|
"mapped_file": uint64(0),
|
|
|
|
"total_inactive_file": uint64(0),
|
|
|
|
"pgpgout": uint64(0),
|
|
|
|
"rss": uint64(0),
|
|
|
|
"total_mapped_file": uint64(0),
|
|
|
|
"writeback": uint64(0),
|
|
|
|
"unevictable": uint64(0),
|
|
|
|
"pgpgin": uint64(0),
|
|
|
|
"total_unevictable": uint64(0),
|
|
|
|
"pgmajfault": uint64(0),
|
|
|
|
"total_rss": uint64(44),
|
|
|
|
"total_rss_huge": uint64(444),
|
|
|
|
"total_writeback": uint64(55),
|
|
|
|
"total_inactive_anon": uint64(0),
|
|
|
|
"rss_huge": uint64(0),
|
|
|
|
"hierarchical_memory_limit": uint64(0),
|
|
|
|
"total_pgfault": uint64(0),
|
|
|
|
"total_active_file": uint64(0),
|
|
|
|
"active_anon": uint64(0),
|
|
|
|
"total_active_anon": uint64(0),
|
|
|
|
"total_pgpgout": uint64(0),
|
|
|
|
"total_cache": uint64(0),
|
|
|
|
"inactive_anon": uint64(0),
|
|
|
|
"active_file": uint64(1),
|
|
|
|
"pgfault": uint64(2),
|
|
|
|
"inactive_file": uint64(3),
|
|
|
|
"total_pgpgin": uint64(4),
|
|
|
|
"usage_percent": float64(55.55),
|
2016-04-12 20:59:19 +00:00
|
|
|
"container_id": "123456789",
|
2016-02-11 10:49:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 20:59:19 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_mem", memfields, tags)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
2016-04-12 20:59:19 +00:00
|
|
|
// test docker_container_cpu measurement
|
2016-01-20 23:21:19 +00:00
|
|
|
cputags := copyTags(tags)
|
|
|
|
cputags["cpu"] = "cpu-total"
|
2016-02-11 10:49:48 +00:00
|
|
|
cpufields := map[string]interface{}{
|
|
|
|
"usage_total": uint64(500),
|
|
|
|
"usage_in_usermode": uint64(100),
|
|
|
|
"usage_in_kernelmode": uint64(200),
|
|
|
|
"usage_system": uint64(100),
|
|
|
|
"throttling_periods": uint64(1),
|
|
|
|
"throttling_throttled_periods": uint64(0),
|
|
|
|
"throttling_throttled_time": uint64(0),
|
|
|
|
"usage_percent": float64(400.0),
|
2016-04-12 20:59:19 +00:00
|
|
|
"container_id": "123456789",
|
2016-02-11 10:49:48 +00:00
|
|
|
}
|
2016-04-12 20:59:19 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_cpu", cpufields, cputags)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
cputags["cpu"] = "cpu0"
|
|
|
|
cpu0fields := map[string]interface{}{
|
2016-05-09 08:22:53 +00:00
|
|
|
"usage_total": uint64(1),
|
|
|
|
"container_id": "123456789",
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
2016-04-12 20:59:19 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_cpu", cpu0fields, cputags)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
cputags["cpu"] = "cpu1"
|
|
|
|
cpu1fields := map[string]interface{}{
|
2016-05-09 08:22:53 +00:00
|
|
|
"usage_total": uint64(1002),
|
|
|
|
"container_id": "123456789",
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
2016-04-12 20:59:19 +00:00
|
|
|
acc.AssertContainsTaggedFields(t, "docker_container_cpu", cpu1fields, cputags)
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 17:37:41 +00:00
|
|
|
func testStats() *types.StatsJSON {
|
|
|
|
stats := &types.StatsJSON{}
|
|
|
|
stats.Read = time.Now()
|
|
|
|
stats.Networks = make(map[string]types.NetworkStats)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
stats.CPUStats.CPUUsage.PercpuUsage = []uint64{1, 1002}
|
|
|
|
stats.CPUStats.CPUUsage.UsageInUsermode = 100
|
|
|
|
stats.CPUStats.CPUUsage.TotalUsage = 500
|
|
|
|
stats.CPUStats.CPUUsage.UsageInKernelmode = 200
|
2016-04-03 17:37:41 +00:00
|
|
|
stats.CPUStats.SystemUsage = 100
|
2016-01-20 23:21:19 +00:00
|
|
|
stats.CPUStats.ThrottlingData.Periods = 1
|
|
|
|
|
2016-02-09 15:20:56 +00:00
|
|
|
stats.PreCPUStats.CPUUsage.TotalUsage = 400
|
2016-04-03 17:37:41 +00:00
|
|
|
stats.PreCPUStats.SystemUsage = 50
|
|
|
|
|
|
|
|
stats.MemoryStats.Stats = make(map[string]uint64)
|
|
|
|
stats.MemoryStats.Stats["total_pgmajfault"] = 0
|
|
|
|
stats.MemoryStats.Stats["cache"] = 0
|
|
|
|
stats.MemoryStats.Stats["mapped_file"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_inactive_file"] = 0
|
|
|
|
stats.MemoryStats.Stats["pagpgout"] = 0
|
|
|
|
stats.MemoryStats.Stats["rss"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_mapped_file"] = 0
|
|
|
|
stats.MemoryStats.Stats["writeback"] = 0
|
|
|
|
stats.MemoryStats.Stats["unevictable"] = 0
|
|
|
|
stats.MemoryStats.Stats["pgpgin"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_unevictable"] = 0
|
|
|
|
stats.MemoryStats.Stats["pgmajfault"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_rss"] = 44
|
|
|
|
stats.MemoryStats.Stats["total_rss_huge"] = 444
|
|
|
|
stats.MemoryStats.Stats["total_write_back"] = 55
|
|
|
|
stats.MemoryStats.Stats["total_inactive_anon"] = 0
|
|
|
|
stats.MemoryStats.Stats["rss_huge"] = 0
|
|
|
|
stats.MemoryStats.Stats["hierarchical_memory_limit"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_pgfault"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_active_file"] = 0
|
|
|
|
stats.MemoryStats.Stats["active_anon"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_active_anon"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_pgpgout"] = 0
|
|
|
|
stats.MemoryStats.Stats["total_cache"] = 0
|
|
|
|
stats.MemoryStats.Stats["inactive_anon"] = 0
|
|
|
|
stats.MemoryStats.Stats["active_file"] = 1
|
|
|
|
stats.MemoryStats.Stats["pgfault"] = 2
|
|
|
|
stats.MemoryStats.Stats["inactive_file"] = 3
|
|
|
|
stats.MemoryStats.Stats["total_pgpgin"] = 4
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
stats.MemoryStats.MaxUsage = 1001
|
|
|
|
stats.MemoryStats.Usage = 1111
|
|
|
|
stats.MemoryStats.Failcnt = 1
|
2016-02-09 15:20:56 +00:00
|
|
|
stats.MemoryStats.Limit = 2000
|
2016-01-20 23:21:19 +00:00
|
|
|
|
2016-04-03 17:37:41 +00:00
|
|
|
stats.Networks["eth0"] = types.NetworkStats{
|
2016-01-20 23:21:19 +00:00
|
|
|
RxDropped: 1,
|
|
|
|
RxBytes: 2,
|
|
|
|
RxErrors: 3,
|
|
|
|
TxPackets: 4,
|
|
|
|
TxDropped: 1,
|
|
|
|
RxPackets: 2,
|
|
|
|
TxErrors: 3,
|
|
|
|
TxBytes: 4,
|
|
|
|
}
|
|
|
|
|
2016-07-21 15:50:12 +00:00
|
|
|
stats.Networks["eth1"] = types.NetworkStats{
|
|
|
|
RxDropped: 5,
|
|
|
|
RxBytes: 6,
|
|
|
|
RxErrors: 7,
|
|
|
|
TxPackets: 8,
|
|
|
|
TxDropped: 5,
|
|
|
|
RxPackets: 6,
|
|
|
|
TxErrors: 7,
|
|
|
|
TxBytes: 8,
|
|
|
|
}
|
|
|
|
|
2016-04-03 17:37:41 +00:00
|
|
|
sbr := types.BlkioStatEntry{
|
2016-01-20 23:21:19 +00:00
|
|
|
Major: 6,
|
|
|
|
Minor: 0,
|
|
|
|
Op: "read",
|
|
|
|
Value: 100,
|
|
|
|
}
|
2016-04-03 17:37:41 +00:00
|
|
|
sr := types.BlkioStatEntry{
|
2016-01-20 23:21:19 +00:00
|
|
|
Major: 6,
|
|
|
|
Minor: 0,
|
|
|
|
Op: "write",
|
|
|
|
Value: 101,
|
|
|
|
}
|
2016-07-21 15:50:12 +00:00
|
|
|
sr2 := types.BlkioStatEntry{
|
|
|
|
Major: 6,
|
|
|
|
Minor: 1,
|
|
|
|
Op: "write",
|
|
|
|
Value: 201,
|
|
|
|
}
|
2016-01-20 23:21:19 +00:00
|
|
|
|
2016-04-03 17:37:41 +00:00
|
|
|
stats.BlkioStats.IoServiceBytesRecursive = append(
|
|
|
|
stats.BlkioStats.IoServiceBytesRecursive, sbr)
|
|
|
|
stats.BlkioStats.IoServicedRecursive = append(
|
|
|
|
stats.BlkioStats.IoServicedRecursive, sr)
|
2016-07-21 15:50:12 +00:00
|
|
|
stats.BlkioStats.IoServicedRecursive = append(
|
|
|
|
stats.BlkioStats.IoServicedRecursive, sr2)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
return stats
|
|
|
|
}
|
2016-02-24 04:58:14 +00:00
|
|
|
|
|
|
|
func TestDockerGatherInfo(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
2017-02-17 19:36:44 +00:00
|
|
|
d := Docker{
|
|
|
|
client: nil,
|
|
|
|
testing: true,
|
|
|
|
}
|
2016-02-24 04:58:14 +00:00
|
|
|
|
|
|
|
err := d.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"docker",
|
|
|
|
map[string]interface{}{
|
2016-04-03 17:37:41 +00:00
|
|
|
"n_listener_events": int(0),
|
|
|
|
"n_cpus": int(4),
|
|
|
|
"n_used_file_descriptors": int(19),
|
|
|
|
"n_containers": int(108),
|
2016-09-21 09:37:49 +00:00
|
|
|
"n_containers_running": int(98),
|
|
|
|
"n_containers_stopped": int(6),
|
|
|
|
"n_containers_paused": int(3),
|
2016-04-03 17:37:41 +00:00
|
|
|
"n_images": int(199),
|
|
|
|
"n_goroutines": int(39),
|
2016-02-24 04:58:14 +00:00
|
|
|
},
|
2016-09-06 07:37:46 +00:00
|
|
|
map[string]string{"engine_host": "absol"},
|
2016-02-24 04:58:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
|
|
|
"docker_data",
|
|
|
|
map[string]interface{}{
|
|
|
|
"used": int64(17300000000),
|
|
|
|
"total": int64(107400000000),
|
|
|
|
"available": int64(36530000000),
|
|
|
|
},
|
|
|
|
map[string]string{
|
2016-09-06 07:37:46 +00:00
|
|
|
"unit": "bytes",
|
|
|
|
"engine_host": "absol",
|
2016-02-24 04:58:14 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
2016-04-12 20:59:19 +00:00
|
|
|
"docker_container_cpu",
|
2016-02-24 04:58:14 +00:00
|
|
|
map[string]interface{}{
|
2016-05-09 08:22:53 +00:00
|
|
|
"usage_total": uint64(1231652),
|
|
|
|
"container_id": "b7dfbb9478a6ae55e237d4d74f8bbb753f0817192b5081334dc78476296e2173",
|
2016-02-24 04:58:14 +00:00
|
|
|
},
|
|
|
|
map[string]string{
|
2016-07-19 16:31:01 +00:00
|
|
|
"container_name": "etcd2",
|
2016-12-16 13:53:16 +00:00
|
|
|
"container_image": "quay.io:4443/coreos/etcd",
|
2016-07-19 16:31:01 +00:00
|
|
|
"cpu": "cpu3",
|
|
|
|
"container_version": "v2.2.2",
|
2016-09-06 07:37:46 +00:00
|
|
|
"engine_host": "absol",
|
2016-02-24 04:58:14 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
acc.AssertContainsTaggedFields(t,
|
2016-04-12 20:59:19 +00:00
|
|
|
"docker_container_mem",
|
2016-02-24 04:58:14 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"total_pgpgout": uint64(0),
|
|
|
|
"usage_percent": float64(0),
|
|
|
|
"rss": uint64(0),
|
|
|
|
"total_writeback": uint64(0),
|
|
|
|
"active_anon": uint64(0),
|
|
|
|
"total_pgmafault": uint64(0),
|
|
|
|
"total_rss": uint64(0),
|
|
|
|
"total_unevictable": uint64(0),
|
|
|
|
"active_file": uint64(0),
|
|
|
|
"total_mapped_file": uint64(0),
|
|
|
|
"pgpgin": uint64(0),
|
|
|
|
"total_active_file": uint64(0),
|
|
|
|
"total_active_anon": uint64(0),
|
|
|
|
"total_cache": uint64(0),
|
|
|
|
"inactive_anon": uint64(0),
|
|
|
|
"pgmajfault": uint64(0),
|
|
|
|
"total_inactive_anon": uint64(0),
|
|
|
|
"total_rss_huge": uint64(0),
|
|
|
|
"rss_huge": uint64(0),
|
|
|
|
"hierarchical_memory_limit": uint64(0),
|
|
|
|
"pgpgout": uint64(0),
|
|
|
|
"unevictable": uint64(0),
|
|
|
|
"total_inactive_file": uint64(0),
|
|
|
|
"writeback": uint64(0),
|
|
|
|
"total_pgfault": uint64(0),
|
|
|
|
"total_pgpgin": uint64(0),
|
|
|
|
"cache": uint64(0),
|
|
|
|
"mapped_file": uint64(0),
|
|
|
|
"inactive_file": uint64(0),
|
|
|
|
"max_usage": uint64(0),
|
|
|
|
"fail_count": uint64(0),
|
|
|
|
"pgfault": uint64(0),
|
|
|
|
"usage": uint64(0),
|
|
|
|
"limit": uint64(18935443456),
|
2016-04-12 20:59:19 +00:00
|
|
|
"container_id": "b7dfbb9478a6ae55e237d4d74f8bbb753f0817192b5081334dc78476296e2173",
|
2016-02-24 04:58:14 +00:00
|
|
|
},
|
|
|
|
map[string]string{
|
2016-09-06 07:37:46 +00:00
|
|
|
"engine_host": "absol",
|
2016-07-19 16:31:01 +00:00
|
|
|
"container_name": "etcd2",
|
2016-12-16 13:53:16 +00:00
|
|
|
"container_image": "quay.io:4443/coreos/etcd",
|
2016-07-19 16:31:01 +00:00
|
|
|
"container_version": "v2.2.2",
|
2016-02-24 04:58:14 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
//fmt.Print(info)
|
|
|
|
}
|