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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package vsphere
     4  
     5  import (
     6  	_ "embed"
     7  	"sync"
     8  	"time"
     9  
    10  	"github.com/netdata/go.d.plugin/agent/module"
    11  	"github.com/netdata/go.d.plugin/modules/vsphere/match"
    12  	rs "github.com/netdata/go.d.plugin/modules/vsphere/resources"
    13  	"github.com/netdata/go.d.plugin/pkg/web"
    14  
    15  	"github.com/vmware/govmomi/performance"
    16  )
    17  
    18  //go:embed "config_schema.json"
    19  var configSchema string
    20  
    21  func init() {
    22  	module.Register("vsphere", module.Creator{
    23  		JobConfigSchema: configSchema,
    24  		Defaults: module.Defaults{
    25  			UpdateEvery: 20,
    26  		},
    27  		Create: func() module.Module { return New() },
    28  	})
    29  }
    30  
    31  func New() *VSphere {
    32  	config := Config{
    33  		HTTP: web.HTTP{
    34  			Client: web.Client{
    35  				Timeout: web.Duration{Duration: time.Second * 20},
    36  			},
    37  		},
    38  		DiscoveryInterval: web.Duration{Duration: time.Minute * 5},
    39  		HostsInclude:      []string{"/*"},
    40  		VMsInclude:        []string{"/*"},
    41  	}
    42  
    43  	return &VSphere{
    44  		collectionLock:  new(sync.RWMutex),
    45  		Config:          config,
    46  		charts:          &module.Charts{},
    47  		discoveredHosts: make(map[string]int),
    48  		discoveredVMs:   make(map[string]int),
    49  		charted:         make(map[string]bool),
    50  	}
    51  }
    52  
    53  type Config struct {
    54  	web.HTTP          `yaml:",inline"`
    55  	DiscoveryInterval web.Duration       `yaml:"discovery_interval"`
    56  	HostsInclude      match.HostIncludes `yaml:"host_include"`
    57  	VMsInclude        match.VMIncludes   `yaml:"vm_include"`
    58  }
    59  
    60  type (
    61  	VSphere struct {
    62  		module.Base
    63  		UpdateEvery int `yaml:"update_every"`
    64  		Config      `yaml:",inline"`
    65  
    66  		discoverer
    67  		scraper
    68  
    69  		collectionLock  *sync.RWMutex
    70  		resources       *rs.Resources
    71  		discoveryTask   *task
    72  		discoveredHosts map[string]int
    73  		discoveredVMs   map[string]int
    74  		charted         map[string]bool
    75  		charts          *module.Charts
    76  	}
    77  	discoverer interface {
    78  		Discover() (*rs.Resources, error)
    79  	}
    80  	scraper interface {
    81  		ScrapeHosts(rs.Hosts) []performance.EntityMetric
    82  		ScrapeVMs(rs.VMs) []performance.EntityMetric
    83  	}
    84  )
    85  
    86  func (vs *VSphere) Init() bool {
    87  	if err := vs.validateConfig(); err != nil {
    88  		vs.Errorf("error on validating config: %v", err)
    89  		return false
    90  	}
    91  
    92  	vsClient, err := vs.initClient()
    93  	if err != nil {
    94  		vs.Errorf("error on creating vsphere client: %v", err)
    95  		return false
    96  	}
    97  
    98  	err = vs.initDiscoverer(vsClient)
    99  	if err != nil {
   100  		vs.Errorf("error on creating vsphere discoverer: %v", err)
   101  		return false
   102  	}
   103  
   104  	vs.initScraper(vsClient)
   105  
   106  	err = vs.discoverOnce()
   107  	if err != nil {
   108  		vs.Errorf("error on discovering: %v", err)
   109  		return false
   110  	}
   111  
   112  	vs.goDiscovery()
   113  
   114  	return true
   115  }
   116  
   117  func (vs *VSphere) Check() bool {
   118  	return true
   119  }
   120  
   121  func (vs *VSphere) Charts() *module.Charts {
   122  	return vs.charts
   123  }
   124  
   125  func (vs *VSphere) Collect() map[string]int64 {
   126  	mx, err := vs.collect()
   127  	if err != nil {
   128  		vs.Error(err)
   129  	}
   130  
   131  	if len(mx) == 0 {
   132  		return nil
   133  	}
   134  	return mx
   135  }
   136  
   137  func (vs *VSphere) Cleanup() {
   138  	if vs.discoveryTask == nil {
   139  		return
   140  	}
   141  	vs.discoveryTask.stop()
   142  }