2016-01-20 23:21:19 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2016-02-24 04:58:14 +00:00
|
|
|
"encoding/json"
|
2016-01-20 23:21:19 +00:00
|
|
|
"fmt"
|
2016-04-02 22:34:34 +00:00
|
|
|
"io"
|
2016-01-29 17:11:28 +00:00
|
|
|
"log"
|
2016-02-24 04:58:14 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2016-01-20 23:21:19 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/engine-api/client"
|
|
|
|
"github.com/docker/engine-api/types"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-05-01 16:20:15 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-20 23:21:19 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
// Docker object
|
2016-01-20 23:21:19 +00:00
|
|
|
type Docker struct {
|
|
|
|
Endpoint string
|
|
|
|
ContainerNames []string
|
2016-05-01 16:20:15 +00:00
|
|
|
Timeout internal.Duration
|
2016-07-21 15:50:12 +00:00
|
|
|
PerDevice bool `toml:"perdevice"`
|
|
|
|
Total bool `toml:"total"`
|
2016-01-20 23:21:19 +00:00
|
|
|
|
2016-09-06 07:37:46 +00:00
|
|
|
client DockerClient
|
|
|
|
engine_host string
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
// DockerClient interface, useful for testing
|
2016-02-24 04:58:14 +00:00
|
|
|
type DockerClient interface {
|
2016-04-02 22:34:34 +00:00
|
|
|
Info(ctx context.Context) (types.Info, error)
|
|
|
|
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
|
|
|
|
ContainerStats(ctx context.Context, containerID string, stream bool) (io.ReadCloser, error)
|
2016-02-24 04:58:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
// KB, MB, GB, TB, PB...human friendly
|
2016-02-24 04:58:14 +00:00
|
|
|
const (
|
|
|
|
KB = 1000
|
|
|
|
MB = 1000 * KB
|
|
|
|
GB = 1000 * MB
|
|
|
|
TB = 1000 * GB
|
|
|
|
PB = 1000 * TB
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sizeRegex = regexp.MustCompile(`^(\d+(\.\d+)*) ?([kKmMgGtTpP])?[bB]?$`)
|
|
|
|
)
|
|
|
|
|
2016-01-20 23:21:19 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Docker Endpoint
|
|
|
|
## To use TCP, set endpoint = "tcp://[ip]:[port]"
|
|
|
|
## To use environment variables (ie, docker-machine), set endpoint = "ENV"
|
2016-01-20 23:21:19 +00:00
|
|
|
endpoint = "unix:///var/run/docker.sock"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Only collect metrics for these containers, collect all if empty
|
2016-01-20 23:21:19 +00:00
|
|
|
container_names = []
|
2016-05-01 16:20:15 +00:00
|
|
|
## Timeout for docker list, info, and stats commands
|
|
|
|
timeout = "5s"
|
2016-07-21 15:50:12 +00:00
|
|
|
|
|
|
|
## Whether to report for each container per-device blkio (8:0, 8:1...) and
|
|
|
|
## network (eth0, eth1, ...) stats or not
|
|
|
|
perdevice = true
|
|
|
|
## Whether to report for each container total blkio and network stats or not
|
|
|
|
total = false
|
|
|
|
|
2016-01-20 23:21:19 +00:00
|
|
|
`
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
// Description returns input description
|
2016-01-20 23:21:19 +00:00
|
|
|
func (d *Docker) Description() string {
|
|
|
|
return "Read metrics about docker containers"
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
// SampleConfig prints sampleConfig
|
2016-01-20 23:21:19 +00:00
|
|
|
func (d *Docker) SampleConfig() string { return sampleConfig }
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
// Gather starts stats collection
|
2016-01-27 21:21:36 +00:00
|
|
|
func (d *Docker) Gather(acc telegraf.Accumulator) error {
|
2016-01-20 23:21:19 +00:00
|
|
|
if d.client == nil {
|
2016-04-02 22:34:34 +00:00
|
|
|
var c *client.Client
|
2016-01-20 23:21:19 +00:00
|
|
|
var err error
|
2016-04-02 22:34:34 +00:00
|
|
|
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
|
2016-01-20 23:21:19 +00:00
|
|
|
if d.Endpoint == "ENV" {
|
2016-04-02 22:34:34 +00:00
|
|
|
c, err = client.NewEnvClient()
|
2016-01-20 23:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if d.Endpoint == "" {
|
2016-04-02 22:34:34 +00:00
|
|
|
c, err = client.NewClient("unix:///var/run/docker.sock", "", nil, defaultHeaders)
|
2016-01-20 23:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-02 22:34:34 +00:00
|
|
|
c, err = client.NewClient(d.Endpoint, "", nil, defaultHeaders)
|
2016-01-20 23:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.client = c
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:58:14 +00:00
|
|
|
// Get daemon info
|
|
|
|
err := d.gatherInfo(acc)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// List containers
|
2016-04-02 22:34:34 +00:00
|
|
|
opts := types.ContainerListOptions{}
|
2016-05-01 16:20:15 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
|
|
|
|
defer cancel()
|
|
|
|
containers, err := d.client.ContainerList(ctx, opts)
|
2016-01-20 23:21:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:58:14 +00:00
|
|
|
// Get container data
|
2016-01-20 23:21:19 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(containers))
|
|
|
|
for _, container := range containers {
|
2016-04-02 22:34:34 +00:00
|
|
|
go func(c types.Container) {
|
2016-01-20 23:21:19 +00:00
|
|
|
defer wg.Done()
|
|
|
|
err := d.gatherContainer(c, acc)
|
|
|
|
if err != nil {
|
2016-04-12 20:59:19 +00:00
|
|
|
log.Printf("Error gathering container %s stats: %s\n",
|
|
|
|
c.Names, err.Error())
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
}(container)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:58:14 +00:00
|
|
|
func (d *Docker) gatherInfo(acc telegraf.Accumulator) error {
|
|
|
|
// Init vars
|
|
|
|
dataFields := make(map[string]interface{})
|
|
|
|
metadataFields := make(map[string]interface{})
|
|
|
|
now := time.Now()
|
|
|
|
// Get info from docker daemon
|
2016-05-01 16:20:15 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
|
|
|
|
defer cancel()
|
|
|
|
info, err := d.client.Info(ctx)
|
2016-02-24 04:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-06 07:37:46 +00:00
|
|
|
d.engine_host = info.Name
|
2016-02-24 04:58:14 +00:00
|
|
|
|
|
|
|
fields := map[string]interface{}{
|
2016-04-02 22:34:34 +00:00
|
|
|
"n_cpus": info.NCPU,
|
|
|
|
"n_used_file_descriptors": info.NFd,
|
|
|
|
"n_containers": info.Containers,
|
|
|
|
"n_images": info.Images,
|
|
|
|
"n_goroutines": info.NGoroutines,
|
|
|
|
"n_listener_events": info.NEventsListener,
|
2016-02-24 04:58:14 +00:00
|
|
|
}
|
|
|
|
// Add metrics
|
|
|
|
acc.AddFields("docker",
|
|
|
|
fields,
|
2016-09-06 07:37:46 +00:00
|
|
|
map[string]string{"engine_host": d.engine_host},
|
2016-02-24 04:58:14 +00:00
|
|
|
now)
|
|
|
|
acc.AddFields("docker",
|
2016-04-02 22:34:34 +00:00
|
|
|
map[string]interface{}{"memory_total": info.MemTotal},
|
2016-09-06 07:37:46 +00:00
|
|
|
map[string]string{"unit": "bytes", "engine_host": d.engine_host},
|
2016-02-24 04:58:14 +00:00
|
|
|
now)
|
|
|
|
// Get storage metrics
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, rawData := range info.DriverStatus {
|
2016-02-24 04:58:14 +00:00
|
|
|
// Try to convert string to int (bytes)
|
|
|
|
value, err := parseSize(rawData[1])
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := strings.ToLower(strings.Replace(rawData[0], " ", "_", -1))
|
|
|
|
if name == "pool_blocksize" {
|
|
|
|
// pool blocksize
|
|
|
|
acc.AddFields("docker",
|
|
|
|
map[string]interface{}{"pool_blocksize": value},
|
2016-09-06 07:37:46 +00:00
|
|
|
map[string]string{"unit": "bytes", "engine_host": d.engine_host},
|
2016-02-24 04:58:14 +00:00
|
|
|
now)
|
|
|
|
} else if strings.HasPrefix(name, "data_space_") {
|
|
|
|
// data space
|
2016-04-02 22:34:34 +00:00
|
|
|
fieldName := strings.TrimPrefix(name, "data_space_")
|
|
|
|
dataFields[fieldName] = value
|
2016-02-24 04:58:14 +00:00
|
|
|
} else if strings.HasPrefix(name, "metadata_space_") {
|
|
|
|
// metadata space
|
2016-04-02 22:34:34 +00:00
|
|
|
fieldName := strings.TrimPrefix(name, "metadata_space_")
|
|
|
|
metadataFields[fieldName] = value
|
2016-02-24 04:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(dataFields) > 0 {
|
|
|
|
acc.AddFields("docker_data",
|
|
|
|
dataFields,
|
2016-09-06 07:37:46 +00:00
|
|
|
map[string]string{"unit": "bytes", "engine_host": d.engine_host},
|
2016-02-24 04:58:14 +00:00
|
|
|
now)
|
|
|
|
}
|
|
|
|
if len(metadataFields) > 0 {
|
|
|
|
acc.AddFields("docker_metadata",
|
|
|
|
metadataFields,
|
2016-09-06 07:37:46 +00:00
|
|
|
map[string]string{"unit": "bytes", "engine_host": d.engine_host},
|
2016-02-24 04:58:14 +00:00
|
|
|
now)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:21:19 +00:00
|
|
|
func (d *Docker) gatherContainer(
|
2016-04-02 22:34:34 +00:00
|
|
|
container types.Container,
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2016-01-20 23:21:19 +00:00
|
|
|
) error {
|
2016-04-02 22:34:34 +00:00
|
|
|
var v *types.StatsJSON
|
2016-01-20 23:21:19 +00:00
|
|
|
// Parse container name
|
|
|
|
cname := "unknown"
|
|
|
|
if len(container.Names) > 0 {
|
|
|
|
// Not sure what to do with other names, just take the first.
|
|
|
|
cname = strings.TrimPrefix(container.Names[0], "/")
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:31:01 +00:00
|
|
|
// the image name sometimes has a version part.
|
|
|
|
// ie, rabbitmq:3-management
|
|
|
|
imageParts := strings.Split(container.Image, ":")
|
|
|
|
imageName := imageParts[0]
|
|
|
|
imageVersion := "unknown"
|
|
|
|
if len(imageParts) > 1 {
|
|
|
|
imageVersion = imageParts[1]
|
|
|
|
}
|
2016-01-20 23:21:19 +00:00
|
|
|
tags := map[string]string{
|
2016-09-06 07:37:46 +00:00
|
|
|
"engine_host": d.engine_host,
|
2016-07-19 16:31:01 +00:00
|
|
|
"container_name": cname,
|
|
|
|
"container_image": imageName,
|
|
|
|
"container_version": imageVersion,
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
if len(d.ContainerNames) > 0 {
|
|
|
|
if !sliceContains(cname, d.ContainerNames) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:20:15 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
|
|
|
|
defer cancel()
|
|
|
|
r, err := d.client.ContainerStats(ctx, container.ID, false)
|
2016-04-02 22:34:34 +00:00
|
|
|
if err != nil {
|
2016-05-15 11:19:53 +00:00
|
|
|
return fmt.Errorf("Error getting docker stats: %s", err.Error())
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
2016-04-02 22:34:34 +00:00
|
|
|
defer r.Close()
|
|
|
|
dec := json.NewDecoder(r)
|
|
|
|
if err = dec.Decode(&v); err != nil {
|
2016-04-12 20:59:19 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error decoding: %s", err.Error())
|
2016-01-29 17:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 23:21:19 +00:00
|
|
|
// Add labels to tags
|
2016-04-12 20:59:19 +00:00
|
|
|
for k, label := range container.Labels {
|
|
|
|
tags[k] = label
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 15:50:12 +00:00
|
|
|
gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gatherContainerStats(
|
2016-04-02 22:34:34 +00:00
|
|
|
stat *types.StatsJSON,
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2016-01-20 23:21:19 +00:00
|
|
|
tags map[string]string,
|
2016-04-12 20:59:19 +00:00
|
|
|
id string,
|
2016-07-21 15:50:12 +00:00
|
|
|
perDevice bool,
|
|
|
|
total bool,
|
2016-01-20 23:21:19 +00:00
|
|
|
) {
|
|
|
|
now := stat.Read
|
|
|
|
|
|
|
|
memfields := map[string]interface{}{
|
|
|
|
"max_usage": stat.MemoryStats.MaxUsage,
|
|
|
|
"usage": stat.MemoryStats.Usage,
|
|
|
|
"fail_count": stat.MemoryStats.Failcnt,
|
|
|
|
"limit": stat.MemoryStats.Limit,
|
2016-04-02 22:34:34 +00:00
|
|
|
"total_pgmafault": stat.MemoryStats.Stats["total_pgmajfault"],
|
|
|
|
"cache": stat.MemoryStats.Stats["cache"],
|
|
|
|
"mapped_file": stat.MemoryStats.Stats["mapped_file"],
|
|
|
|
"total_inactive_file": stat.MemoryStats.Stats["total_inactive_file"],
|
|
|
|
"pgpgout": stat.MemoryStats.Stats["pagpgout"],
|
|
|
|
"rss": stat.MemoryStats.Stats["rss"],
|
|
|
|
"total_mapped_file": stat.MemoryStats.Stats["total_mapped_file"],
|
|
|
|
"writeback": stat.MemoryStats.Stats["writeback"],
|
|
|
|
"unevictable": stat.MemoryStats.Stats["unevictable"],
|
|
|
|
"pgpgin": stat.MemoryStats.Stats["pgpgin"],
|
|
|
|
"total_unevictable": stat.MemoryStats.Stats["total_unevictable"],
|
|
|
|
"pgmajfault": stat.MemoryStats.Stats["pgmajfault"],
|
|
|
|
"total_rss": stat.MemoryStats.Stats["total_rss"],
|
|
|
|
"total_rss_huge": stat.MemoryStats.Stats["total_rss_huge"],
|
|
|
|
"total_writeback": stat.MemoryStats.Stats["total_write_back"],
|
|
|
|
"total_inactive_anon": stat.MemoryStats.Stats["total_inactive_anon"],
|
|
|
|
"rss_huge": stat.MemoryStats.Stats["rss_huge"],
|
|
|
|
"hierarchical_memory_limit": stat.MemoryStats.Stats["hierarchical_memory_limit"],
|
|
|
|
"total_pgfault": stat.MemoryStats.Stats["total_pgfault"],
|
|
|
|
"total_active_file": stat.MemoryStats.Stats["total_active_file"],
|
|
|
|
"active_anon": stat.MemoryStats.Stats["active_anon"],
|
|
|
|
"total_active_anon": stat.MemoryStats.Stats["total_active_anon"],
|
|
|
|
"total_pgpgout": stat.MemoryStats.Stats["total_pgpgout"],
|
|
|
|
"total_cache": stat.MemoryStats.Stats["total_cache"],
|
|
|
|
"inactive_anon": stat.MemoryStats.Stats["inactive_anon"],
|
|
|
|
"active_file": stat.MemoryStats.Stats["active_file"],
|
|
|
|
"pgfault": stat.MemoryStats.Stats["pgfault"],
|
|
|
|
"inactive_file": stat.MemoryStats.Stats["inactive_file"],
|
|
|
|
"total_pgpgin": stat.MemoryStats.Stats["total_pgpgin"],
|
2016-02-11 10:49:48 +00:00
|
|
|
"usage_percent": calculateMemPercent(stat),
|
2016-04-12 20:59:19 +00:00
|
|
|
"container_id": id,
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
2016-04-12 20:59:19 +00:00
|
|
|
acc.AddFields("docker_container_mem", memfields, tags, now)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
cpufields := map[string]interface{}{
|
|
|
|
"usage_total": stat.CPUStats.CPUUsage.TotalUsage,
|
|
|
|
"usage_in_usermode": stat.CPUStats.CPUUsage.UsageInUsermode,
|
|
|
|
"usage_in_kernelmode": stat.CPUStats.CPUUsage.UsageInKernelmode,
|
2016-04-02 22:34:34 +00:00
|
|
|
"usage_system": stat.CPUStats.SystemUsage,
|
2016-01-20 23:21:19 +00:00
|
|
|
"throttling_periods": stat.CPUStats.ThrottlingData.Periods,
|
|
|
|
"throttling_throttled_periods": stat.CPUStats.ThrottlingData.ThrottledPeriods,
|
|
|
|
"throttling_throttled_time": stat.CPUStats.ThrottlingData.ThrottledTime,
|
2016-02-11 10:49:48 +00:00
|
|
|
"usage_percent": calculateCPUPercent(stat),
|
2016-04-12 20:59:19 +00:00
|
|
|
"container_id": id,
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
cputags := copyTags(tags)
|
|
|
|
cputags["cpu"] = "cpu-total"
|
2016-04-12 20:59:19 +00:00
|
|
|
acc.AddFields("docker_container_cpu", cpufields, cputags, now)
|
2016-01-20 23:21:19 +00:00
|
|
|
|
|
|
|
for i, percpu := range stat.CPUStats.CPUUsage.PercpuUsage {
|
|
|
|
percputags := copyTags(tags)
|
|
|
|
percputags["cpu"] = fmt.Sprintf("cpu%d", i)
|
2016-05-09 08:22:53 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"usage_total": percpu,
|
|
|
|
"container_id": id,
|
|
|
|
}
|
|
|
|
acc.AddFields("docker_container_cpu", fields, percputags, now)
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 15:50:12 +00:00
|
|
|
totalNetworkStatMap := make(map[string]interface{})
|
2016-01-20 23:21:19 +00:00
|
|
|
for network, netstats := range stat.Networks {
|
|
|
|
netfields := map[string]interface{}{
|
2016-04-12 20:59:19 +00:00
|
|
|
"rx_dropped": netstats.RxDropped,
|
|
|
|
"rx_bytes": netstats.RxBytes,
|
|
|
|
"rx_errors": netstats.RxErrors,
|
|
|
|
"tx_packets": netstats.TxPackets,
|
|
|
|
"tx_dropped": netstats.TxDropped,
|
|
|
|
"rx_packets": netstats.RxPackets,
|
|
|
|
"tx_errors": netstats.TxErrors,
|
|
|
|
"tx_bytes": netstats.TxBytes,
|
|
|
|
"container_id": id,
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
// Create a new network tag dictionary for the "network" tag
|
2016-07-21 15:50:12 +00:00
|
|
|
if perDevice {
|
|
|
|
nettags := copyTags(tags)
|
|
|
|
nettags["network"] = network
|
|
|
|
acc.AddFields("docker_container_net", netfields, nettags, now)
|
|
|
|
}
|
|
|
|
if total {
|
|
|
|
for field, value := range netfields {
|
|
|
|
if field == "container_id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, ok := totalNetworkStatMap[field]
|
|
|
|
if ok {
|
|
|
|
totalNetworkStatMap[field] = totalNetworkStatMap[field].(uint64) + value.(uint64)
|
|
|
|
} else {
|
|
|
|
totalNetworkStatMap[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// totalNetworkStatMap could be empty if container is running with --net=host.
|
|
|
|
if total && len(totalNetworkStatMap) != 0 {
|
2016-01-20 23:21:19 +00:00
|
|
|
nettags := copyTags(tags)
|
2016-07-21 15:50:12 +00:00
|
|
|
nettags["network"] = "total"
|
|
|
|
totalNetworkStatMap["container_id"] = id
|
|
|
|
acc.AddFields("docker_container_net", totalNetworkStatMap, nettags, now)
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 15:50:12 +00:00
|
|
|
gatherBlockIOMetrics(stat, acc, tags, now, id, perDevice, total)
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
func calculateMemPercent(stat *types.StatsJSON) float64 {
|
2016-02-11 10:49:48 +00:00
|
|
|
var memPercent = 0.0
|
|
|
|
if stat.MemoryStats.Limit > 0 {
|
|
|
|
memPercent = float64(stat.MemoryStats.Usage) / float64(stat.MemoryStats.Limit) * 100.0
|
|
|
|
}
|
|
|
|
return memPercent
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
func calculateCPUPercent(stat *types.StatsJSON) float64 {
|
2016-02-11 10:49:48 +00:00
|
|
|
var cpuPercent = 0.0
|
|
|
|
// calculate the change for the cpu and system usage of the container in between readings
|
|
|
|
cpuDelta := float64(stat.CPUStats.CPUUsage.TotalUsage) - float64(stat.PreCPUStats.CPUUsage.TotalUsage)
|
2016-04-02 22:34:34 +00:00
|
|
|
systemDelta := float64(stat.CPUStats.SystemUsage) - float64(stat.PreCPUStats.SystemUsage)
|
2016-02-09 15:20:56 +00:00
|
|
|
|
|
|
|
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
|
|
|
cpuPercent = (cpuDelta / systemDelta) * float64(len(stat.CPUStats.CPUUsage.PercpuUsage)) * 100.0
|
|
|
|
}
|
|
|
|
return cpuPercent
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:21:19 +00:00
|
|
|
func gatherBlockIOMetrics(
|
2016-04-02 22:34:34 +00:00
|
|
|
stat *types.StatsJSON,
|
2016-01-27 21:21:36 +00:00
|
|
|
acc telegraf.Accumulator,
|
2016-01-20 23:21:19 +00:00
|
|
|
tags map[string]string,
|
|
|
|
now time.Time,
|
2016-05-04 12:17:23 +00:00
|
|
|
id string,
|
2016-07-21 15:50:12 +00:00
|
|
|
perDevice bool,
|
|
|
|
total bool,
|
2016-01-20 23:21:19 +00:00
|
|
|
) {
|
|
|
|
blkioStats := stat.BlkioStats
|
|
|
|
// Make a map of devices to their block io stats
|
|
|
|
deviceStatMap := make(map[string]map[string]interface{})
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, metric := range blkioStats.IoServiceBytesRecursive {
|
2016-01-20 23:21:19 +00:00
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
|
|
|
_, ok := deviceStatMap[device]
|
|
|
|
if !ok {
|
|
|
|
deviceStatMap[device] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
field := fmt.Sprintf("io_service_bytes_recursive_%s", strings.ToLower(metric.Op))
|
|
|
|
deviceStatMap[device][field] = metric.Value
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, metric := range blkioStats.IoServicedRecursive {
|
2016-01-20 23:21:19 +00:00
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
|
|
|
_, ok := deviceStatMap[device]
|
|
|
|
if !ok {
|
|
|
|
deviceStatMap[device] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
field := fmt.Sprintf("io_serviced_recursive_%s", strings.ToLower(metric.Op))
|
|
|
|
deviceStatMap[device][field] = metric.Value
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, metric := range blkioStats.IoQueuedRecursive {
|
2016-01-20 23:21:19 +00:00
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
|
|
|
field := fmt.Sprintf("io_queue_recursive_%s", strings.ToLower(metric.Op))
|
|
|
|
deviceStatMap[device][field] = metric.Value
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, metric := range blkioStats.IoServiceTimeRecursive {
|
2016-01-20 23:21:19 +00:00
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
|
|
|
field := fmt.Sprintf("io_service_time_recursive_%s", strings.ToLower(metric.Op))
|
|
|
|
deviceStatMap[device][field] = metric.Value
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, metric := range blkioStats.IoWaitTimeRecursive {
|
2016-01-20 23:21:19 +00:00
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
|
|
|
field := fmt.Sprintf("io_wait_time_%s", strings.ToLower(metric.Op))
|
|
|
|
deviceStatMap[device][field] = metric.Value
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, metric := range blkioStats.IoMergedRecursive {
|
2016-01-20 23:21:19 +00:00
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
|
|
|
field := fmt.Sprintf("io_merged_recursive_%s", strings.ToLower(metric.Op))
|
|
|
|
deviceStatMap[device][field] = metric.Value
|
|
|
|
}
|
|
|
|
|
2016-04-02 22:34:34 +00:00
|
|
|
for _, metric := range blkioStats.IoTimeRecursive {
|
2016-01-20 23:21:19 +00:00
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
2016-04-03 17:40:12 +00:00
|
|
|
deviceStatMap[device]["io_time_recursive"] = metric.Value
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, metric := range blkioStats.SectorsRecursive {
|
|
|
|
device := fmt.Sprintf("%d:%d", metric.Major, metric.Minor)
|
2016-04-03 17:40:12 +00:00
|
|
|
deviceStatMap[device]["sectors_recursive"] = metric.Value
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 15:50:12 +00:00
|
|
|
totalStatMap := make(map[string]interface{})
|
2016-01-20 23:21:19 +00:00
|
|
|
for device, fields := range deviceStatMap {
|
2016-05-04 12:17:23 +00:00
|
|
|
fields["container_id"] = id
|
2016-07-21 15:50:12 +00:00
|
|
|
if perDevice {
|
|
|
|
iotags := copyTags(tags)
|
|
|
|
iotags["device"] = device
|
|
|
|
acc.AddFields("docker_container_blkio", fields, iotags, now)
|
|
|
|
}
|
|
|
|
if total {
|
|
|
|
for field, value := range fields {
|
|
|
|
if field == "container_id" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, ok := totalStatMap[field]
|
|
|
|
if ok {
|
|
|
|
totalStatMap[field] = totalStatMap[field].(uint64) + value.(uint64)
|
|
|
|
} else {
|
|
|
|
totalStatMap[field] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if total {
|
|
|
|
totalStatMap["container_id"] = id
|
|
|
|
iotags := copyTags(tags)
|
|
|
|
iotags["device"] = "total"
|
|
|
|
acc.AddFields("docker_container_blkio", totalStatMap, iotags, now)
|
2016-01-20 23:21:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyTags(in map[string]string) map[string]string {
|
|
|
|
out := make(map[string]string)
|
|
|
|
for k, v := range in {
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func sliceContains(in string, sl []string) bool {
|
|
|
|
for _, str := range sl {
|
|
|
|
if str == in {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:58:14 +00:00
|
|
|
// Parses the human-readable size string into the amount it represents.
|
|
|
|
func parseSize(sizeStr string) (int64, error) {
|
|
|
|
matches := sizeRegex.FindStringSubmatch(sizeStr)
|
|
|
|
if len(matches) != 4 {
|
|
|
|
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := strconv.ParseFloat(matches[1], 64)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uMap := map[string]int64{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
|
|
|
|
unitPrefix := strings.ToLower(matches[3])
|
|
|
|
if mul, ok := uMap[unitPrefix]; ok {
|
|
|
|
size *= float64(mul)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(size), nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:21:19 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("docker", func() telegraf.Input {
|
2016-05-04 20:49:59 +00:00
|
|
|
return &Docker{
|
2016-07-21 15:50:12 +00:00
|
|
|
PerDevice: true,
|
|
|
|
Timeout: internal.Duration{Duration: time.Second * 5},
|
2016-05-04 20:49:59 +00:00
|
|
|
}
|
2016-01-20 23:21:19 +00:00
|
|
|
})
|
|
|
|
}
|