VMware {code} Community
mickmason
Contributor
Contributor
Jump to solution

Obtain the name of the cluster on which a Virtual Machine resides by using the virtual machine name

Hi,

I've written some code to get various bits of info about our VMs for use in capacity planning, We have 5 Virtual Centers, each one has at least two clusters.

My code already gets a handle to the Virtual Machine object (using the code snippet below) from which i can get the hostname. Is there anyway to determine which cluster this host resides on, for instance, is there a property of the Virtual Machine object that can be used to obtain this info?

public void connectVC()

{

string URI = "https://" + Server + "/sdk";

client.Connect(URI);

client.Login(UserName, Password);

}

public void get_vms()

{

NameValueCollection filter = new NameValueCollection();

filter.Add("name", "^wks");

IList<EntityViewBase> vmList = client.FindEntityViews(typeof(VirtualMachine), null, filter, null);

foreach (VirtualMachine vm in vmList)

{

'Do Stuff

}

Thanks

Mick

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
lamw
Community Manager
Community Manager
Jump to solution

Take a look at this Perl script that retrieves what you're looking for, you may need to translate this into another language if you're not using the Perl bindings:

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

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".

View solution in original post

0 Kudos
4 Replies
mickmason
Contributor
Contributor
Jump to solution

I have now got a handle to the HostSystem using the code below. Does the HostSystem object have any properties that will allow me to determine the name of the cluster on which it is hosted?

machine.Name = vm.Name;

ManagedObjectReference mor_host = vm.Runtime.Host;

string[] props = new string[]{ "name","summary","config","runtime","network","vm" };

HostSystem hs= (HostSystem)client.GetView(mor_host, props);

Cheers

M

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

afaik, there is no property in the HostSystem that allows you to correlate which vCenter cluster it's part of, this is a feature of vCenter only. What you can do is go from the VM->ESX(i) host then you'll need to retrieve all clusters from vCenter and then check to see which cluster the host is part of and break out of the loop once you've found it.

In Perl you would do something of the following to get all Clusters:

$cluster_views = Vim::find_entity_views(view_type => 'ClusterComputeResource');

As you can see from here: http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ComputeResource.html one of the properties the Cluster holds is an array of hosts attached and you can easily match up the name.

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

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".

0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Take a look at this Perl script that retrieves what you're looking for, you may need to translate this into another language if you're not using the Perl bindings:

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

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".

0 Kudos
mickmason
Contributor
Contributor
Jump to solution

Thanks.

I'd already figured it out by the time I read your solution, but I have gone a different route.

By walking backwards up the tree (using the Parent property), I was able to grab the cluster name.

//get a list of all VMs. The filter is set to only get workstations

IList<EntityViewBase> vmList = client.FindEntityViews(typeof(VirtualMachine), null, filter, null);

foreach (VirtualMachine vm in vmList)

{

//instantiates a _vm object that is used to hold the details of the VirtualMachine

_vm machine = new _vm();

DateTime? dt = vm.Summary.Runtime.BootTime;

machine.Name = vm.Name;

//Get host server name

ManagedObjectReference mor_host = vm.Runtime.Host;

string[] props = new string[] { "name", "summary", "config", "runtime", "network", "vm" };

HostSystem hs = (HostSystem)client.GetView(mor_host, props);

string hn = hs.Name;

//This checks 2 dictionaries (one for datacenter, and one for cluster) to make sure the hostname hasn't already been added

if (!dicClusterInfo.ContainsKey(hs.Name) && !dicDCInfo.ContainsKey(hs.Name))

{

//This gets a handle to the specific virtual machine

NameValueCollection hostFilter = new NameValueCollection();

hostFilter.Add("name", hs.Name);

string[] parentProps = new string[] { "name", "parent" };

IList<EntityViewBase> hsList = client.FindEntityViews(typeof(HostSystem), null, hostFilter, parentProps);

foreach (HostSystem h in hsList)

{

//This gets the parent of the guest machine, which is the cluster

ManagedObjectReference mor_clu = h.Parent;

ClusterComputeResource clust = (ClusterComputeResource)client.GetView(mor_clu, parentProps);

machine.ClusterName = clust.Name;

dicClusterInfo.Add(hs.Name, clust.Name);


This gets the parent of the cluster, which is a folder

ManagedObjectReference mor_fld = clust.Parent;

Folder fld = (Folder)client.GetView(mor_fld, parentProps);


This gets the parent of the folder, which is the Datacenter

ManagedObjectReference mor_fldParent = fld.Parent;

Folder fld2 = (Folder)client.GetView(mor_fldParent, parentProps);

ManagedObjectReference mor_dc = fld2.Parent;

Datacenter dc = (Datacenter)client.GetView(mor_dc, parentProps);

machine.folderName = fld.Name;

machine.DataCenter = dc.Name;

dicDCInfo.Add(hs.Name, dc.Name);

}

}

That's probably as clear as mud, but hopefully will be of some use to someone.

M

0 Kudos