github.com/netdata/go.d.plugin@v0.58.1/modules/docker/docker.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package docker
     4  
     5  import (
     6  	"context"
     7  	_ "embed"
     8  	"time"
     9  
    10  	"github.com/netdata/go.d.plugin/agent/module"
    11  	"github.com/netdata/go.d.plugin/pkg/web"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	docker "github.com/docker/docker/client"
    15  )
    16  
    17  //go:embed "config_schema.json"
    18  var configSchema string
    19  
    20  func init() {
    21  	module.Register("docker", module.Creator{
    22  		JobConfigSchema: configSchema,
    23  		Create:          func() module.Module { return New() },
    24  	})
    25  }
    26  
    27  func New() *Docker {
    28  	return &Docker{
    29  		Config: Config{
    30  			Address:              docker.DefaultDockerHost,
    31  			Timeout:              web.Duration{Duration: time.Second * 5},
    32  			CollectContainerSize: false,
    33  		},
    34  
    35  		charts: summaryCharts.Copy(),
    36  		newClient: func(cfg Config) (dockerClient, error) {
    37  			return docker.NewClientWithOpts(docker.WithHost(cfg.Address))
    38  		},
    39  		containers: make(map[string]bool),
    40  	}
    41  }
    42  
    43  type Config struct {
    44  	Timeout              web.Duration `yaml:"timeout"`
    45  	Address              string       `yaml:"address"`
    46  	CollectContainerSize bool         `yaml:"collect_container_size"`
    47  }
    48  
    49  type (
    50  	Docker struct {
    51  		module.Base
    52  		Config `yaml:",inline"`
    53  
    54  		charts *module.Charts
    55  
    56  		newClient     func(Config) (dockerClient, error)
    57  		client        dockerClient
    58  		verNegotiated bool
    59  
    60  		containers map[string]bool
    61  	}
    62  	dockerClient interface {
    63  		NegotiateAPIVersion(context.Context)
    64  		Info(context.Context) (types.Info, error)
    65  		ImageList(context.Context, types.ImageListOptions) ([]types.ImageSummary, error)
    66  		ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error)
    67  		Close() error
    68  	}
    69  )
    70  
    71  func (d *Docker) Init() bool {
    72  	return true
    73  }
    74  
    75  func (d *Docker) Check() bool {
    76  	return len(d.Collect()) > 0
    77  }
    78  
    79  func (d *Docker) Charts() *module.Charts {
    80  	return d.charts
    81  }
    82  
    83  func (d *Docker) Collect() map[string]int64 {
    84  	mx, err := d.collect()
    85  	if err != nil {
    86  		d.Error(err)
    87  	}
    88  
    89  	if len(mx) == 0 {
    90  		return nil
    91  	}
    92  	return mx
    93  }
    94  
    95  func (d *Docker) Cleanup() {
    96  	if d.client == nil {
    97  		return
    98  	}
    99  	if err := d.client.Close(); err != nil {
   100  		d.Warningf("error on closing docker client: %v", err)
   101  	}
   102  	d.client = nil
   103  }