VMware {code} Community
nd81
Contributor
Contributor

List all HostSystems under a DataCenter

how does one list all the HostSystems under a given DataCenter ?

I know that I can do find_entity_views(view_type => 'HostSystem')...but I want to discover hosts systems based

on what datacenters I get.

Datacenter managed object does not have any property or method to list HostSystem

my sample code -

my $datacenter = $s->find_entity_views(view_type => 'Datacenter');

foreach (@$datacenter) {

print $_->name,"\n";

}

Reply
0 Kudos
1 Reply
stumpr
Virtuoso
Virtuoso

#!/usr/bin/perl

use strict;
use warnings;

use VMware::VIRuntime;

my %opts = (
	dcname => {
	type => "=s",
	variable => "DCNAME",
	help => "Datacenter display name",
	required => 1,
	}
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();

my $dc_name = Opts::get_option("dcname");

Util::connect();

my ($dc_view, $host_views);

$dc_view = Vim::find_entity_view( view_type => "Datacenter",
									properties => ['name'
                                                                          ], # put on new line to avoid forum mangling
									filter => { 'name' => $dc_name } );

unless ($dc_view) {
	die "Unable to find Datacenter: \'$dc_name\'!";
}

$host_views = Vim::find_entity_views( view_type => "HostSystem",
										properties => ['name'
                                                                                ], #put on new line to avoid forum mangling
										begin_entity => $dc_view );
						
print "Datacenter: $dc_name\n";				
foreach ( @{$host_views} ) {
	print "   Host: " . $_->name . "\n";
}

Essentially the idea is to get a MO-ref to your Datacenter, then use it as a begin_entity in your find_entity_views call for the HostSystems.

Now if you want to sort of reproduce the inventory tree, you can also just traverse the folder structure. I created this script for traversing folders for Virtual Machines. You can adapt it a bit for Hosts, however, you'll have to be aware of ComputeResource and ClusterComputeResource objects.

Then on a more advanced angle, some people have created a single RetrieveProperties call to get all the objects in inventory and their parent-child relationships to build the tree. This is mostly aimed at reducing networking overhead in multiple SOAP calls.

Unless you have a lot of Datacenters, I would use the begin_entity method I posted. If you have a very large set of Datacenters, then you might want to consider the other options if you hit a performance bottleneck.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos