This commit is contained in:
64
main.go
64
main.go
@@ -6,11 +6,11 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/vmware/govmomi"
|
||||
"github.com/vmware/govmomi/event"
|
||||
"github.com/vmware/govmomi/find"
|
||||
"github.com/vmware/govmomi/vim25/methods"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
@@ -22,12 +22,7 @@ var (
|
||||
location *time.Location
|
||||
)
|
||||
|
||||
func exit(err error) {
|
||||
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func getEvents(eventTypes []string, begin time.Duration, end time.Duration) []types.Event {
|
||||
func getEvents(eventTypes []string, entities []types.ManagedObjectReference, begin time.Duration, end time.Duration) []types.Event {
|
||||
var returnEvents []types.Event
|
||||
// Refer https://github.com/vmware/govmomi/blob/main/examples/events/main.go
|
||||
now, err := methods.GetCurrentTime(ctx, c) // vCenter server time (UTC)
|
||||
@@ -77,8 +72,9 @@ func getEvents(eventTypes []string, begin time.Duration, end time.Duration) []ty
|
||||
for i := range events {
|
||||
event := events[i].GetEvent()
|
||||
returnEvents = append(returnEvents, *event)
|
||||
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)
|
||||
//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), event.FullFormattedMessage)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +115,55 @@ func main() {
|
||||
}
|
||||
defer c.Logout(ctx)
|
||||
|
||||
getEvents([]string{"com.vmware.vc.HA.DasHostFailedEvent"}, *begin, *end)
|
||||
hostFailures := getEvents([]string{"com.vmware.vc.HA.DasHostFailedEvent"}, []types.ManagedObjectReference{}, *begin, *end)
|
||||
|
||||
finder := find.NewFinder(c.Client)
|
||||
//m := view.NewManager(c.Client)
|
||||
|
||||
if len(hostFailures) > 0 {
|
||||
fmt.Printf("Found at least one host failure, proceeding with VM restart search\n")
|
||||
vmFailures := getEvents([]string{"com.vmware.vc.ha.VmRestartedByHAEvent"}, []types.ManagedObjectReference{}, *begin, *end)
|
||||
|
||||
for i := range vmFailures {
|
||||
event := vmFailures[i]
|
||||
fmt.Printf("Found VM '%s' restarted in cluster '%s'\n", event.Vm.Name, event.ComputeResource.Name)
|
||||
vm, err := finder.VirtualMachine(ctx, event.Vm.Name)
|
||||
if err != nil {
|
||||
fmt.Printf("Error finding VM : '%s'\n", err)
|
||||
} else {
|
||||
fmt.Printf("Found MoRef '%v'\n", vm.Reference())
|
||||
disconnectedEvents := getEvents([]string{"VmDisconnectedEvent"}, []types.ManagedObjectReference{vm.Reference()}, *begin, *end)
|
||||
fmt.Printf("Retrieved '%d' corresponding events.\n", len(disconnectedEvents))
|
||||
}
|
||||
|
||||
/*
|
||||
// Get a reference to the cluster mentioned in the event
|
||||
cluster, err := finder.ClusterComputeResource(ctx, vmFailures[i].ComputeResource.Name)
|
||||
|
||||
view, _ := m.CreateContainerView(ctx, cluster, []string{"VirtualMachine"}, true)
|
||||
var vms []mo.VirtualMachine
|
||||
err = view.Retrieve(ctx, []string{"VirtualMachine"}, []string{}, &vms)
|
||||
|
||||
// Specify what properties we want to retrieve
|
||||
|
||||
err = v.Retrieve(ctx, []string{"HostSystem"}, []string{"summary", "runtime"}, &hs)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Run the actual esxcli command on each host
|
||||
finder := find.NewFinder(c.Client)
|
||||
for _, host := range hs {
|
||||
//fmt.Printf("Host name %s is %s\n", host.Summary.Config.Name, host.Runtime.ConnectionState)
|
||||
|
||||
// Make sure that the host is connected before we try running an esxcli command
|
||||
if host.Runtime.ConnectionState == "connected" {
|
||||
// Get a reference to the host object so that we can check the ramdisk values
|
||||
objRef, err := finder.ObjectReference(ctx, host.Reference())
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
// Selecting default datacenter
|
||||
/*
|
||||
|
Reference in New Issue
Block a user