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

This commit is contained in:
2023-05-31 11:36:11 +10:00
parent e4448239e2
commit 6b68971981

67
main.go
View File

@@ -61,9 +61,9 @@ func getEvents(eventTypes []string, entities []types.ManagedObjectReference, beg
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error getting vCenter time: %s\n", err) fmt.Fprintf(os.Stderr, "Error getting vCenter time: %s\n", err)
os.Exit(1) os.Exit(1)
} else { } //else {
//log.Printf("vCenter time is '%v'\n", now) //log.Printf("vCenter time is '%v'\n", now)
} //}
m := event.NewManager(c.Client) m := event.NewManager(c.Client)
root := c.ServiceContent.RootFolder root := c.ServiceContent.RootFolder
@@ -89,7 +89,11 @@ func getEvents(eventTypes []string, entities []types.ManagedObjectReference, beg
} }
for _, e := range entities { for _, e := range entities {
log.Printf("getEvents setting entity '%v' to filter\n", entities[0]) // Only log the entity we're filtering if it isn't the vcenter root
if e != root {
log.Printf("getEvents setting entity '%v' to filter\n", e)
}
filter.Entity = &types.EventFilterSpecByEntity{ filter.Entity = &types.EventFilterSpecByEntity{
Entity: e, Entity: e,
Recursion: types.EventFilterSpecRecursionOptionAll, Recursion: types.EventFilterSpecRecursionOptionAll,
@@ -151,30 +155,31 @@ func getCluster(name string) mo.ClusterComputeResource {
return mo.ClusterComputeResource{} return mo.ClusterComputeResource{}
} }
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 func getVM(name string) mo.VirtualMachine {
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) m := view.NewManager(c.Client)
cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
var vms []mo.VirtualMachine var vms []mo.VirtualMachine
log.Printf("Searching for VM '%s'\n", name) log.Printf("Searching for VM '%s'\n", name)
err := cv.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary", "name"}, &vms) err := cv.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary", "name"}, &vms)
if err != nil { if err != nil {
log.Printf("Failed searching for VM %s : %s\n", name, err) log.Printf("Failed searching for VM %s : %s\n", name, err)
return mo.VirtualMachine{} return mo.VirtualMachine{}
} else { } else {
for _, vm := range vms { for _, vm := range vms {
if vm.Name == name { if vm.Name == name {
log.Printf("Found corresponding VM with MoRef '%s'\n", vm.Reference()) log.Printf("Found corresponding VM with MoRef '%s'\n", vm.Reference())
return vm return vm
}
} }
} }
// If we reached here then we didn't find a VM
return mo.VirtualMachine{}
} }
*/
// If we reached here then we didn't find a VM
return mo.VirtualMachine{}
}
func getVmInCluster(name string, cluster types.ManagedObjectReference) mo.VirtualMachine { func getVmInCluster(name string, cluster types.ManagedObjectReference) mo.VirtualMachine {
// Create a container view so that we can search vCenter for a VM if we found any failure events // Create a container view so that we can search vCenter for a VM if we found any failure events
m := view.NewManager(c.Client) m := view.NewManager(c.Client)
@@ -324,19 +329,7 @@ func main() {
continue continue
} }
} }
} else { // Didn't find any VM disconnected events
/*
disconnectedHost := disconnectedEvents[0]
log.Printf("First disconnected message indicates that VM was running on host '%s' previously\n", disconnectedHost.Host.Name)
// Capture all the failure events for this host
for _, hostEvent := range hostFailures {
if hostEvent.Host.Name == disconnectedHost.Host.Name {
possibleHosts = append(possibleHosts, hostEvent)
}
}
*/
} else {
log.Printf("could not determine previous host for this VM. Filtering all host failures for events prior to VM restart time '%s'\n", vmRestartTime) log.Printf("could not determine previous host for this VM. Filtering all host failures for events prior to VM restart time '%s'\n", vmRestartTime)
// Search for host failures // Search for host failures