|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package vcenterreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver" |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/url" |
| 21 | + |
| 22 | + "github.com/vmware/govmomi" |
| 23 | + "github.com/vmware/govmomi/find" |
| 24 | + "github.com/vmware/govmomi/object" |
| 25 | + "github.com/vmware/govmomi/performance" |
| 26 | + "github.com/vmware/govmomi/property" |
| 27 | + "github.com/vmware/govmomi/vim25" |
| 28 | + vt "github.com/vmware/govmomi/vim25/types" |
| 29 | +) |
| 30 | + |
| 31 | +// vcenterClient is a client that |
| 32 | +type vcenterClient struct { |
| 33 | + moClient *govmomi.Client |
| 34 | + vimDriver *vim25.Client |
| 35 | + finder *find.Finder |
| 36 | + pc *property.Collector |
| 37 | + pm *performance.Manager |
| 38 | + cfg *Config |
| 39 | +} |
| 40 | + |
| 41 | +func newVcenterClient(c *Config) *vcenterClient { |
| 42 | + return &vcenterClient{ |
| 43 | + cfg: c, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// EnsureConnection will establish a connection to the vSphere SDK if not already established |
| 48 | +func (vc *vcenterClient) EnsureConnection(ctx context.Context) error { |
| 49 | + if vc.moClient != nil { |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + sdkURL, err := vc.cfg.SDKUrl() |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + client, err := govmomi.NewClient(ctx, sdkURL, vc.cfg.Insecure) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("unable to connect to vSphere SDK on listed endpoint: %w", err) |
| 60 | + } |
| 61 | + tlsCfg, err := vc.cfg.LoadTLSConfig() |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + client.DefaultTransport().TLSClientConfig = tlsCfg |
| 66 | + user := url.UserPassword(vc.cfg.Username, vc.cfg.Password) |
| 67 | + err = client.Login(ctx, user) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("unable to login to vcenter sdk: %w", err) |
| 70 | + } |
| 71 | + vc.moClient = client |
| 72 | + vc.vimDriver = client.Client |
| 73 | + vc.pc = property.DefaultCollector(vc.vimDriver) |
| 74 | + vc.finder = find.NewFinder(vc.vimDriver) |
| 75 | + vc.pm = performance.NewManager(vc.vimDriver) |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// Disconnect will logout of the autenticated session |
| 80 | +func (vc *vcenterClient) Disconnect(ctx context.Context) error { |
| 81 | + if vc.moClient != nil { |
| 82 | + return vc.moClient.Logout(ctx) |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// Clusters returns the clusterComputeResources of the vSphere SDK |
| 88 | +func (vc *vcenterClient) Datacenters(ctx context.Context) ([]*object.Datacenter, error) { |
| 89 | + datacenters, err := vc.finder.DatacenterList(ctx, "*") |
| 90 | + if err != nil { |
| 91 | + return []*object.Datacenter{}, fmt.Errorf("unable to get datacenter lists: %w", err) |
| 92 | + } |
| 93 | + return datacenters, nil |
| 94 | +} |
| 95 | + |
| 96 | +// Clusters returns the clusterComputeResources of the vSphere SDK |
| 97 | +func (vc *vcenterClient) Clusters(ctx context.Context, datacenter *object.Datacenter) ([]*object.ClusterComputeResource, error) { |
| 98 | + vc.finder = vc.finder.SetDatacenter(datacenter) |
| 99 | + clusters, err := vc.finder.ClusterComputeResourceList(ctx, "*") |
| 100 | + if err != nil { |
| 101 | + return []*object.ClusterComputeResource{}, err |
| 102 | + } |
| 103 | + return clusters, nil |
| 104 | +} |
| 105 | + |
| 106 | +// ResourcePools returns the resourcePools in the vSphere SDK |
| 107 | +func (vc *vcenterClient) ResourcePools(ctx context.Context) ([]*object.ResourcePool, error) { |
| 108 | + rps, err := vc.finder.ResourcePoolList(ctx, "*") |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("unable to retrieve resource pools: %w", err) |
| 111 | + } |
| 112 | + return rps, err |
| 113 | +} |
| 114 | + |
| 115 | +func (vc *vcenterClient) VMs(ctx context.Context) ([]*object.VirtualMachine, error) { |
| 116 | + vms, err := vc.finder.VirtualMachineList(ctx, "*") |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("unable to retrieve resource pools: %w", err) |
| 119 | + } |
| 120 | + return vms, err |
| 121 | +} |
| 122 | + |
| 123 | +type perfSampleResult struct { |
| 124 | + counters map[string]*vt.PerfCounterInfo |
| 125 | + results []performance.EntityMetric |
| 126 | +} |
| 127 | + |
| 128 | +func (vc *vcenterClient) performanceQuery( |
| 129 | + ctx context.Context, |
| 130 | + spec vt.PerfQuerySpec, |
| 131 | + names []string, |
| 132 | + objs []vt.ManagedObjectReference, |
| 133 | +) (*perfSampleResult, error) { |
| 134 | + if vc.pm == nil { |
| 135 | + return &perfSampleResult{}, nil |
| 136 | + } |
| 137 | + vc.pm.Sort = true |
| 138 | + sample, err := vc.pm.SampleByName(ctx, spec, names, objs) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + result, err := vc.pm.ToMetricSeries(ctx, sample) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + counterInfoByName, err := vc.pm.CounterInfoByName(ctx) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + return &perfSampleResult{ |
| 151 | + counters: counterInfoByName, |
| 152 | + results: result, |
| 153 | + }, nil |
| 154 | +} |
0 commit comments