2018-09-11 21:53:46 +00:00
|
|
|
package vsphere
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-28 21:24:43 +00:00
|
|
|
"errors"
|
2018-09-11 21:53:46 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-12-28 21:24:43 +00:00
|
|
|
"math/rand"
|
2018-09-11 21:53:46 +00:00
|
|
|
"net/url"
|
2018-11-02 19:05:28 +00:00
|
|
|
"regexp"
|
2018-09-11 21:53:46 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/filter"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/vmware/govmomi/object"
|
|
|
|
"github.com/vmware/govmomi/performance"
|
|
|
|
"github.com/vmware/govmomi/view"
|
|
|
|
"github.com/vmware/govmomi/vim25/mo"
|
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
|
|
|
)
|
|
|
|
|
2018-11-02 19:05:28 +00:00
|
|
|
var isolateLUN = regexp.MustCompile(".*/([^/]+)/?$")
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
const metricLookback = 3 // Number of time periods to look back at for non-realtime metrics
|
|
|
|
|
|
|
|
const rtMetricLookback = 3 // Number of time periods to look back at for realtime metrics
|
|
|
|
|
|
|
|
const maxSampleConst = 10 // Absolute maximim number of samples regardless of period
|
|
|
|
|
|
|
|
const maxMetadataSamples = 100 // Number of resources to sample for metric metadata
|
2018-11-06 22:22:43 +00:00
|
|
|
|
2018-09-11 21:53:46 +00:00
|
|
|
// Endpoint is a high-level representation of a connected vCenter endpoint. It is backed by the lower
|
|
|
|
// level Client type.
|
|
|
|
type Endpoint struct {
|
|
|
|
Parent *VSphere
|
|
|
|
URL *url.URL
|
2019-01-03 19:30:05 +00:00
|
|
|
resourceKinds map[string]*resourceKind
|
2018-11-06 22:22:43 +00:00
|
|
|
hwMarks *TSCache
|
2018-11-02 19:05:28 +00:00
|
|
|
lun2ds map[string]string
|
2018-09-11 21:53:46 +00:00
|
|
|
discoveryTicker *time.Ticker
|
|
|
|
collectMux sync.RWMutex
|
|
|
|
initialized bool
|
|
|
|
clientFactory *ClientFactory
|
|
|
|
busy sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type resourceKind struct {
|
|
|
|
name string
|
|
|
|
pKey string
|
|
|
|
parentTag string
|
|
|
|
enabled bool
|
|
|
|
realTime bool
|
|
|
|
sampling int32
|
|
|
|
objects objectMap
|
|
|
|
filters filter.Filter
|
2018-12-28 21:24:43 +00:00
|
|
|
include []string
|
|
|
|
simple bool
|
|
|
|
metrics performance.MetricList
|
2018-09-11 21:53:46 +00:00
|
|
|
collectInstances bool
|
2018-12-28 21:24:43 +00:00
|
|
|
parent string
|
|
|
|
getObjects func(context.Context, *Client, *Endpoint, *view.ContainerView) (objectMap, error)
|
|
|
|
latestSample time.Time
|
|
|
|
lastColl time.Time
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricEntry struct {
|
|
|
|
tags map[string]string
|
|
|
|
name string
|
|
|
|
ts time.Time
|
|
|
|
fields map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type objectMap map[string]objectRef
|
|
|
|
|
|
|
|
type objectRef struct {
|
|
|
|
name string
|
2018-10-01 23:13:32 +00:00
|
|
|
altID string
|
2018-09-11 21:53:46 +00:00
|
|
|
ref types.ManagedObjectReference
|
|
|
|
parentRef *types.ManagedObjectReference //Pointer because it must be nillable
|
|
|
|
guest string
|
|
|
|
dcname string
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func (e *Endpoint) getParent(obj *objectRef, res *resourceKind) (*objectRef, bool) {
|
|
|
|
if pKind, ok := e.resourceKinds[res.parent]; ok {
|
|
|
|
if p, ok := pKind.objects[obj.parentRef.Value]; ok {
|
|
|
|
return &p, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpoint returns a new connection to a vCenter based on the URL and configuration passed
|
|
|
|
// as parameters.
|
|
|
|
func NewEndpoint(ctx context.Context, parent *VSphere, url *url.URL) (*Endpoint, error) {
|
|
|
|
e := Endpoint{
|
|
|
|
URL: url,
|
|
|
|
Parent: parent,
|
2018-11-06 22:22:43 +00:00
|
|
|
hwMarks: NewTSCache(1 * time.Hour),
|
2018-11-02 19:05:28 +00:00
|
|
|
lun2ds: make(map[string]string),
|
2018-09-11 21:53:46 +00:00
|
|
|
initialized: false,
|
|
|
|
clientFactory: NewClientFactory(ctx, url, parent),
|
|
|
|
}
|
|
|
|
|
2019-01-03 19:30:05 +00:00
|
|
|
e.resourceKinds = map[string]*resourceKind{
|
2018-09-11 21:53:46 +00:00
|
|
|
"datacenter": {
|
|
|
|
name: "datacenter",
|
|
|
|
pKey: "dcname",
|
|
|
|
parentTag: "",
|
|
|
|
enabled: anythingEnabled(parent.DatacenterMetricExclude),
|
|
|
|
realTime: false,
|
|
|
|
sampling: 300,
|
|
|
|
objects: make(objectMap),
|
|
|
|
filters: newFilterOrPanic(parent.DatacenterMetricInclude, parent.DatacenterMetricExclude),
|
2018-12-28 21:24:43 +00:00
|
|
|
simple: isSimple(parent.DatacenterMetricInclude, parent.DatacenterMetricExclude),
|
|
|
|
include: parent.DatacenterMetricInclude,
|
2018-09-11 21:53:46 +00:00
|
|
|
collectInstances: parent.DatacenterInstances,
|
|
|
|
getObjects: getDatacenters,
|
2018-12-28 21:24:43 +00:00
|
|
|
parent: "",
|
2018-09-11 21:53:46 +00:00
|
|
|
},
|
|
|
|
"cluster": {
|
|
|
|
name: "cluster",
|
|
|
|
pKey: "clustername",
|
|
|
|
parentTag: "dcname",
|
|
|
|
enabled: anythingEnabled(parent.ClusterMetricExclude),
|
|
|
|
realTime: false,
|
|
|
|
sampling: 300,
|
|
|
|
objects: make(objectMap),
|
|
|
|
filters: newFilterOrPanic(parent.ClusterMetricInclude, parent.ClusterMetricExclude),
|
2018-12-28 21:24:43 +00:00
|
|
|
simple: isSimple(parent.ClusterMetricInclude, parent.ClusterMetricExclude),
|
|
|
|
include: parent.ClusterMetricInclude,
|
2018-09-11 21:53:46 +00:00
|
|
|
collectInstances: parent.ClusterInstances,
|
|
|
|
getObjects: getClusters,
|
2018-12-28 21:24:43 +00:00
|
|
|
parent: "datacenter",
|
2018-09-11 21:53:46 +00:00
|
|
|
},
|
|
|
|
"host": {
|
|
|
|
name: "host",
|
|
|
|
pKey: "esxhostname",
|
|
|
|
parentTag: "clustername",
|
|
|
|
enabled: anythingEnabled(parent.HostMetricExclude),
|
|
|
|
realTime: true,
|
|
|
|
sampling: 20,
|
|
|
|
objects: make(objectMap),
|
|
|
|
filters: newFilterOrPanic(parent.HostMetricInclude, parent.HostMetricExclude),
|
2018-12-28 21:24:43 +00:00
|
|
|
simple: isSimple(parent.HostMetricInclude, parent.HostMetricExclude),
|
|
|
|
include: parent.HostMetricInclude,
|
2018-09-11 21:53:46 +00:00
|
|
|
collectInstances: parent.HostInstances,
|
|
|
|
getObjects: getHosts,
|
2018-12-28 21:24:43 +00:00
|
|
|
parent: "cluster",
|
2018-09-11 21:53:46 +00:00
|
|
|
},
|
|
|
|
"vm": {
|
|
|
|
name: "vm",
|
|
|
|
pKey: "vmname",
|
|
|
|
parentTag: "esxhostname",
|
|
|
|
enabled: anythingEnabled(parent.VMMetricExclude),
|
|
|
|
realTime: true,
|
|
|
|
sampling: 20,
|
|
|
|
objects: make(objectMap),
|
|
|
|
filters: newFilterOrPanic(parent.VMMetricInclude, parent.VMMetricExclude),
|
2018-12-28 21:24:43 +00:00
|
|
|
simple: isSimple(parent.VMMetricInclude, parent.VMMetricExclude),
|
|
|
|
include: parent.VMMetricInclude,
|
2018-09-11 21:53:46 +00:00
|
|
|
collectInstances: parent.VMInstances,
|
|
|
|
getObjects: getVMs,
|
2018-12-28 21:24:43 +00:00
|
|
|
parent: "host",
|
2018-09-11 21:53:46 +00:00
|
|
|
},
|
|
|
|
"datastore": {
|
|
|
|
name: "datastore",
|
|
|
|
pKey: "dsname",
|
|
|
|
enabled: anythingEnabled(parent.DatastoreMetricExclude),
|
|
|
|
realTime: false,
|
|
|
|
sampling: 300,
|
|
|
|
objects: make(objectMap),
|
|
|
|
filters: newFilterOrPanic(parent.DatastoreMetricInclude, parent.DatastoreMetricExclude),
|
2018-12-28 21:24:43 +00:00
|
|
|
simple: isSimple(parent.DatastoreMetricInclude, parent.DatastoreMetricExclude),
|
|
|
|
include: parent.DatastoreMetricInclude,
|
2018-09-11 21:53:46 +00:00
|
|
|
collectInstances: parent.DatastoreInstances,
|
|
|
|
getObjects: getDatastores,
|
2018-12-28 21:24:43 +00:00
|
|
|
parent: "",
|
2018-09-11 21:53:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start discover and other goodness
|
|
|
|
err := e.init(ctx)
|
|
|
|
|
|
|
|
return &e, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func anythingEnabled(ex []string) bool {
|
|
|
|
for _, s := range ex {
|
|
|
|
if s == "*" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFilterOrPanic(include []string, exclude []string) filter.Filter {
|
|
|
|
f, err := filter.NewIncludeExcludeFilter(include, exclude)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Include/exclude filters are invalid: %s", err))
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func isSimple(include []string, exclude []string) bool {
|
|
|
|
if len(exclude) > 0 || len(include) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, s := range include {
|
|
|
|
if strings.Contains(s, "*") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-11 21:53:46 +00:00
|
|
|
func (e *Endpoint) startDiscovery(ctx context.Context) {
|
|
|
|
e.discoveryTicker = time.NewTicker(e.Parent.ObjectDiscoveryInterval.Duration)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-e.discoveryTicker.C:
|
|
|
|
err := e.discover(ctx)
|
|
|
|
if err != nil && err != context.Canceled {
|
|
|
|
log.Printf("E! [input.vsphere]: Error in discovery for %s: %v", e.URL.Host, err)
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("D! [input.vsphere]: Exiting discovery goroutine for %s", e.URL.Host)
|
|
|
|
e.discoveryTicker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) initalDiscovery(ctx context.Context) {
|
|
|
|
err := e.discover(ctx)
|
|
|
|
if err != nil && err != context.Canceled {
|
|
|
|
log.Printf("E! [input.vsphere]: Error in discovery for %s: %v", e.URL.Host, err)
|
|
|
|
}
|
|
|
|
e.startDiscovery(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) init(ctx context.Context) error {
|
|
|
|
|
|
|
|
if e.Parent.ObjectDiscoveryInterval.Duration > 0 {
|
|
|
|
|
|
|
|
// Run an initial discovery. If force_discovery_on_init isn't set, we kick it off as a
|
|
|
|
// goroutine without waiting for it. This will probably cause us to report an empty
|
|
|
|
// dataset on the first collection, but it solves the issue of the first collection timing out.
|
|
|
|
if e.Parent.ForceDiscoverOnInit {
|
|
|
|
log.Printf("D! [input.vsphere]: Running initial discovery and waiting for it to finish")
|
|
|
|
e.initalDiscovery(ctx)
|
|
|
|
} else {
|
|
|
|
// Otherwise, just run it in the background. We'll probably have an incomplete first metric
|
|
|
|
// collection this way.
|
2018-12-28 21:24:43 +00:00
|
|
|
go func() {
|
|
|
|
e.initalDiscovery(ctx)
|
|
|
|
}()
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e.initialized = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) getMetricNameMap(ctx context.Context) (map[int32]string, error) {
|
|
|
|
client, err := e.clientFactory.GetClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
mn, err := client.CounterInfoByName(ctx)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
names := make(map[int32]string)
|
|
|
|
for name, m := range mn {
|
|
|
|
names[m.Key] = name
|
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func (e *Endpoint) getMetadata(ctx context.Context, obj objectRef, sampling int32) (performance.MetricList, error) {
|
2018-09-11 21:53:46 +00:00
|
|
|
client, err := e.clientFactory.GetClient(ctx)
|
|
|
|
if err != nil {
|
2018-12-28 21:24:43 +00:00
|
|
|
return nil, err
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 20:08:09 +00:00
|
|
|
ctx1, cancel1 := context.WithTimeout(ctx, e.Parent.Timeout.Duration)
|
|
|
|
defer cancel1()
|
2018-12-28 21:24:43 +00:00
|
|
|
metrics, err := client.Perf.AvailableMetric(ctx1, obj.ref.Reference(), sampling)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
return metrics, nil
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) getDatacenterName(ctx context.Context, client *Client, cache map[string]string, r types.ManagedObjectReference) string {
|
|
|
|
path := make([]string, 0)
|
|
|
|
returnVal := ""
|
|
|
|
here := r
|
|
|
|
for {
|
|
|
|
if name, ok := cache[here.Reference().String()]; ok {
|
|
|
|
// Populate cache for the entire chain of objects leading here.
|
|
|
|
returnVal = name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
path = append(path, here.Reference().String())
|
|
|
|
o := object.NewCommon(client.Client.Client, r)
|
|
|
|
var result mo.ManagedEntity
|
2018-10-11 20:08:09 +00:00
|
|
|
ctx1, cancel1 := context.WithTimeout(ctx, e.Parent.Timeout.Duration)
|
|
|
|
defer cancel1()
|
|
|
|
err := o.Properties(ctx1, here, []string{"parent", "name"}, &result)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("W! [input.vsphere]: Error while resolving parent. Assuming no parent exists. Error: %s", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if result.Reference().Type == "Datacenter" {
|
|
|
|
// Populate cache for the entire chain of objects leading here.
|
|
|
|
returnVal = result.Name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if result.Parent == nil {
|
|
|
|
log.Printf("D! [input.vsphere]: No parent found for %s (ascending from %s)", here.Reference(), r.Reference())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
here = result.Parent.Reference()
|
|
|
|
}
|
|
|
|
for _, s := range path {
|
|
|
|
cache[s] = returnVal
|
|
|
|
}
|
|
|
|
return returnVal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) discover(ctx context.Context) error {
|
|
|
|
e.busy.Lock()
|
|
|
|
defer e.busy.Unlock()
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
metricNames, err := e.getMetricNameMap(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sw := NewStopwatch("discover", e.URL.Host)
|
|
|
|
|
|
|
|
client, err := e.clientFactory.GetClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("D! [input.vsphere]: Discover new objects for %s", e.URL.Host)
|
|
|
|
resourceKinds := make(map[string]resourceKind)
|
|
|
|
dcNameCache := make(map[string]string)
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
numRes := int64(0)
|
|
|
|
|
2018-09-11 21:53:46 +00:00
|
|
|
// Populate resource objects, and endpoint instance info.
|
2019-01-03 19:30:05 +00:00
|
|
|
newObjects := make(map[string]objectMap)
|
2018-09-11 21:53:46 +00:00
|
|
|
for k, res := range e.resourceKinds {
|
|
|
|
log.Printf("D! [input.vsphere] Discovering resources for %s", res.name)
|
2018-11-06 22:22:43 +00:00
|
|
|
// Need to do this for all resource types even if they are not enabled
|
|
|
|
if res.enabled || k != "vm" {
|
2018-12-28 21:24:43 +00:00
|
|
|
objects, err := res.getObjects(ctx, client, e, client.Root)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in datacenter names where available (no need to do it for Datacenters)
|
|
|
|
if res.name != "Datacenter" {
|
|
|
|
for k, obj := range objects {
|
|
|
|
if obj.parentRef != nil {
|
|
|
|
obj.dcname = e.getDatacenterName(ctx, client, dcNameCache, *obj.parentRef)
|
|
|
|
objects[k] = obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
// No need to collect metric metadata if resource type is not enabled
|
|
|
|
if res.enabled {
|
|
|
|
if res.simple {
|
2019-01-03 19:30:05 +00:00
|
|
|
e.simpleMetadataSelect(ctx, client, res)
|
2018-12-28 21:24:43 +00:00
|
|
|
} else {
|
2019-01-03 19:30:05 +00:00
|
|
|
e.complexMetadataSelect(ctx, res, objects, metricNames)
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
}
|
2019-01-03 19:30:05 +00:00
|
|
|
newObjects[k] = objects
|
2018-12-28 21:24:43 +00:00
|
|
|
|
|
|
|
SendInternalCounterWithTags("discovered_objects", e.URL.Host, map[string]string{"type": res.name}, int64(len(objects)))
|
|
|
|
numRes += int64(len(objects))
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 19:05:28 +00:00
|
|
|
// Build lun2ds map
|
|
|
|
dss := resourceKinds["datastore"]
|
|
|
|
l2d := make(map[string]string)
|
|
|
|
for _, ds := range dss.objects {
|
|
|
|
url := ds.altID
|
|
|
|
m := isolateLUN.FindStringSubmatch(url)
|
|
|
|
if m != nil {
|
|
|
|
l2d[m[1]] = ds.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 21:53:46 +00:00
|
|
|
// Atomically swap maps
|
|
|
|
e.collectMux.Lock()
|
|
|
|
defer e.collectMux.Unlock()
|
|
|
|
|
2019-01-03 19:30:05 +00:00
|
|
|
for k, v := range newObjects {
|
|
|
|
e.resourceKinds[k].objects = v
|
|
|
|
}
|
2018-11-02 19:05:28 +00:00
|
|
|
e.lun2ds = l2d
|
2018-09-11 21:53:46 +00:00
|
|
|
|
|
|
|
sw.Stop()
|
2018-12-28 21:24:43 +00:00
|
|
|
SendInternalCounterWithTags("discovered_objects", e.URL.Host, map[string]string{"type": "instance-total"}, numRes)
|
2018-09-11 21:53:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func (e *Endpoint) simpleMetadataSelect(ctx context.Context, client *Client, res *resourceKind) {
|
|
|
|
log.Printf("D! [input.vsphere] Using fast metric metadata selection for %s", res.name)
|
|
|
|
m, err := client.CounterInfoByName(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("E! [input.vsphere]: Error while getting metric metadata. Discovery will be incomplete. Error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.metrics = make(performance.MetricList, 0, len(res.include))
|
|
|
|
for _, s := range res.include {
|
|
|
|
if pci, ok := m[s]; ok {
|
|
|
|
cnt := types.PerfMetricId{
|
|
|
|
CounterId: pci.Key,
|
|
|
|
}
|
|
|
|
if res.collectInstances {
|
|
|
|
cnt.Instance = "*"
|
|
|
|
} else {
|
|
|
|
cnt.Instance = ""
|
|
|
|
}
|
|
|
|
res.metrics = append(res.metrics, cnt)
|
|
|
|
} else {
|
|
|
|
log.Printf("W! [input.vsphere] Metric name %s is unknown. Will not be collected", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) complexMetadataSelect(ctx context.Context, res *resourceKind, objects objectMap, metricNames map[int32]string) {
|
|
|
|
// We're only going to get metadata from maxMetadataSamples resources. If we have
|
|
|
|
// more resources than that, we pick maxMetadataSamples samples at random.
|
|
|
|
sampledObjects := make([]objectRef, len(objects))
|
|
|
|
i := 0
|
|
|
|
for _, obj := range objects {
|
|
|
|
sampledObjects[i] = obj
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
n := len(sampledObjects)
|
|
|
|
if n > maxMetadataSamples {
|
|
|
|
// Shuffle samples into the maxMetadatSamples positions
|
|
|
|
for i := 0; i < maxMetadataSamples; i++ {
|
|
|
|
j := int(rand.Int31n(int32(i + 1)))
|
|
|
|
t := sampledObjects[i]
|
|
|
|
sampledObjects[i] = sampledObjects[j]
|
|
|
|
sampledObjects[j] = t
|
|
|
|
}
|
|
|
|
sampledObjects = sampledObjects[0:maxMetadataSamples]
|
|
|
|
}
|
|
|
|
|
|
|
|
instInfoMux := sync.Mutex{}
|
|
|
|
te := NewThrottledExecutor(e.Parent.DiscoverConcurrency)
|
|
|
|
for _, obj := range sampledObjects {
|
|
|
|
func(obj objectRef) {
|
|
|
|
te.Run(ctx, func() {
|
|
|
|
metrics, err := e.getMetadata(ctx, obj, res.sampling)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("E! [input.vsphere]: Error while getting metric metadata. Discovery will be incomplete. Error: %s", err)
|
|
|
|
}
|
|
|
|
mMap := make(map[string]types.PerfMetricId)
|
|
|
|
for _, m := range metrics {
|
|
|
|
if m.Instance != "" && res.collectInstances {
|
|
|
|
m.Instance = "*"
|
|
|
|
} else {
|
|
|
|
m.Instance = ""
|
|
|
|
}
|
|
|
|
if res.filters.Match(metricNames[m.CounterId]) {
|
|
|
|
mMap[strconv.Itoa(int(m.CounterId))+"|"+m.Instance] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("D! [input.vsphere] Found %d metrics for %s", len(mMap), obj.name)
|
|
|
|
instInfoMux.Lock()
|
|
|
|
defer instInfoMux.Unlock()
|
|
|
|
if len(mMap) > len(res.metrics) {
|
|
|
|
res.metrics = make(performance.MetricList, len(mMap))
|
|
|
|
i := 0
|
|
|
|
for _, m := range mMap {
|
|
|
|
res.metrics[i] = m
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(obj)
|
|
|
|
}
|
|
|
|
te.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDatacenters(ctx context.Context, client *Client, e *Endpoint, root *view.ContainerView) (objectMap, error) {
|
2018-09-11 21:53:46 +00:00
|
|
|
var resources []mo.Datacenter
|
2018-12-28 21:24:43 +00:00
|
|
|
err := client.ListResources(ctx, root, []string{"Datacenter"}, []string{"name", "parent"}, &resources)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := make(objectMap, len(resources))
|
|
|
|
for _, r := range resources {
|
|
|
|
m[r.ExtensibleManagedObject.Reference().Value] = objectRef{
|
|
|
|
name: r.Name, ref: r.ExtensibleManagedObject.Reference(), parentRef: r.Parent, dcname: r.Name}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func getClusters(ctx context.Context, client *Client, e *Endpoint, root *view.ContainerView) (objectMap, error) {
|
2018-09-11 21:53:46 +00:00
|
|
|
var resources []mo.ClusterComputeResource
|
2018-12-28 21:24:43 +00:00
|
|
|
err := client.ListResources(ctx, root, []string{"ClusterComputeResource"}, []string{"name", "parent"}, &resources)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cache := make(map[string]*types.ManagedObjectReference)
|
|
|
|
m := make(objectMap, len(resources))
|
|
|
|
for _, r := range resources {
|
|
|
|
// We're not interested in the immediate parent (a folder), but the data center.
|
|
|
|
p, ok := cache[r.Parent.Value]
|
|
|
|
if !ok {
|
|
|
|
o := object.NewFolder(root.Client(), *r.Parent)
|
|
|
|
var folder mo.Folder
|
2018-10-11 20:08:09 +00:00
|
|
|
ctx2, cancel2 := context.WithTimeout(ctx, e.Parent.Timeout.Duration)
|
|
|
|
defer cancel2()
|
|
|
|
err := o.Properties(ctx2, *r.Parent, []string{"parent"}, &folder)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("W! [input.vsphere] Error while getting folder parent: %e", err)
|
|
|
|
p = nil
|
|
|
|
} else {
|
|
|
|
pp := folder.Parent.Reference()
|
|
|
|
p = &pp
|
|
|
|
cache[r.Parent.Value] = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m[r.ExtensibleManagedObject.Reference().Value] = objectRef{
|
|
|
|
name: r.Name, ref: r.ExtensibleManagedObject.Reference(), parentRef: p}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func getHosts(ctx context.Context, client *Client, e *Endpoint, root *view.ContainerView) (objectMap, error) {
|
2018-09-11 21:53:46 +00:00
|
|
|
var resources []mo.HostSystem
|
2018-12-28 21:24:43 +00:00
|
|
|
err := client.ListResources(ctx, root, []string{"HostSystem"}, []string{"name", "parent"}, &resources)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := make(objectMap)
|
|
|
|
for _, r := range resources {
|
|
|
|
m[r.ExtensibleManagedObject.Reference().Value] = objectRef{
|
|
|
|
name: r.Name, ref: r.ExtensibleManagedObject.Reference(), parentRef: r.Parent}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func getVMs(ctx context.Context, client *Client, e *Endpoint, root *view.ContainerView) (objectMap, error) {
|
2018-09-11 21:53:46 +00:00
|
|
|
var resources []mo.VirtualMachine
|
2018-12-28 21:24:43 +00:00
|
|
|
err := client.ListResources(ctx, root, []string{"VirtualMachine"}, []string{"name", "runtime.host", "runtime.powerState", "config.guestId", "config.uuid"}, &resources)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := make(objectMap)
|
|
|
|
for _, r := range resources {
|
2018-12-28 21:24:43 +00:00
|
|
|
if r.Runtime.PowerState != "poweredOn" {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-01 23:13:32 +00:00
|
|
|
guest := "unknown"
|
|
|
|
uuid := ""
|
2018-09-11 21:53:46 +00:00
|
|
|
// Sometimes Config is unknown and returns a nil pointer
|
|
|
|
//
|
|
|
|
if r.Config != nil {
|
|
|
|
guest = cleanGuestID(r.Config.GuestId)
|
2018-10-01 23:13:32 +00:00
|
|
|
uuid = r.Config.Uuid
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
m[r.ExtensibleManagedObject.Reference().Value] = objectRef{
|
2018-10-01 23:13:32 +00:00
|
|
|
name: r.Name, ref: r.ExtensibleManagedObject.Reference(), parentRef: r.Runtime.Host, guest: guest, altID: uuid}
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func getDatastores(ctx context.Context, client *Client, e *Endpoint, root *view.ContainerView) (objectMap, error) {
|
2018-09-11 21:53:46 +00:00
|
|
|
var resources []mo.Datastore
|
2018-12-28 21:24:43 +00:00
|
|
|
err := client.ListResources(ctx, root, []string{"Datastore"}, []string{"name", "parent", "info"}, &resources)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := make(objectMap)
|
|
|
|
for _, r := range resources {
|
2018-11-02 19:05:28 +00:00
|
|
|
url := ""
|
|
|
|
if r.Info != nil {
|
|
|
|
info := r.Info.GetDatastoreInfo()
|
|
|
|
if info != nil {
|
|
|
|
url = info.Url
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 21:53:46 +00:00
|
|
|
m[r.ExtensibleManagedObject.Reference().Value] = objectRef{
|
2018-11-02 19:05:28 +00:00
|
|
|
name: r.Name, ref: r.ExtensibleManagedObject.Reference(), parentRef: r.Parent, altID: url}
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts down an Endpoint and releases any resources associated with it.
|
|
|
|
func (e *Endpoint) Close() {
|
|
|
|
e.clientFactory.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect runs a round of data collections as specified in the configuration.
|
|
|
|
func (e *Endpoint) Collect(ctx context.Context, acc telegraf.Accumulator) error {
|
2018-12-28 21:24:43 +00:00
|
|
|
|
2018-09-11 21:53:46 +00:00
|
|
|
// If we never managed to do a discovery, collection will be a no-op. Therefore,
|
|
|
|
// we need to check that a connection is available, or the collection will
|
|
|
|
// silently fail.
|
|
|
|
if _, err := e.clientFactory.GetClient(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.collectMux.RLock()
|
|
|
|
defer e.collectMux.RUnlock()
|
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If discovery interval is disabled (0), discover on each collection cycle
|
|
|
|
if e.Parent.ObjectDiscoveryInterval.Duration == 0 {
|
|
|
|
err := e.discover(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
var wg sync.WaitGroup
|
2018-09-11 21:53:46 +00:00
|
|
|
for k, res := range e.resourceKinds {
|
|
|
|
if res.enabled {
|
2018-12-28 21:24:43 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(k string) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := e.collectResource(ctx, k, acc)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(err)
|
|
|
|
}
|
|
|
|
}(k)
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
wg.Wait()
|
2018-11-06 22:22:43 +00:00
|
|
|
|
|
|
|
// Purge old timestamps from the cache
|
|
|
|
e.hwMarks.Purge()
|
2018-09-11 21:53:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
// Workaround to make sure pqs is a copy of the loop variable and won't change.
|
|
|
|
func submitChunkJob(ctx context.Context, te *ThrottledExecutor, job func([]types.PerfQuerySpec), pqs []types.PerfQuerySpec) {
|
|
|
|
te.Run(ctx, func() {
|
|
|
|
job(pqs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) chunkify(ctx context.Context, res *resourceKind, now time.Time, latest time.Time, acc telegraf.Accumulator, job func([]types.PerfQuerySpec)) {
|
|
|
|
te := NewThrottledExecutor(e.Parent.CollectConcurrency)
|
2018-11-06 22:22:43 +00:00
|
|
|
maxMetrics := e.Parent.MaxQueryMetrics
|
|
|
|
if maxMetrics < 1 {
|
|
|
|
maxMetrics = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Workaround for vCenter weirdness. Cluster metrics seem to count multiple times
|
|
|
|
// when checking query size, so keep it at a low value.
|
|
|
|
// Revisit this when we better understand the reason why vCenter counts it this way!
|
|
|
|
if res.name == "cluster" && maxMetrics > 10 {
|
|
|
|
maxMetrics = 10
|
|
|
|
}
|
2018-09-11 21:53:46 +00:00
|
|
|
pqs := make([]types.PerfQuerySpec, 0, e.Parent.MaxQueryObjects)
|
|
|
|
metrics := 0
|
|
|
|
total := 0
|
|
|
|
nRes := 0
|
|
|
|
for _, object := range res.objects {
|
2018-12-28 21:24:43 +00:00
|
|
|
mr := len(res.metrics)
|
2018-09-11 21:53:46 +00:00
|
|
|
for mr > 0 {
|
|
|
|
mc := mr
|
2018-11-06 22:22:43 +00:00
|
|
|
headroom := maxMetrics - metrics
|
2018-09-11 21:53:46 +00:00
|
|
|
if !res.realTime && mc > headroom { // Metric query limit only applies to non-realtime metrics
|
|
|
|
mc = headroom
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
fm := len(res.metrics) - mr
|
2018-09-11 21:53:46 +00:00
|
|
|
pq := types.PerfQuerySpec{
|
|
|
|
Entity: object.ref,
|
2018-12-28 21:24:43 +00:00
|
|
|
MaxSample: maxSampleConst,
|
|
|
|
MetricId: res.metrics[fm : fm+mc],
|
2018-09-11 21:53:46 +00:00
|
|
|
IntervalId: res.sampling,
|
2018-11-06 22:22:43 +00:00
|
|
|
Format: "normal",
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
start, ok := e.hwMarks.Get(object.ref.Value)
|
|
|
|
if !ok {
|
|
|
|
// Look back 3 sampling periods by default
|
|
|
|
start = latest.Add(time.Duration(-res.sampling) * time.Second * (metricLookback - 1))
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
pq.StartTime = &start
|
|
|
|
pq.EndTime = &now
|
2018-09-11 21:53:46 +00:00
|
|
|
|
|
|
|
pqs = append(pqs, pq)
|
|
|
|
mr -= mc
|
|
|
|
metrics += mc
|
|
|
|
|
|
|
|
// We need to dump the current chunk of metrics for one of two reasons:
|
|
|
|
// 1) We filled up the metric quota while processing the current resource
|
|
|
|
// 2) We are at the last resource and have no more data to process.
|
2018-12-28 21:24:43 +00:00
|
|
|
// 3) The query contains more than 100,000 individual metrics
|
|
|
|
if mr > 0 || nRes >= e.Parent.MaxQueryObjects || len(pqs) > 100000 {
|
2018-11-06 22:22:43 +00:00
|
|
|
log.Printf("D! [input.vsphere]: Queueing query: %d objects, %d metrics (%d remaining) of type %s for %s. Processed objects: %d. Total objects %d",
|
2018-09-11 21:53:46 +00:00
|
|
|
len(pqs), metrics, mr, res.name, e.URL.Host, total+1, len(res.objects))
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
// Don't send work items if the context has been cancelled.
|
2018-09-11 21:53:46 +00:00
|
|
|
if ctx.Err() == context.Canceled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
// Run collection job
|
|
|
|
submitChunkJob(ctx, te, job, pqs)
|
2018-09-11 21:53:46 +00:00
|
|
|
pqs = make([]types.PerfQuerySpec, 0, e.Parent.MaxQueryObjects)
|
|
|
|
metrics = 0
|
|
|
|
nRes = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total++
|
|
|
|
nRes++
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
// Handle final partially filled chunk
|
2018-09-11 21:53:46 +00:00
|
|
|
if len(pqs) > 0 {
|
2018-12-28 21:24:43 +00:00
|
|
|
// Run collection job
|
2018-11-06 22:22:43 +00:00
|
|
|
log.Printf("D! [input.vsphere]: Queuing query: %d objects, %d metrics (0 remaining) of type %s for %s. Total objects %d (final chunk)",
|
|
|
|
len(pqs), metrics, res.name, e.URL.Host, len(res.objects))
|
2018-12-28 21:24:43 +00:00
|
|
|
submitChunkJob(ctx, te, job, pqs)
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
|
|
|
|
// Wait for background collection to finish
|
|
|
|
te.Wait()
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc telegraf.Accumulator) error {
|
|
|
|
res := e.resourceKinds[resourceType]
|
2018-10-03 19:02:06 +00:00
|
|
|
client, err := e.clientFactory.GetClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
now, err := client.GetServerTime(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
|
|
|
|
// Estimate the interval at which we're invoked. Use local time (not server time)
|
|
|
|
// since this is about how we got invoked locally.
|
|
|
|
localNow := time.Now()
|
|
|
|
estInterval := time.Duration(time.Minute)
|
|
|
|
if !res.lastColl.IsZero() {
|
|
|
|
estInterval = localNow.Sub(res.lastColl).Truncate(time.Duration(res.sampling) * time.Second)
|
|
|
|
}
|
|
|
|
log.Printf("D! [inputs.vsphere] Interval estimated to %s", estInterval)
|
|
|
|
|
|
|
|
latest := res.latestSample
|
|
|
|
if !latest.IsZero() {
|
2018-10-03 19:02:06 +00:00
|
|
|
elapsed := now.Sub(latest).Seconds() + 5.0 // Allow 5 second jitter.
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("D! [inputs.vsphere]: Latest: %s, elapsed: %f, resource: %s", latest, elapsed, resourceType)
|
2018-09-11 21:53:46 +00:00
|
|
|
if !res.realTime && elapsed < float64(res.sampling) {
|
2018-12-28 21:24:43 +00:00
|
|
|
// No new data would be available. We're outta here!
|
|
|
|
log.Printf("D! [inputs.vsphere]: Sampling period for %s of %d has not elapsed on %s",
|
2018-09-11 21:53:46 +00:00
|
|
|
resourceType, res.sampling, e.URL.Host)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-03 19:02:06 +00:00
|
|
|
latest = now.Add(time.Duration(-res.sampling) * time.Second)
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internalTags := map[string]string{"resourcetype": resourceType}
|
|
|
|
sw := NewStopwatchWithTags("gather_duration", e.URL.Host, internalTags)
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("D! [inputs.vsphere]: Collecting metrics for %d objects of type %s for %s",
|
2018-09-11 21:53:46 +00:00
|
|
|
len(res.objects), resourceType, e.URL.Host)
|
|
|
|
|
|
|
|
count := int64(0)
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
var tsMux sync.Mutex
|
|
|
|
latestSample := time.Time{}
|
2018-09-11 21:53:46 +00:00
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
// Divide workload into chunks and process them concurrently
|
2019-01-03 19:30:05 +00:00
|
|
|
e.chunkify(ctx, res, now, latest, acc,
|
2018-12-28 21:24:43 +00:00
|
|
|
func(chunk []types.PerfQuerySpec) {
|
2019-01-03 19:30:05 +00:00
|
|
|
n, localLatest, err := e.collectChunk(ctx, chunk, res, acc, now, estInterval)
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("D! [inputs.vsphere] CollectChunk for %s returned %d metrics", resourceType, n)
|
|
|
|
if err != nil {
|
|
|
|
acc.AddError(errors.New("While collecting " + res.name + ": " + err.Error()))
|
|
|
|
}
|
|
|
|
atomic.AddInt64(&count, int64(n))
|
|
|
|
tsMux.Lock()
|
|
|
|
defer tsMux.Unlock()
|
|
|
|
if localLatest.After(latestSample) && !localLatest.IsZero() {
|
|
|
|
latestSample = localLatest
|
|
|
|
}
|
|
|
|
})
|
2018-09-11 21:53:46 +00:00
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("D! [inputs.vsphere] Latest sample for %s set to %s", resourceType, latestSample)
|
|
|
|
if !latestSample.IsZero() {
|
|
|
|
res.latestSample = latestSample
|
|
|
|
}
|
2018-09-11 21:53:46 +00:00
|
|
|
sw.Stop()
|
|
|
|
SendInternalCounterWithTags("gather_count", e.URL.Host, internalTags, count)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
func alignSamples(info []types.PerfSampleInfo, values []int64, interval time.Duration) ([]types.PerfSampleInfo, []float64) {
|
|
|
|
rInfo := make([]types.PerfSampleInfo, 0, len(info))
|
|
|
|
rValues := make([]float64, 0, len(values))
|
|
|
|
bi := 1.0
|
|
|
|
var lastBucket time.Time
|
|
|
|
for idx := range info {
|
|
|
|
// According to the docs, SampleInfo and Value should have the same length, but we've seen corrupted
|
|
|
|
// data coming back with missing values. Take care of that gracefully!
|
|
|
|
if idx >= len(values) {
|
|
|
|
log.Printf("D! [inputs.vsphere] len(SampleInfo)>len(Value) %d > %d", len(info), len(values))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
v := float64(values[idx])
|
|
|
|
if v < 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ts := info[idx].Timestamp
|
|
|
|
roundedTs := ts.Truncate(interval)
|
|
|
|
|
|
|
|
// Are we still working on the same bucket?
|
|
|
|
if roundedTs == lastBucket {
|
|
|
|
bi++
|
|
|
|
p := len(rValues) - 1
|
|
|
|
rValues[p] = ((bi-1)/bi)*float64(rValues[p]) + v/bi
|
|
|
|
} else {
|
|
|
|
rValues = append(rValues, v)
|
|
|
|
roundedInfo := types.PerfSampleInfo{
|
|
|
|
Timestamp: roundedTs,
|
|
|
|
Interval: info[idx].Interval,
|
|
|
|
}
|
|
|
|
rInfo = append(rInfo, roundedInfo)
|
|
|
|
bi = 1.0
|
|
|
|
lastBucket = roundedTs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//log.Printf("D! [inputs.vsphere] Aligned samples: %d collapsed into %d", len(info), len(rInfo))
|
|
|
|
return rInfo, rValues
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec, res *resourceKind, acc telegraf.Accumulator, now time.Time, interval time.Duration) (int, time.Time, error) {
|
|
|
|
log.Printf("D! [inputs.vsphere] Query for %s has %d QuerySpecs", res.name, len(pqs))
|
|
|
|
latestSample := time.Time{}
|
2018-09-11 21:53:46 +00:00
|
|
|
count := 0
|
2018-12-28 21:24:43 +00:00
|
|
|
resourceType := res.name
|
2018-09-11 21:53:46 +00:00
|
|
|
prefix := "vsphere" + e.Parent.Separator + resourceType
|
|
|
|
|
|
|
|
client, err := e.clientFactory.GetClient(ctx)
|
|
|
|
if err != nil {
|
2018-12-28 21:24:43 +00:00
|
|
|
return count, latestSample, err
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
metricInfo, err := client.CounterInfoByName(ctx)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
2018-12-28 21:24:43 +00:00
|
|
|
return count, latestSample, err
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
ems, err := client.QueryMetrics(ctx, pqs)
|
2018-09-11 21:53:46 +00:00
|
|
|
if err != nil {
|
2018-12-28 21:24:43 +00:00
|
|
|
return count, latestSample, err
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("D! [inputs.vsphere] Query for %s returned metrics for %d objects", resourceType, len(ems))
|
2018-09-11 21:53:46 +00:00
|
|
|
|
|
|
|
// Iterate through results
|
|
|
|
for _, em := range ems {
|
|
|
|
moid := em.Entity.Reference().Value
|
2018-12-28 21:24:43 +00:00
|
|
|
instInfo, found := res.objects[moid]
|
2018-09-11 21:53:46 +00:00
|
|
|
if !found {
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("E! [inputs.vsphere]: MOID %s not found in cache. Skipping! (This should not happen!)", moid)
|
2018-09-11 21:53:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
buckets := make(map[string]metricEntry)
|
|
|
|
for _, v := range em.Value {
|
|
|
|
name := v.Name
|
|
|
|
t := map[string]string{
|
|
|
|
"vcenter": e.URL.Host,
|
|
|
|
"source": instInfo.name,
|
|
|
|
"moid": moid,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate tags
|
|
|
|
objectRef, ok := res.objects[moid]
|
|
|
|
if !ok {
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("E! [inputs.vsphere]: MOID %s not found in cache. Skipping", moid)
|
2018-09-11 21:53:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
e.populateTags(&objectRef, resourceType, res, t, &v)
|
2018-09-11 21:53:46 +00:00
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
nValues := 0
|
|
|
|
alignedInfo, alignedValues := alignSamples(em.SampleInfo, v.Value, interval) // TODO: Estimate interval
|
2018-09-11 21:53:46 +00:00
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
for idx, sample := range alignedInfo {
|
|
|
|
// According to the docs, SampleInfo and Value should have the same length, but we've seen corrupted
|
|
|
|
// data coming back with missing values. Take care of that gracefully!
|
|
|
|
if idx >= len(alignedValues) {
|
|
|
|
log.Printf("D! [inputs.vsphere] len(SampleInfo)>len(Value) %d > %d", len(alignedInfo), len(alignedValues))
|
|
|
|
break
|
2018-11-06 22:22:43 +00:00
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
ts := sample.Timestamp
|
|
|
|
if ts.After(latestSample) {
|
|
|
|
latestSample = ts
|
|
|
|
}
|
|
|
|
nValues++
|
2018-11-06 22:22:43 +00:00
|
|
|
|
2018-09-11 21:53:46 +00:00
|
|
|
// Organize the metrics into a bucket per measurement.
|
|
|
|
mn, fn := e.makeMetricIdentifier(prefix, name)
|
|
|
|
bKey := mn + " " + v.Instance + " " + strconv.FormatInt(ts.UnixNano(), 10)
|
|
|
|
bucket, found := buckets[bKey]
|
|
|
|
if !found {
|
|
|
|
bucket = metricEntry{name: mn, ts: ts, fields: make(map[string]interface{}), tags: t}
|
|
|
|
buckets[bKey] = bucket
|
|
|
|
}
|
|
|
|
|
|
|
|
// Percentage values must be scaled down by 100.
|
|
|
|
info, ok := metricInfo[name]
|
|
|
|
if !ok {
|
2018-12-28 21:24:43 +00:00
|
|
|
log.Printf("E! [inputs.vsphere]: Could not determine unit for %s. Skipping", name)
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
v := alignedValues[idx]
|
2018-09-11 21:53:46 +00:00
|
|
|
if info.UnitInfo.GetElementDescription().Key == "percent" {
|
2018-12-28 21:24:43 +00:00
|
|
|
bucket.fields[fn] = float64(v) / 100.0
|
2018-09-11 21:53:46 +00:00
|
|
|
} else {
|
2018-12-28 21:24:43 +00:00
|
|
|
bucket.fields[fn] = v
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
count++
|
2018-11-06 22:22:43 +00:00
|
|
|
|
2018-12-28 21:24:43 +00:00
|
|
|
// Update highwater marks
|
|
|
|
e.hwMarks.Put(moid, ts)
|
|
|
|
}
|
|
|
|
if nValues == 0 {
|
|
|
|
log.Printf("D! [inputs.vsphere]: Missing value for: %s, %s", name, objectRef.name)
|
|
|
|
continue
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We've iterated through all the metrics and collected buckets for each
|
|
|
|
// measurement name. Now emit them!
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
acc.AddFields(bucket.name, bucket.fields, bucket.tags, bucket.ts)
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
return count, latestSample, nil
|
2018-09-11 21:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) populateTags(objectRef *objectRef, resourceType string, resource *resourceKind, t map[string]string, v *performance.MetricSeries) {
|
|
|
|
// Map name of object.
|
|
|
|
if resource.pKey != "" {
|
|
|
|
t[resource.pKey] = objectRef.name
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:13:32 +00:00
|
|
|
if resourceType == "vm" && objectRef.altID != "" {
|
|
|
|
t["uuid"] = objectRef.altID
|
|
|
|
}
|
|
|
|
|
2018-09-11 21:53:46 +00:00
|
|
|
// Map parent reference
|
2018-12-28 21:24:43 +00:00
|
|
|
parent, found := e.getParent(objectRef, resource)
|
2018-09-11 21:53:46 +00:00
|
|
|
if found {
|
|
|
|
t[resource.parentTag] = parent.name
|
|
|
|
if resourceType == "vm" {
|
|
|
|
if objectRef.guest != "" {
|
|
|
|
t["guest"] = objectRef.guest
|
|
|
|
}
|
2018-12-28 21:24:43 +00:00
|
|
|
if c, ok := e.resourceKinds["cluster"].objects[parent.parentRef.Value]; ok {
|
2018-09-11 21:53:46 +00:00
|
|
|
t["clustername"] = c.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in Datacenter name
|
|
|
|
if objectRef.dcname != "" {
|
|
|
|
t["dcname"] = objectRef.dcname
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine which point tag to map to the instance
|
|
|
|
name := v.Name
|
|
|
|
instance := "instance-total"
|
|
|
|
if v.Instance != "" {
|
|
|
|
instance = v.Instance
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(name, "cpu.") {
|
|
|
|
t["cpu"] = instance
|
|
|
|
} else if strings.HasPrefix(name, "datastore.") {
|
|
|
|
t["lun"] = instance
|
2018-11-02 19:05:28 +00:00
|
|
|
if ds, ok := e.lun2ds[instance]; ok {
|
|
|
|
t["dsname"] = ds
|
|
|
|
} else {
|
|
|
|
t["dsname"] = instance
|
|
|
|
}
|
2018-09-11 21:53:46 +00:00
|
|
|
} else if strings.HasPrefix(name, "disk.") {
|
|
|
|
t["disk"] = cleanDiskTag(instance)
|
|
|
|
} else if strings.HasPrefix(name, "net.") {
|
|
|
|
t["interface"] = instance
|
|
|
|
} else if strings.HasPrefix(name, "storageAdapter.") {
|
|
|
|
t["adapter"] = instance
|
|
|
|
} else if strings.HasPrefix(name, "storagePath.") {
|
|
|
|
t["path"] = instance
|
|
|
|
} else if strings.HasPrefix(name, "sys.resource") {
|
|
|
|
t["resource"] = instance
|
|
|
|
} else if strings.HasPrefix(name, "vflashModule.") {
|
|
|
|
t["module"] = instance
|
|
|
|
} else if strings.HasPrefix(name, "virtualDisk.") {
|
|
|
|
t["disk"] = instance
|
|
|
|
} else if v.Instance != "" {
|
|
|
|
// default
|
|
|
|
t["instance"] = v.Instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoint) makeMetricIdentifier(prefix, metric string) (string, string) {
|
|
|
|
parts := strings.Split(metric, ".")
|
|
|
|
if len(parts) == 1 {
|
|
|
|
return prefix, parts[0]
|
|
|
|
}
|
|
|
|
return prefix + e.Parent.Separator + parts[0], strings.Join(parts[1:], e.Parent.Separator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanGuestID(id string) string {
|
|
|
|
return strings.TrimSuffix(id, "Guest")
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanDiskTag(disk string) string {
|
|
|
|
// Remove enclosing "<>"
|
|
|
|
return strings.TrimSuffix(strings.TrimPrefix(disk, "<"), ">")
|
|
|
|
}
|