Dear Monks
I have this script
foreach (@$Datacenter_views)
{
my %seen;
my $DC = $_->name;
print "$DC, ";
# get all clusters under this datacenter
my $DC_view = Vim::find_entity_view(view_type => 'Datacenter', fil
+ter => { name => $DC });
my $Cluster_views = Vim::find_entity_views(view_type => 'ClusterCo
+mputeResource', begin_entity => $DC_view);
foreach my $cluster (@$Cluster_views)
{
my $Cluster_name = $cluster->name;
# printf "%s\n",$Cluster_name;
# get all hosts in this cluster
my $Cluster_view = Vim::find_entity_view(view_type => 'Cluster
+ComputeResource', filter => { name => $cluster->name });
my $Hosts_view = Vim::find_entity_views(view_type => 'HostSyst
+em', begin_entity => $Cluster_view);
# print all hosts in the cluster
foreach my $hosts (@$Hosts_view)
{
my $host_name = $hosts->name;
printf"%s, %s\n", $Cluster_name, $hosts->name;
$seen{$host_name} =1;
}
}
# get all hosts under this datacenter
my $hosts_views = Vim::find_entity_views(view_type => 'HostSystem'
+, begin_entity => $DC_view);
foreach my $hosts (@$hosts_views)
{
# This is the problem unless ( %seen)
{
printf "%s\n",$hosts->name;
}
}
exit();
}
I have couple of data centres, each data centre may or may not have clusters in them, and each data centre will have a number of hosts.
This command will get me a list of all clusters in a given data centre
my $Cluster_views = Vim::find_entity_views(view_type => 'ClusterCo
+mputeResource', begin_entity => $DC_view);
This command will get me all hosts in a given cluster name
my $Cluster_view = Vim::find_entity_view(view_type => 'Cluster
+ComputeResource', filter => { name => $cluster->name });
my $Hosts_view = Vim::find_entity_views(view_type => 'HostSyst
+em', begin_entity => $Cluster_view);
This command will get a list all hosts in a given data centre
my $hosts_views = Vim::find_entity_views(view_type => 'HostSystem'
+, begin_entity => $DC_view);
Now i want to print them out in this mannar:
Data Centre, Cluster, Host (only if the host is in a cluster)
Data centre, Host (only if the host not in a data centre)
So to avoid duplication, I thought before I print the list of hosts in the data center, which will also include all hosts in clusters, I get them marked with $seen{$host_name}=1.
then with this bit of code;
foreach my $hosts (@$hosts_views)
{
# This is the problem unless (! %seen)
{
printf "%s\n",$hosts->name;
}
}
}
I check if a host name is marked then it won’t print is unless its unmarked. It makes perfect sense to me, but Perl having none of that! It prints the whole list of hosts in the data centre regardless of the fact that they have already been printed as part of a cluster contents.
Can an enlightened one please help us out and show where I am going wrong?
Thanks