print log messages to stderr
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-05-23 16:52:05 +10:00
parent e65965bb42
commit 55f6889640

58
main.go
View File

@@ -57,7 +57,7 @@ func getEvents(eventTypes []string, entities []types.ManagedObjectReference, beg
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 {
fmt.Printf("vCenter time is '%v'\n", now) fmt.Fprintf(os.Stderr, "vCenter time is '%v'\n", now)
} }
m := event.NewManager(c.Client) m := event.NewManager(c.Client)
@@ -75,7 +75,7 @@ func getEvents(eventTypes []string, entities []types.ManagedObjectReference, beg
}, },
} }
if len(entities) > 0 { if len(entities) > 0 {
fmt.Printf("getEvents setting entity '%v' to filter\n", entities[0]) fmt.Fprintf(os.Stderr, "getEvents setting entity '%v' to filter\n", entities[0])
filter.Entity = &types.EventFilterSpecByEntity{ filter.Entity = &types.EventFilterSpecByEntity{
Entity: entities[0], Entity: entities[0],
Recursion: types.EventFilterSpecRecursionOptionAll, Recursion: types.EventFilterSpecRecursionOptionAll,
@@ -107,8 +107,8 @@ func getEvents(eventTypes []string, entities []types.ManagedObjectReference, beg
event := events[i].GetEvent() event := events[i].GetEvent()
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.Fprintf(os.Stderr, "%d [%s] [%s] %s\n", event.Key, event.CreatedTime.In(location).Format(time.ANSIC), kind, event.FullFormattedMessage)
fmt.Printf("%d [%s] %s\n", event.Key, event.CreatedTime.In(location).Format(time.ANSIC), event.FullFormattedMessage) fmt.Fprintf(os.Stderr, "%d [%s] %s\n", event.Key, event.CreatedTime.In(location).Format(time.ANSIC), event.FullFormattedMessage)
} }
} }
@@ -121,15 +121,15 @@ func getCluster(name string) mo.ClusterComputeResource {
cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"ClusterComputeResource"}, true) cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"ClusterComputeResource"}, true)
var clusters []mo.ClusterComputeResource var clusters []mo.ClusterComputeResource
fmt.Printf("Searching for Cluster '%s'\n", name) fmt.Fprintf(os.Stderr, "Searching for Cluster '%s'\n", name)
err := cv.Retrieve(ctx, []string{"ClusterComputeResource"}, []string{"summary", "name"}, &clusters) err := cv.Retrieve(ctx, []string{"ClusterComputeResource"}, []string{"summary", "name"}, &clusters)
if err != nil { if err != nil {
fmt.Printf("Failed searching for Cluster %s : %s\n", name, err) fmt.Fprintf(os.Stderr, "Failed searching for Cluster %s : %s\n", name, err)
return mo.ClusterComputeResource{} return mo.ClusterComputeResource{}
} else { } else {
for _, cluster := range clusters { for _, cluster := range clusters {
if cluster.Name == name { if cluster.Name == name {
fmt.Printf("Found corresponding Cluster with MoRef '%s'\n", cluster.Reference()) fmt.Fprintf(os.Stderr, "Found corresponding Cluster with MoRef '%s'\n", cluster.Reference())
return cluster return cluster
} }
} }
@@ -145,15 +145,15 @@ func getVM(name string) mo.VirtualMachine {
cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) cv, _ := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
var vms []mo.VirtualMachine var vms []mo.VirtualMachine
fmt.Printf("Searching for VM '%s'\n", name) fmt.Fprintf(os.Stderr, "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 {
fmt.Printf("Failed searching for VM %s : %s\n", name, err) fmt.Fprintf(os.Stderr, "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 {
fmt.Printf("Found corresponding VM with MoRef '%s'\n", vm.Reference()) fmt.Fprintf(os.Stderr, "Found corresponding VM with MoRef '%s'\n", vm.Reference())
return vm return vm
} }
} }
@@ -169,15 +169,15 @@ func getVmInCluster(name string, cluster types.ManagedObjectReference) mo.Virtua
cv, _ := m.CreateContainerView(ctx, cluster, []string{"VirtualMachine"}, true) cv, _ := m.CreateContainerView(ctx, cluster, []string{"VirtualMachine"}, true)
var vms []mo.VirtualMachine var vms []mo.VirtualMachine
fmt.Printf("Searching for VM '%s'\n", name) fmt.Fprintf(os.Stderr, "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 {
fmt.Printf("Failed searching for VM %s : %s\n", name, err) fmt.Fprintf(os.Stderr, "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 {
fmt.Printf("Found corresponding VM with MoRef '%s'\n", vm.Reference()) fmt.Fprintf(os.Stderr, "Found corresponding VM with MoRef '%s'\n", vm.Reference())
return vm return vm
} }
} }
@@ -201,7 +201,7 @@ func main() {
// So we can convert vCenter UTC to our local timezone // So we can convert vCenter UTC to our local timezone
location, _ = time.LoadLocation(*vTZ) location, _ = time.LoadLocation(*vTZ)
fmt.Printf("Starting execution. Built on %s from sha1 %s\n", buildTime, sha1ver) fmt.Fprintf(os.Stderr, "Starting execution. Built on %s from sha1 %s\n", buildTime, sha1ver)
u, err := url.Parse(*vURL) u, err := url.Parse(*vURL)
if err != nil { if err != nil {
@@ -210,11 +210,11 @@ func main() {
} else { } else {
if !strings.HasSuffix(u.Path, "/sdk") { if !strings.HasSuffix(u.Path, "/sdk") {
u.Path, _ = url.JoinPath(u.Path, "/sdk") u.Path, _ = url.JoinPath(u.Path, "/sdk")
fmt.Printf("Updated vCenter URL to '%v'\n", u) fmt.Fprintf(os.Stderr, "Updated vCenter URL to '%v'\n", u)
} }
} }
fmt.Printf("Connecting to vCenter %s\n", u) fmt.Fprintf(os.Stderr, "Connecting to vCenter %s\n", u)
u.User = url.UserPassword(*vUser, *vPass) u.User = url.UserPassword(*vUser, *vPass)
ctx, cancel = context.WithCancel(context.Background()) ctx, cancel = context.WithCancel(context.Background())
@@ -230,11 +230,11 @@ func main() {
//finder := find.NewFinder(c.Client) //finder := find.NewFinder(c.Client)
fmt.Printf("Searching for hostfailure events\n") fmt.Fprintf(os.Stderr, "Searching for hostfailure events\n")
hostFailures := getEvents([]string{"com.vmware.vc.HA.DasHostFailedEvent"}, []types.ManagedObjectReference{}, *begin, *end) 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.Fprintf(os.Stderr, "Found at least one host failure, proceeding with VM restart search\n")
vmFailures := getEvents([]string{"com.vmware.vc.ha.VmRestartedByHAEvent"}, []types.ManagedObjectReference{}, *begin, *end) vmFailures := getEvents([]string{"com.vmware.vc.ha.VmRestartedByHAEvent"}, []types.ManagedObjectReference{}, *begin, *end)
// Sort the host failure events by time // Sort the host failure events by time
@@ -248,7 +248,7 @@ func main() {
var possibleHosts []types.Event var possibleHosts []types.Event
event := vmFailures[i] event := vmFailures[i]
vmRestartTime := event.CreatedTime.In(location) vmRestartTime := event.CreatedTime.In(location)
fmt.Printf("Failure event for VM '%s' restarted in cluster '%s'\n", event.Vm.Name, event.ComputeResource.Name) fmt.Fprintf(os.Stderr, "Failure event for VM '%s' restarted in cluster '%s'\n", event.Vm.Name, event.ComputeResource.Name)
// Get a reference to the cluster mentioned // Get a reference to the cluster mentioned
cluster := getCluster((event.ComputeResource.Name)) cluster := getCluster((event.ComputeResource.Name))
@@ -257,7 +257,7 @@ func main() {
// Use VmDisconnectedEvent to see which host this VM was on // Use VmDisconnectedEvent to see which host this VM was on
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.Fprintf(os.Stderr, "Retrieved '%d' corresponding events.\n", len(disconnectedEvents))
// Determine which host the VM was previoulsy running on // Determine which host the VM was previoulsy running on
if len(disconnectedEvents) > 0 { if len(disconnectedEvents) > 0 {
@@ -267,7 +267,7 @@ func main() {
}) })
disconnectedHost := disconnectedEvents[0] disconnectedHost := disconnectedEvents[0]
fmt.Printf("VM was running on host '%s' previously\n", disconnectedHost.Host.Name) fmt.Fprintf(os.Stderr, "VM was running on host '%s' previously\n", disconnectedHost.Host.Name)
// Capture all the failure events for this host // Capture all the failure events for this host
for _, hostEvent := range hostFailures { for _, hostEvent := range hostFailures {
@@ -276,7 +276,7 @@ func main() {
} }
} }
} else { } else {
fmt.Printf("could not determine previous host for this VM. Filtering all host failures for events prior to VM restart time '%s'\n", vmRestartTime) fmt.Fprintf(os.Stderr, "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
for _, hostEvent := range hostFailures { for _, hostEvent := range hostFailures {
@@ -285,32 +285,32 @@ func main() {
} }
} }
fmt.Printf("Based on event times there were %d possible hosts this VM was running on\n", len(possibleHosts)) fmt.Fprintf(os.Stderr, "Based on event times there were %d possible hosts this VM was running on\n", len(possibleHosts))
if len(possibleHosts) == 0 { if len(possibleHosts) == 0 {
fmt.Printf("No ESXi outage events happened before VM %s restart event at %s, skipping this event.\n", event.Vm.Name, vmRestartTime) fmt.Fprintf(os.Stderr, "No ESXi outage events happened before VM %s restart event at %s, skipping this event.\n", event.Vm.Name, vmRestartTime)
continue continue
} }
} }
if len(possibleHosts) == 1 { if len(possibleHosts) == 1 {
fmt.Printf("Found a single host failure event relating to VM %s\n", event.Vm.Name) fmt.Fprintf(os.Stderr, "Found a single host failure event relating to VM %s\n", event.Vm.Name)
fmt.Printf("Failed host was '%s', using outage start time of '%s'\n", possibleHosts[0].Host.Name, possibleHosts[0].CreatedTime.In(location)) fmt.Fprintf(os.Stderr, "Failed host was '%s', using outage start time of '%s'\n", possibleHosts[0].Host.Name, possibleHosts[0].CreatedTime.In(location))
failedHost = possibleHosts[0].Host.Name failedHost = possibleHosts[0].Host.Name
outageStart = possibleHosts[0].CreatedTime.In(location) outageStart = possibleHosts[0].CreatedTime.In(location)
restartTime = vmRestartTime restartTime = vmRestartTime
} else if len(possibleHosts) > 1 { } else if len(possibleHosts) > 1 {
fmt.Printf("Found multiple host failure events relating to VM %s\n", event.Vm.Name) fmt.Fprintf(os.Stderr, "Found multiple host failure events relating to VM %s\n", event.Vm.Name)
// possible hosts is sorted by time, so use the last value if there are multiple // possible hosts is sorted by time, so use the last value if there are multiple
lastIndex := len(possibleHosts) - 1 lastIndex := len(possibleHosts) - 1
fmt.Printf("Failed host was '%s', using outage start time of '%s'\n", possibleHosts[lastIndex].Host.Name, possibleHosts[lastIndex].CreatedTime.In(location)) fmt.Fprintf(os.Stderr, "Failed host was '%s', using outage start time of '%s'\n", possibleHosts[lastIndex].Host.Name, possibleHosts[lastIndex].CreatedTime.In(location))
failedHost = possibleHosts[lastIndex].Host.Name failedHost = possibleHosts[lastIndex].Host.Name
outageStart = possibleHosts[lastIndex].CreatedTime.In(location) outageStart = possibleHosts[lastIndex].CreatedTime.In(location)
restartTime = vmRestartTime restartTime = vmRestartTime
} else { } else {
fmt.Printf("Didn't get anything useful!\n") fmt.Fprintf(os.Stderr, "Didn't get anything useful!\n")
} }
/* /*