Hello,
I've got a simple inventory query setup that pulls back all of the virtual machines in vCenter. Is there any easy way to get the cluster name that the VM resides in? I'm not finding a basic VM property that contains this information. I see there's a "parent" property, so I'm not sure if that's what I need to use or not. A code snippet below is basically what I'd like to do:
IList<EntityViewBase> vmList = client.FindEntityViews(typeof(VirtualMachine), null, null, null);
foreach (VirtualMachine vm in vmList)
{
Console.WriteLine(vm.Name);
Console.WirteLine(vm.??)
}
Any help is greatly appreciated. Thanks!
There is not direct mapping of a vCenter cluster to a VM, you can walk backwards OR you can just query all clusters of vCenter and then do whatever you want with the VMs that are under each cluster.
Take a look at this vSphere SDK for Perl script for some ideas:
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
VMware Code Central - Scripts/Sample code for Developers and Administrators
If you find this information useful, please award points for "correct" or "helpful".
Unfortunately, they didn't expose the current Cluster as a simple property on the Virtual Machine managed object.
You're right though, you can use the parent property to traceback to determine parent relationships in Virtual Center.
You can use a recursive function similar to the following:
## Recursive function to walk up the inventory tree using the parent property of each ## ManagedObject. sub FindCluster { my ($entity, $n_ref) = @_; unless ( defined $entity ) { #Root folder reached. No ClusterComputeResource was found. undef $$n_ref; return; } # If the object type is a ClusterComputeResource, set the passed value of the pass by reference # of $cluster_name if($entity->type eq "ClusterComputeResource") { my $cluster_view = Vim::get_view(mo_ref => $entity, properties => \['name'] ); $$n_ref = $cluster_view->name; return; } $entity_view = Vim::get_view(mo_ref => $entity, properties => \['name'] ); FindCluster($entity_view->parent, $n_ref); }
Then in the main section of your program you could call the function on your managed object's parent property:
FindCluster($host->parent, \$cluster_name); if (defined $cluster_name) { print "Cluster for '$hostname' = $cluster_name\n"; } else { print "No cluster found for $hostname.\n"; }
In this case I used a host managed object, but you should be able to use a Virtual Machine managed object instead as your first parameter.
The other option is to just enumerate all your clusters and look for the presence of your Virtual Machine. You'll find this doesn't scale as well and can take some runtime to complete in a larger environment. However, it works and is a bit simpler to code.
You'll have to remove the preceding *\& before the [ after the named argument properties. The forum parses the bracket badly.
Thank you for the detailed reply! I figured that the solution probably wasn't going to be easy This definitely leads me down the right track!
So I got this one figured out in C# - it seems to work good enough:
private string getClusterName(VimClient client, HostSystem host)
{
string clusterName = null;
//Add whatever ClusterComputeResource properties you want to query.
string[] clusterProps = new string[] { "name" };
try
{
ManagedObjectReference objCluster = host.Parent;
ClusterComputeResource cluster = (ClusterComputeResource)client.GetView(objCluster, clusterProps);
clusterName = cluster.Name;
}
catch (Exception ex)
{
Console.WriteLine(DateTime.Now + "::::ERROR::An error occured while attempting get the cluster name. MESSAGE: " + ex.Message);
}
return clusterName;
}