code tidy up
This commit is contained in:
@@ -27,6 +27,7 @@ steps:
|
|||||||
# PLUGIN_INCLUDE: ^events$,^events_checksum.txt$
|
# PLUGIN_INCLUDE: ^events$,^events_checksum.txt$
|
||||||
# PLUGIN_EXCLUDE: ^\.git/$
|
# PLUGIN_EXCLUDE: ^\.git/$
|
||||||
|
|
||||||
|
# https://github.com/hypervtechnics/drone-sftp
|
||||||
- name: dell-sftp-deploy
|
- name: dell-sftp-deploy
|
||||||
image: hypervtechnics/drone-sftp
|
image: hypervtechnics/drone-sftp
|
||||||
settings:
|
settings:
|
||||||
|
52
main.go
52
main.go
@@ -182,7 +182,7 @@ func getCluster(name string) mo.ClusterComputeResource {
|
|||||||
return mo.VirtualMachine{}
|
return mo.VirtualMachine{}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
func getVmInCluster(name string, cluster types.ManagedObjectReference) mo.VirtualMachine {
|
func getVmInCluster(name string, cluster types.ManagedObjectReference) (mo.VirtualMachine, error) {
|
||||||
// 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)
|
||||||
cv, _ := m.CreateContainerView(ctx, cluster, []string{"VirtualMachine"}, true)
|
cv, _ := m.CreateContainerView(ctx, cluster, []string{"VirtualMachine"}, true)
|
||||||
@@ -192,18 +192,18 @@ func getVmInCluster(name string, cluster types.ManagedObjectReference) mo.Virtua
|
|||||||
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{}, fmt.Errorf("error searching for VM %s : %s", name, err)
|
||||||
} 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'", vm.Reference())
|
||||||
return vm
|
return vm, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we reached here then we didn't find a VM
|
// If we reached here then we didn't find a VM
|
||||||
return mo.VirtualMachine{}
|
return mo.VirtualMachine{}, fmt.Errorf("no VM found with name %s", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -272,6 +272,7 @@ func main() {
|
|||||||
|
|
||||||
log.Printf("Searching for ha status change events\n")
|
log.Printf("Searching for ha status change events\n")
|
||||||
haStatusChanges := getEvents([]string{"com.vmware.vc.HA.HostStateChangedEvent"}, []types.ManagedObjectReference{}, *begin, *end)
|
haStatusChanges := getEvents([]string{"com.vmware.vc.HA.HostStateChangedEvent"}, []types.ManagedObjectReference{}, *begin, *end)
|
||||||
|
log.Printf("Found %d ha status change events\n", len(haStatusChanges))
|
||||||
|
|
||||||
// filter ha status changed messages for unreachable ones
|
// filter ha status changed messages for unreachable ones
|
||||||
for _, h := range haStatusChanges {
|
for _, h := range haStatusChanges {
|
||||||
@@ -290,19 +291,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure that this event is within 10 minutes either side of the corresponding host failed event
|
||||||
if hostFound {
|
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))
|
unreachableStartComparison := h.CreatedTime.In(location).Add(time.Duration(int64(time.Minute) * -10))
|
||||||
unreachableEndComparison := h.CreatedTime.In(location).Add(time.Duration(int64(time.Minute) * 10))
|
unreachableEndComparison := h.CreatedTime.In(location).Add(time.Duration(int64(time.Minute) * 10))
|
||||||
|
|
||||||
if haFailedTime.Before(unreachableEndComparison) && haFailedTime.After(unreachableStartComparison) {
|
if haFailedTime.Before(unreachableEndComparison) && haFailedTime.After(unreachableStartComparison) {
|
||||||
haUnreachableEvents = append(haUnreachableEvents, h)
|
haUnreachableEvents = append(haUnreachableEvents, h)
|
||||||
log.Printf("Recording host %s unreachable HA status event at %s\n", h.Host.Name, h.CreatedTime.In(location).Format(time.ANSIC))
|
log.Printf("Keeping host %s unreachable HA status event at %s\n", h.Host.Name, h.CreatedTime.In(location).Format(time.ANSIC))
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Host %s HA failure time at %s was before %s or after %s, excluding this ha unreachable event\n", h.Host.Name, haFailedTime.Format(time.ANSIC),
|
log.Printf("Excluding HA Unreachable for Host %s at time %s since it was before %s or after %s\n", h.Host.Name, haFailedTime.Format(time.ANSIC),
|
||||||
unreachableStartComparison, unreachableEndComparison)
|
unreachableEndComparison, unreachableStartComparison)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Host %s was not found in the list of hostfailure events, skipping this host\n", h.Host.Name)
|
log.Printf("Host %s was not found in the list of hostfailure events, skipping this host\n", h.Host.Name)
|
||||||
}
|
}
|
||||||
@@ -325,6 +325,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := range vmFailures {
|
for i := range vmFailures {
|
||||||
|
var vm mo.VirtualMachine
|
||||||
var outageStart, restartTime time.Time
|
var outageStart, restartTime time.Time
|
||||||
var failedHost string
|
var failedHost string
|
||||||
var possibleHosts []types.Event
|
var possibleHosts []types.Event
|
||||||
@@ -339,28 +340,31 @@ func main() {
|
|||||||
fuzzyTime := vmRestartTime.Add(time.Duration(int64(time.Minute) * int64(*fuzzyMinutes)))
|
fuzzyTime := vmRestartTime.Add(time.Duration(int64(time.Minute) * int64(*fuzzyMinutes)))
|
||||||
log.Printf("Failure event for VM '%s' restarted in cluster '%s' at %s\n", event.Vm.Name, event.ComputeResource.Name, event.CreatedTime.In(location).Format(time.ANSIC))
|
log.Printf("Failure event for VM '%s' restarted in cluster '%s' at %s\n", event.Vm.Name, event.ComputeResource.Name, event.CreatedTime.In(location).Format(time.ANSIC))
|
||||||
|
|
||||||
// Get a reference to the cluster mentioned
|
// filter all the disconnected events to the ones belonging to this VM
|
||||||
|
for _, e := range allVmDisconnectedEvents {
|
||||||
|
if e.Vm.Name == event.Vm.Name {
|
||||||
|
log.Printf("Adding VM disconnected event on host %s at time %s\n", e.Host.Name, e.CreatedTime.In(location))
|
||||||
|
vmDisconnectedEvents = append(vmDisconnectedEvents, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("Filtered '%d' VmDisconnectedEvent events belonging to VM '%s'\n", len(vmDisconnectedEvents), event.Vm.Name)
|
||||||
|
|
||||||
|
// Get a reference to the cluster mentioned in the event
|
||||||
cluster := getCluster((event.ComputeResource.Name))
|
cluster := getCluster((event.ComputeResource.Name))
|
||||||
vm := getVmInCluster(event.Vm.Name, cluster.Reference())
|
vm, err = getVmInCluster(event.Vm.Name, cluster.Reference())
|
||||||
//log.Printf("VM: '%+v'\n", vm)
|
//log.Printf("VM: '%+v'\n", vm)
|
||||||
//vm := getVM(event.Vm.Name)
|
//vm := getVM(event.Vm.Name)
|
||||||
|
|
||||||
// If we couldn't find the vm then skip this event
|
// If we couldn't find the vm then try using a list of all the disconnected events found
|
||||||
if len(vm.ExtensibleManagedObject.Self.Value) == 0 {
|
//if len(vm.ExtensibleManagedObject.Self.Value) == 0 {
|
||||||
|
if err != nil {
|
||||||
log.Printf("No VM matching string '%s' found in cluster '%s'\n", event.Vm.Name, cluster.Reference())
|
log.Printf("No VM matching string '%s' found in cluster '%s'\n", event.Vm.Name, cluster.Reference())
|
||||||
for _, e := range allVmDisconnectedEvents {
|
vmFound = false
|
||||||
if e.Vm.Name == event.Vm.Name {
|
|
||||||
log.Printf("Adding VM disconnected event on host %s at time %s\n", e.Host.Name, e.CreatedTime.In(location))
|
|
||||||
vmDisconnectedEvents = append(vmDisconnectedEvents, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.Printf("Filtered '%d' VmDisconnectedEvent events.\n", len(vmDisconnectedEvents))
|
|
||||||
//continue
|
|
||||||
} else {
|
} else {
|
||||||
// Use VmDisconnectedEvent to see which host this VM was on
|
// Use VmDisconnectedEvent to see which host this VM was on
|
||||||
vmFound = true
|
vmFound = true
|
||||||
vmDisconnectedEvents = getEvents([]string{"VmDisconnectedEvent"}, []types.ManagedObjectReference{vm.Reference()}, *begin, *end)
|
//vmDisconnectedEvents = getEvents([]string{"VmDisconnectedEvent"}, []types.ManagedObjectReference{vm.Reference()}, *begin, *end)
|
||||||
log.Printf("Retrieved '%d' VmDisconnectedEvent events.\n", len(vmDisconnectedEvents))
|
//log.Printf("Retrieved '%d' VmDisconnectedEvent events.\n", len(vmDisconnectedEvents))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine which host the VM was previoulsy running on
|
// Determine which host the VM was previoulsy running on
|
||||||
|
Reference in New Issue
Block a user