From bcd2e37b8c1c7c193722fa8b3f98222b12254a79 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Thu, 26 Oct 2023 16:52:26 +1100 Subject: [PATCH] try to avoid segv --- main.go | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 8e26272..63b91c9 100644 --- a/main.go +++ b/main.go @@ -141,7 +141,7 @@ func getScsiBusSharingVMs(client *govmomi.Client) error { // Determine cluster based on runtime host of VM based on https://github.com/vmware/govmomi/issues/1242#issuecomment-427671990 for _, host := range hsList { if host.Reference() == *vm.Summary.Runtime.Host { - fmt.Printf("host %s matches with parent %s\n", host.Name, host.Parent) + fmt.Printf("host %s matches, host parent %s\n", host.Name, host.Parent) var cluster mo.ManagedEntity err = pc.RetrieveOne(ctx, *host.Parent, []string{"name"}, &cluster) @@ -156,27 +156,33 @@ func getScsiBusSharingVMs(client *govmomi.Client) error { } } - if len(vm.Config.Hardware.Device) > 0 { - for _, device := range vm.Config.Hardware.Device { - //fmt.Printf("device: %v\n", device) - if scsi, ok := device.(types.BaseVirtualSCSIController); ok { - fmt.Printf("scsi: %v\n", scsi) - controller := scsi.GetVirtualSCSIController() - //fmt.Printf("controller: %s\n", device.GetVirtualDevice().DeviceInfo.GetDescription().Label) - fmt.Printf("VM %s is using SCSI bus sharing mode: %s\n", vm.Name, string(controller.SharedBus)) + if vm.Config != nil && vm.Config.Hardware.Device != nil { + if len(vm.Config.Hardware.Device) > 0 { + for _, device := range vm.Config.Hardware.Device { + //fmt.Printf("device: %v\n", device) + if scsi, ok := device.(types.BaseVirtualSCSIController); ok { + fmt.Printf("scsi: %v\n", scsi) + controller := scsi.GetVirtualSCSIController() + //fmt.Printf("controller: %s\n", device.GetVirtualDevice().DeviceInfo.GetDescription().Label) + fmt.Printf("VM %s is using SCSI bus sharing mode: %s\n", vm.Name, string(controller.SharedBus)) + + if controller.SharedBus != "noSharing" { + result := BusSharingResults{ + VmName: vm.Name, + ClusterName: clusterName, + ControllerName: device.GetVirtualDevice().DeviceInfo.GetDescription().Label, + SharingType: sharedBusToString(controller.SharedBus), + } + busSharingResults = append(busSharingResults, result) - if controller.SharedBus != "noSharing" { - result := BusSharingResults{ - VmName: vm.Name, - ClusterName: clusterName, - ControllerName: device.GetVirtualDevice().DeviceInfo.GetDescription().Label, - SharingType: sharedBusToString(controller.SharedBus), } - busSharingResults = append(busSharingResults, result) - } } } + } else if vm.Config == nil { + fmt.Printf("vm %s has no config\n", vm.Name) + } else if vm.Config.Hardware.Device == nil { + fmt.Printf("vm %s has no hardware device\n", vm.Name) } } @@ -261,6 +267,10 @@ func main() { //begin := flag.Duration("b", time.Hour, "Begin time") // default BeginTime is 1h ago flag.Parse() + if len(*vURL) == 0 { + panic("Unable to connect to empty vCenter URL") + } + // Print logs to file f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil {