progress
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-05-23 12:58:29 +10:00
parent 7da16fc9da
commit 69fe4b887e

64
main.go
View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"os"
"sort"
"strings"
"time"
@@ -92,6 +93,30 @@ func getEvents(eventTypes []string, entities []types.ManagedObjectReference, beg
return returnEvents
}
func getVM(name string) mo.VirtualMachine {
// 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)
var vms []mo.VirtualMachine
fmt.Printf("Searching for VM\n")
err := cv.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary", "name"}, &vms)
if err != nil {
fmt.Printf("Failed searching for VM %s : %s\n", name, err)
return mo.VirtualMachine{}
} else {
for _, vm := range vms {
if vm.Name == name {
fmt.Printf("Found corresponding VM with MoRef '%s'\n", vm.Reference())
return vm
}
}
}
// If we reached here then we didn't find a VM
return mo.VirtualMachine{}
}
func main() {
// Command line flags for the vCenter connection
vURL := flag.String("url", "", "The URL of a vCenter server, eg https://server.domain.example/sdk")
@@ -134,10 +159,7 @@ func main() {
defer c.Logout(ctx)
//finder := find.NewFinder(c.Client)
m := view.NewManager(c.Client)
// Create a container view so that we can search vCenter for a VM if we found any failure events
cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
fmt.Printf("Searching for hostfailure events\n")
hostFailures := getEvents([]string{"com.vmware.vc.HA.DasHostFailedEvent"}, []types.ManagedObjectReference{}, *begin, *end)
@@ -146,23 +168,31 @@ func main() {
vmFailures := getEvents([]string{"com.vmware.vc.ha.VmRestartedByHAEvent"}, []types.ManagedObjectReference{}, *begin, *end)
for i := range vmFailures {
var outageStart time.Time
event := vmFailures[i]
fmt.Printf("Found VM '%s' restarted in cluster '%s'\n", event.Vm.Name, event.ComputeResource.Name)
fmt.Printf("Failure event for VM '%s' restarted in cluster '%s'\n", event.Vm.Name, event.ComputeResource.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 {
fmt.Printf("Failed searching for VM %s : %s\n", event.Vm.Name, err)
vm := getVM(event.Vm.Name)
// Use VmDisconnectedEvent to see which host this VM was on
disconnectedEvents := getEvents([]string{"VmDisconnectedEvent"}, []types.ManagedObjectReference{vm.Reference()}, *begin, *end)
fmt.Printf("Retrieved '%d' corresponding events.\n", len(disconnectedEvents))
// Calculate the VM outage duration based on the previous host
if len(disconnectedEvents) > 0 {
// Sort the disconnected events by time
sort.Slice(disconnectedEvents[:], func(i, j int) bool {
return disconnectedEvents[i].CreatedTime.Before(disconnectedEvents[j].CreatedTime)
})
// Use the earliest event as the outage start
disconnectedHost := disconnectedEvents[0]
outageStart = disconnectedHost.CreatedTime.In(location)
fmt.Printf("VM was running on host '%s' previously, setting outage start to '%s'\n", disconnectedHost.Host.Name, outageStart)
} else {
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)
fmt.Printf("Retrieved '%d' corresponding events.\n", len(disconnectedEvents))
}
}
fmt.Printf("could not determine previous host for this VM. Filtering all host failures for events prior to VM restart time '%s'\n", event.CreatedTime.In(location))
// Search for host failures
}
/*