From 47c61f0417da1a8b36f5f2b509a88188bfa82684 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Mon, 8 Apr 2024 16:57:52 +1000 Subject: [PATCH] avoid panic when vm wasn't found in our search --- main.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index bba2b6d..14f7135 100644 --- a/main.go +++ b/main.go @@ -291,7 +291,6 @@ func main() { } if hostFound { - // make sure that this event is within 10 minutes of the corresponding host failed event unreachableStartComparison := h.CreatedTime.In(location).Add(time.Duration(int64(time.Minute) * -10)) unreachableEndComparison := h.CreatedTime.In(location).Add(time.Duration(int64(time.Minute) * 10)) @@ -330,6 +329,9 @@ func main() { var failedHost string var possibleHosts []types.Event var vmDisconnectedEvents []types.Event + var vmFound bool + var vmOS string + var vmPowerState string event := vmFailures[i] vmRestartTime := event.CreatedTime.In(location) @@ -356,6 +358,7 @@ func main() { //continue } else { // Use VmDisconnectedEvent to see which host this VM was on + vmFound = true vmDisconnectedEvents = getEvents([]string{"VmDisconnectedEvent"}, []types.ManagedObjectReference{vm.Reference()}, *begin, *end) log.Printf("Retrieved '%d' VmDisconnectedEvent events.\n", len(vmDisconnectedEvents)) } @@ -499,6 +502,14 @@ func main() { duration := restartTime.Sub(outageStart) out := time.Time{}.Add(duration) + if vmFound { + vmOS = vm.Summary.Guest.GuestFullName + vmPowerState = string(vm.Summary.Runtime.PowerState) + } else { + vmOS = "" + vmPowerState = "" + } + // Create a new result result := OutageResults{ VM: event.Vm.Name, @@ -508,8 +519,8 @@ func main() { Cluster: event.ComputeResource.Name, FailedHost: failedHost, NewHost: event.Host.Name, - GuestOS: vm.Summary.Guest.GuestFullName, - CurrentPowerState: string(vm.Summary.Runtime.PowerState), + GuestOS: vmOS, + CurrentPowerState: vmPowerState, Description: event.FullFormattedMessage, } // Append to list of all results