-
Notifications
You must be signed in to change notification settings - Fork 2
/
vsphere_event_listener.go
187 lines (159 loc) · 5.53 KB
/
vsphere_event_listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package collectdvsphere
import (
"context"
"net/url"
"github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/event"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
// VSphereEventListener connects to a vSphere API and listens for certain
// events, reporting them to a StatsCollector
type VSphereEventListener struct {
config VSphereConfig
statsCollector *StatsCollector
client *govmomi.Client
logger logrus.FieldLogger
}
// A VSphereConfig provides configuration for a VSphereEventListener
type VSphereConfig struct {
URL *url.URL
Insecure bool
ClusterPaths []string
BaseVMPaths []string
}
// NewVSphereEventListener creates a VSphereEventListener with a given
// configuration. Call Start on the event listener to start listening and
// reporting to the given stats collector.
func NewVSphereEventListener(config VSphereConfig, statsCollector *StatsCollector, logger logrus.FieldLogger) *VSphereEventListener {
return &VSphereEventListener{
config: config,
statsCollector: statsCollector,
logger: logger,
}
}
// Start starts the event listener and begins reporting stats to the
// StatsCollector.
func (l *VSphereEventListener) Start(ctx context.Context) error {
err := l.makeClient(ctx)
if err != nil {
return errors.Wrap(err, "couldn't create vSphere client")
}
err = l.prefillHosts(ctx)
if err != nil {
return errors.Wrap(err, "couldn't prefill hosts")
}
l.logger.Info("prefilled hosts")
err = l.prefillBaseVMs(ctx)
if err != nil {
return errors.Wrap(err, "couldn't prefill base VMs")
}
l.logger.Info("prefilled base VMs")
clusterRefs, err := l.clusterReferences(ctx)
if err != nil {
return err
}
eventManager := event.NewManager(l.client.Client)
l.logger.WithField("cluster-count", len(clusterRefs)).Info("starting event listener")
err = eventManager.Events(ctx, clusterRefs, 25, true, false, l.handleEvents)
return errors.Wrap(err, "event handling failed")
}
func (l *VSphereEventListener) handleEvents(ee []types.BaseEvent) error {
for _, baseEvent := range ee {
// TODO: A lot of the Host and Vm args can be nil, we should handle that
switch e := baseEvent.(type) {
case *types.VmPoweredOnEvent:
l.statsCollector.MarkPowerOnSuccess(e.Host.Name)
case *types.VmFailedToPowerOnEvent:
l.statsCollector.MarkPowerOnFailure(e.Host.Name)
case *types.VmPoweredOffEvent:
l.statsCollector.MarkPowerOffSuccess(e.Host.Name)
case *types.VmFailedToPowerOffEvent:
l.statsCollector.MarkPowerOffFailure(e.Host.Name)
case *types.VmClonedEvent:
l.statsCollector.MarkCloneSuccess(e.SourceVm.Name)
case *types.VmCloneFailedEvent:
l.statsCollector.MarkCloneFailure(e.Vm.Name)
}
}
return nil
}
func (l *VSphereEventListener) makeClient(ctx context.Context) (err error) {
l.client, err = govmomi.NewClient(ctx, l.config.URL, l.config.Insecure)
return errors.Wrap(err, "failed to create govmomi client")
}
func (l *VSphereEventListener) clusterReferences(ctx context.Context) ([]types.ManagedObjectReference, error) {
finder := find.NewFinder(l.client.Client, true)
clusters := make([]types.ManagedObjectReference, 0, len(l.config.ClusterPaths))
for _, path := range l.config.ClusterPaths {
cluster, err := finder.ClusterComputeResource(ctx, path)
if err != nil {
return nil, errors.Wrapf(err, "failed to find cluster with path %s", path)
}
clusters = append(clusters, cluster.Reference())
}
return clusters, nil
}
func (l *VSphereEventListener) prefillHosts(ctx context.Context) error {
clusterRefs, err := l.clusterReferences(ctx)
if err != nil {
return errors.Wrap(err, "failed to get references to compute clusters")
}
for _, clusterRef := range clusterRefs {
hosts, err := object.NewClusterComputeResource(l.client.Client, clusterRef).Hosts(ctx)
if err != nil {
return errors.Wrapf(err, "failed to list hosts in compute cluster with ID %s", clusterRef)
}
for _, host := range hosts {
var mhost mo.HostSystem
err := host.Properties(ctx, host.Reference(), []string{"summary"}, &mhost)
if err != nil {
return errors.Wrapf(err, "failed to get summary for host with ID %s", host.Reference())
}
name := mhost.Summary.Config.Name
l.logger.WithField("name", name).Info("prefilling host")
if name != "" {
l.statsCollector.ensureHostExists(name)
}
}
}
return nil
}
func (l *VSphereEventListener) prefillBaseVMs(ctx context.Context) error {
if len(l.config.BaseVMPaths) == 0 {
// Skip if no base VM path, for backwards compatibility with v1.0.0
return nil
}
finder := find.NewFinder(l.client.Client, true)
for _, baseVMPath := range l.config.BaseVMPaths {
folder, err := finder.Folder(ctx, baseVMPath)
if err != nil {
return errors.Wrapf(err, "failed to find base vm folder with path %s", baseVMPath)
}
children, err := folder.Children(ctx)
if err != nil {
return errors.Wrapf(err, "failed to list children of base vm folder with path %s", baseVMPath)
}
for _, vmRef := range children {
vm, ok := vmRef.(*object.VirtualMachine)
if !ok {
continue
}
var mvm mo.VirtualMachine
err := vm.Properties(ctx, vm.Reference(), []string{"config"}, &mvm)
if err != nil {
return errors.Wrapf(err, "failed to get config for base VM with ID %s", vmRef.Reference())
}
name := mvm.Config.Name
l.logger.WithField("name", name).Info("prefilling base VM")
if name != "" {
l.statsCollector.ensureBaseVMExists(name)
}
}
}
return nil
}