VMware {code} Community
jguereca
Contributor
Contributor

Missing Virtual Machines from local plugin export

Hello, 

I am leveraging the vSphere Web Services SDK for a local plugin to retrieve all the virtual machines from vCenter. A customer has pointed out the list the plugin retrieves is incomplete and incorrect. 

From the data they manually exported, I noticed a few things.

  • Out of 200 VM's only about 90 were exported by the plugin
  • Most of the virtual machines had the incorrect Host associated to them

I would like some help identifying what I could be doing wrong. Below are some code snippets to the Container View and Virtual Machines retrieval functions that I'm using. 

// Get references to the ViewManager and the PropertyCollector
ManagedObjectReference viewManager = _serviceContent.getViewManager();
ManagedObjectReference propertyCollector = _serviceContent.getPropertyCollector();

RetrieveResult props = null;

// Managed Objects to include in the ContainerView
List<String> vObjects = new ArrayList<>();
vObjects.add("Datastore");
vObjects.add("HostSystem");
vObjects.add("VirtualMachine");
vObjects.add("ClusterComputeResource");

if (startingPoint == null) {
  startingPoint = _serviceContent.getRootFolder();
}

try {
  ManagedObjectReference containerView = _vimPort.createContainerView(viewManager, startingPoint, vObjects, true);

  props = retrieveProperties(containerView, propertyCollector, vSphereObject, propertyFilter);
} catch (RuntimeFaultFaultMsg runtimeFaultFaultMsg) {
  _logger.error("Could not create ContainerView for " + vSphereObject, runtimeFaultFaultMsg);
} catch (InvalidPropertyFaultMsg invalidPropertyFaultMsg) {
  _logger.error("Could not retrieveProperties for " + vSphereObject, invalidPropertyFaultMsg);
} catch (Exception e) {
  _logger.error("An unexpected error occurred", e);
}

return formatRetrievedProperties(props);
}

 

And this is the function I'm using to collect all the Virtual Machine data

private List<VirtualMachine> transformVmPropertiesToObjects(List<Map<String, Object>> retrievedVms) {
  List<VirtualMachine> virtualMachines = new ArrayList<>();

  for (Map<String, Object> retrievedVM : retrievedVms) {
    // _logger.info("============== VirtualMachine Start of Object ==============");
    // retrievedVM.forEach((key, value) -> _logger.info(key + ": " + value));
    // _logger.info("============== VirtualMachine End of Object ==============");

    String powerState = getVMPowerState(retrievedVM);

    // Storage space being consumed on Host
    Object storageBytesObject = retrievedVM.get(VM_STORAGE);
    Long storageBytes = Long.parseLong((storageBytesObject == null) ? "0" : storageBytesObject.toString());

    double storageGb = storageBytes / Math.pow(1024, 3);
    String storageString = Integer.toString((int) Math.round(storageGb)) + " GBs";

    // Guest OS on VM
    Object guestOSObject = retrievedVM.get(VM_GUEST_OS);
    String guestOS = (guestOSObject == null) ? "" : guestOSObject.toString();

    // CPU count
    Object cpusObject = retrievedVM.get(VM_NUM_CPU_CORES);
    String cpus = (cpusObject == null) ? "" : cpusObject.toString();

    // Total memory
    Object memoryObject = retrievedVM.get(VM_MEMORY_SIZE);
    int memory = Integer.parseInt((memoryObject == null) ? "0" : memoryObject.toString());
    memory = memory / 1024;
    String memoryString = Integer.toString(memory) + " GBs";

    Object ipAddressObject = retrievedVM.get(VM_IP_ADDRESS);
    String ipAddress = (ipAddressObject == null) ? "" : ipAddressObject.toString();

    ManagedObjectReference hostRef = (ManagedObjectReference) retrievedVM.get(VM_HOST);

    // Since the 'runtime.host' property in a VirtualMachine is a MOR, the
    // propertyCollector is needed to capture the additional information
    List<Map<String, Object>> hostData = retrieveObjectProperties(hostRef, HOST, "vm_host");

    String host = (String) hostData.get(0).get(NAME);
    String vcenter = (String) hostData.get(0).get(HOST_VCENTER);

    // Capturing ClusterComputerResource MOR
    ManagedObjectReference clusterRef = (ManagedObjectReference) hostData.get(0).get(PARENT);

    // Since the 'parent' property in a HostSystem is a MOR, the
    // propertyCollector is needed to capture the additional information
    List<Map<String, Object>> clusterMap = retrieveObjectProperties(clusterRef, HOST, "host_cluster");
    String cluster = "";
    if (clusterMap.size() > 0) {
      cluster = (String) clusterMap.get(0).get("name");
    }

    // Since the 'datastore' property in a HostSystem is a MOR, the
    // propertyCollector is needed to capture the additional information
    List<Map<String, Object>> datastoreMap = retrieveObjectProperties(null, VM, "vm_datastore");

    List<String> datastoreList = new ArrayList<>();

    // Since this retrieves an array of datastores attached to the HostSystem
    // The sum needs to be calculated for total free space and total capacity
    for (Map<String, Object> datastore : datastoreMap) {
      datastoreList.add((String) datastore.get(NAME));
    }

    VirtualMachine vm = new VirtualMachine((String) retrievedVM.get(VM_NAME), vcenter, host, powerState, cpus,
        memoryString, guestOS, storageString, cluster, datastoreList, ipAddress);

    virtualMachines.add(vm);
  }

  return virtualMachines;
}
Labels (3)
0 Kudos
0 Replies