This commit is contained in:
28
main.go
28
main.go
@@ -10,8 +10,9 @@ import (
|
|||||||
|
|
||||||
"github.com/vmware/govmomi"
|
"github.com/vmware/govmomi"
|
||||||
"github.com/vmware/govmomi/event"
|
"github.com/vmware/govmomi/event"
|
||||||
"github.com/vmware/govmomi/find"
|
"github.com/vmware/govmomi/view"
|
||||||
"github.com/vmware/govmomi/vim25/methods"
|
"github.com/vmware/govmomi/vim25/methods"
|
||||||
|
"github.com/vmware/govmomi/vim25/mo"
|
||||||
"github.com/vmware/govmomi/vim25/types"
|
"github.com/vmware/govmomi/vim25/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -80,7 +81,7 @@ func getEvents(eventTypes []string, entities []types.ManagedObjectReference, beg
|
|||||||
returnEvents = append(returnEvents, *event)
|
returnEvents = append(returnEvents, *event)
|
||||||
//kind := reflect.TypeOf(events[i]).Elem().Name()
|
//kind := reflect.TypeOf(events[i]).Elem().Name()
|
||||||
//fmt.Printf("%d [%s] [%s] %s\n", event.Key, event.CreatedTime.In(location).Format(time.ANSIC), kind, event.FullFormattedMessage)
|
//fmt.Printf("%d [%s] [%s] %s\n", event.Key, event.CreatedTime.In(location).Format(time.ANSIC), kind, event.FullFormattedMessage)
|
||||||
fmt.Printf("%d [%s] [%s] %s\n", event.Key, event.CreatedTime.In(location).Format(time.ANSIC), event.FullFormattedMessage)
|
fmt.Printf("%d [%s] %s\n", event.Key, event.CreatedTime.In(location).Format(time.ANSIC), event.FullFormattedMessage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,10 +122,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer c.Logout(ctx)
|
defer c.Logout(ctx)
|
||||||
|
|
||||||
hostFailures := getEvents([]string{"com.vmware.vc.HA.DasHostFailedEvent"}, []types.ManagedObjectReference{}, *begin, *end)
|
//finder := find.NewFinder(c.Client)
|
||||||
|
m := view.NewManager(c.Client)
|
||||||
|
|
||||||
finder := find.NewFinder(c.Client)
|
// Create a container view so that we can search vCenter for a VM if we found any failure events
|
||||||
//m := view.NewManager(c.Client)
|
cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
|
||||||
|
|
||||||
|
hostFailures := getEvents([]string{"com.vmware.vc.HA.DasHostFailedEvent"}, []types.ManagedObjectReference{}, *begin, *end)
|
||||||
|
|
||||||
if len(hostFailures) > 0 {
|
if len(hostFailures) > 0 {
|
||||||
fmt.Printf("Found at least one host failure, proceeding with VM restart search\n")
|
fmt.Printf("Found at least one host failure, proceeding with VM restart search\n")
|
||||||
@@ -133,14 +137,22 @@ func main() {
|
|||||||
for i := range vmFailures {
|
for i := range vmFailures {
|
||||||
event := vmFailures[i]
|
event := vmFailures[i]
|
||||||
fmt.Printf("Found VM '%s' restarted in cluster '%s'\n", event.Vm.Name, event.ComputeResource.Name)
|
fmt.Printf("Found VM '%s' restarted in cluster '%s'\n", event.Vm.Name, event.ComputeResource.Name)
|
||||||
vm, err := finder.VirtualMachine(ctx, event.Vm.Name)
|
|
||||||
|
// Search for the VM
|
||||||
|
// Specify what properties we want to retrieve
|
||||||
|
var vms []mo.VirtualMachine
|
||||||
|
err = cv.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary", "name"}, &vms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error finding VM : '%s'\n", err)
|
fmt.Printf("Failed searching for VM %s : %s\n", event.Vm.Name, err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Found MoRef '%v'\n", vm.Reference())
|
for _, vm := range vms {
|
||||||
|
if vm.Name == event.Vm.Name {
|
||||||
|
fmt.Printf("Found corresponding VM with MoRef '%s'\n", vm.Reference())
|
||||||
disconnectedEvents := getEvents([]string{"VmDisconnectedEvent"}, []types.ManagedObjectReference{vm.Reference()}, *begin, *end)
|
disconnectedEvents := getEvents([]string{"VmDisconnectedEvent"}, []types.ManagedObjectReference{vm.Reference()}, *begin, *end)
|
||||||
fmt.Printf("Retrieved '%d' corresponding events.\n", len(disconnectedEvents))
|
fmt.Printf("Retrieved '%d' corresponding events.\n", len(disconnectedEvents))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Get a reference to the cluster mentioned in the event
|
// Get a reference to the cluster mentioned in the event
|
||||||
|
Reference in New Issue
Block a user