Hi,
I am looking for a way to find out all the VMs that belong to a cluster. In other words, I want a list that maps a clusters to all the VMs that run on it.
In the API documentation, I find that “ClousterComputeResouce” has a managed object refence to “HostSystem”.
And “HostSystem” has a managed object reference to the “VirtualMachine”
So I can go like this: ClusterComputeResource->ManagedObjectRefence(HostSystem)->ManagedObjectRefence(VirtualMachine)->whatever_properties_I_need_from_the_VM.
However, this would cause a lot of “Vim::get_views” calls inside a “foreach” loop. This would be deterrent to the performance as the vCetner under question has ~50 clusters and has ~3500 VMs running.
I think this would literally kill the vCenter with so many API calls.
My Question: Is there any other efficient way to find out Cluster and their respective VMs in vSphere Perl SDK.
In vSphere client, we get the “Virtual Machine” tab when we click on the Cluster and can see all the VMs that belong to that cluster. So I am hoping that there must be another easier way out.
Thanks.
Hi,
You don't need the host_view for this action. Why don't you use begin_entity for the vm views?
my $cluster ="cluster1";
my $cluster_view = Vim::find_entity_view ( view_type => 'ClusterComputeResource',
filter => { 'name' => qr/^$cluster/i } );
if($cluster_view) {
my $vm_views = Vim::find_entity_views( view_type => 'VirtualMachine',
begin_entity => $cluster_view,
properties => [ 'name' ] );
foreach my $vm_ref(@$vm_views) {
print $vm_ref->name."\n";
}
}
Hope that helps!
Greetings, Chris
To provide more insight, pelase take a look at my code below. This script will list the cluster,VM and the vCPUs of that VM.
#!/usr/bin/perl -wuse strict;use Data::Dumper;use VMware::VIRuntime;my %opts = (datacenter => {type => "=s",help => "Enter the Dacenter Name",required => 1,},);Opts::add_options(%opts);Opts::parse();Opts::validate();Util::connect();my $dc = Opts::get_option("datacenter");my $datacenter = Vim::find_entity_view ( view_type => "Datacenter",properties => [ "name" ],filter => { name => $dc },);my $clusters = Vim::find_entity_views ( view_type => "ClusterComputeResource",properties => [ "name","summary.numCpuCores","host" ],begin_entity => $datacenter,);foreach my $cluster (@$clusters){foreach my $host_mo_ref ( @{ $cluster->host } ){my $host_view = Vim::get_view(mo_ref => $host_mo_ref,properties => [ "vm" ],);foreach my $vm_mo_ref ( @{ $host_view->vm } ){my $vm_view = Vim::get_view(mo_ref => $vm_mo_ref,properties => [ "name", "summary.config.numCpu" ],);my $cpu = $vm_view->get_property("summary.config.numCpu");print $cluster->name."\t".$vm_view->name."\t".$cpu."\n";}}}Util::disconnect();
I tried to make it faster by pulling only the properties that are required. This script is running in the lab with good performacne. However, I am not sure whether i should use this script in the PROD with aobve said vCetner configuration.
Any suggestion would be immensly helpful.
Thanks.
Hi,
You don't need the host_view for this action. Why don't you use begin_entity for the vm views?
my $cluster ="cluster1";
my $cluster_view = Vim::find_entity_view ( view_type => 'ClusterComputeResource',
filter => { 'name' => qr/^$cluster/i } );
if($cluster_view) {
my $vm_views = Vim::find_entity_views( view_type => 'VirtualMachine',
begin_entity => $cluster_view,
properties => [ 'name' ] );
foreach my $vm_ref(@$vm_views) {
print $vm_ref->name."\n";
}
}
Hope that helps!
Greetings, Chris
Perfect. This is helpful. I was able to do the same thing with only 2 foreach loops instead of 3. I didnt think about "begin_entity" clause and was runing around the ManagedObjectRefence.
Many thnaks chris.
In my lab, my script was taking ~37-39 seconds to fetch the output.
With the changes you suggested, Now in script takes ~20 -22 seconds. This is a major improvement.
many thanks Chris.
Nice to hear, that it speeds up your script ![]()
Chris
