VMware Cloud Community
napallo
Contributor
Contributor

Query VMs supported by HostSystems in cluster

I'm trying to use VI3 SDK in Java, and list the VirtualMachines running under the ESX HostSystems.

I list all the HostSystem objects, and then for all HostSystem objects with UUID entries, I call the function below. This procedure returns the VMs fine when an ESX host is NOT in a cluster. However, when I disconnect/reconnect the ESX host into a cluster - I hit null references for all the VM references underneath. How do I query for the associated VMs (that are using resources off a Host)? I've not been successful with walking thru the ResourcePool to connect the dots.

Help certainly appreciated. Thanks!

=================

public void list_vms_on_host_via_uuid(String uuid) throws Exception {

try {

ObjectContent[] ocary, ocary2 = null;

ObjectContent oc = null, oc2 = null;

DynamicProperty dp = null;

ManagedObjectReference mor = null, hostSystemManagedObjectRef = null, vmmor = null;

ArrayOfManagedObjectReference amor;

hostSystemManagedObjectRef = service.findByUuid(sic.getSearchIndex(), mor, uuid, false);

ManagedObjectReference pCollector = _sic.getPropertyCollector();

PropertySpec pSpec = new PropertySpec();

pSpec.setType("HostSystem");

pSpec.setPathSet(new String[] {"vm"});

ObjectSpec oSpec = new ObjectSpec();

oSpec.setObj(hostSystemManagedObjectRef);

oSpec.setSkip(Boolean.FALSE);

PropertyFilterSpec pfSpec = new PropertyFilterSpec();

pfSpec.setObjectSet(new ObjectSpec[] ); pfSpec.setPropSet(new PropertySpec[] ); ocary = service.retrieveProperties(propCol, new PropertyFilterSpec[] ); oc = ocary[0]; dp = oc.getPropSet()[0]; amor = (ArrayOfManagedObjectReference) dp.getVal(); if (amor.getManagedObjectReference() == null) { System.out.println(" list_vms_on_host_via_uuid: NULL value for the MOR"); return new String[] {""}; } for (int ai = 0; ai < amor.getManagedObjectReference().length; ai++) { vmmor = amor.getManagedObjectReference(ai); oSpec.setObj(vmmor); pfSpec.setObjectSet(new ObjectSpec[] );

pSpec.setType("VirtualMachine");

pSpec.setPathSet(new String[] {"config.name", "config.uuid", "guest.guestState", "summary.config.memorySizeMB", "config.files.vmPathName", "guest.guestFullName", "guest.guestId"});

pfSpec.setPropSet(new PropertySpec[] );

ocary2 = service.retrieveProperties(propCol, new PropertyFilterSpec[] );

oc2 = ocary2[0];

PrintOutContents(ocary2);

}

}

catch (Exception e) {

System.out.println("Exception caught in list_vms_on_host_via_uuid method:");

e.printStackTrace();

throw e;

}

}

0 Kudos
2 Replies
napallo
Contributor
Contributor

Further clarification:

With recursion thru ResourcePools, I can find all VMs belonging to a clustered ComputeResource. And I can find all ESX hosts contributing to those ComputeResources. But I am still at a loss in finding which hosts are supporting individual VMs within that ResourcePool. This is easily gathered with my previously posted code, when the VMs aren't beneath a cluster. But when ESX hosts are moved under a cluster, Virtual Center no longer references supported VirtualMachines by the "vm" property under "HostSystem".

I'd like to identify snapshots of hosts being used per VM.

Does anyone know how to do this when the supporting Hosts are in a cluster? Is this even possible?

0 Kudos
napallo
Contributor
Contributor

Since I have the same ESX host "disconnected" in one place and "connected" in another, I suspect the findByUuid function is returning the disconnected host, not the active/connected host in a cluster.

I'm going to test my theory (that findByUuid is returning the disconnected host) by creating a Path and using "service.findByInventoryPath" for setObj. I'll update this thread on my findings.

Details follow:

=======================

In browsing the MOB content via VC server URL, I validated the disconnected ESX host object no longer contained VM references. I also validated that the ESX host under the cluster actually DOES contain the vm references. That's when I noticed something very interesting, which may be the cause of grief...

When I moved an ESX host into a new cluster, I "disconnected" the original ESX host server from the datacenter, and connected it into the cluster. I didn't want to remove it because I wanted to validate this Java utility on clustered and non-clustered environments. Well, from the MOB content I noticed that the UUID of the disconnected/non-clustered ESX host (under the ComputeResource), was the same as the UUID of the newly connected/clustered ESX host (under the ClusterComputeResource). This was interesting because I was using the "service.findByUuid" to get a handle on the ESX host, and now there were two ESX hosts listed in my inventory with the same UUID.

0 Kudos