try to avoid segv
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-10-26 16:52:26 +11:00
parent 10c2173421
commit bcd2e37b8c

44
main.go
View File

@@ -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 // Determine cluster based on runtime host of VM based on https://github.com/vmware/govmomi/issues/1242#issuecomment-427671990
for _, host := range hsList { for _, host := range hsList {
if host.Reference() == *vm.Summary.Runtime.Host { 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 var cluster mo.ManagedEntity
err = pc.RetrieveOne(ctx, *host.Parent, []string{"name"}, &cluster) 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 { if vm.Config != nil && vm.Config.Hardware.Device != nil {
for _, device := range vm.Config.Hardware.Device { if len(vm.Config.Hardware.Device) > 0 {
//fmt.Printf("device: %v\n", device) for _, device := range vm.Config.Hardware.Device {
if scsi, ok := device.(types.BaseVirtualSCSIController); ok { //fmt.Printf("device: %v\n", device)
fmt.Printf("scsi: %v\n", scsi) if scsi, ok := device.(types.BaseVirtualSCSIController); ok {
controller := scsi.GetVirtualSCSIController() fmt.Printf("scsi: %v\n", scsi)
//fmt.Printf("controller: %s\n", device.GetVirtualDevice().DeviceInfo.GetDescription().Label) controller := scsi.GetVirtualSCSIController()
fmt.Printf("VM %s is using SCSI bus sharing mode: %s\n", vm.Name, string(controller.SharedBus)) //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 //begin := flag.Duration("b", time.Hour, "Begin time") // default BeginTime is 1h ago
flag.Parse() flag.Parse()
if len(*vURL) == 0 {
panic("Unable to connect to empty vCenter URL")
}
// Print logs to file // Print logs to file
f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {