VMware Cloud Community
vtphilk
Contributor
Contributor

Determine ESXi host Guest is Running on from VCenter

Is there a way to poll what ESX and it's hostname a vcenter is hosting a Guest from assuming you had privledges to do it?  I'm running VNC ports on my ESX hosts and would like to automate in my perl script the grabbing of the ESXi host that is 'hosting' the vnc port for each image.  However, I cannot find anywhere the module or function to call for determining the host a guest is running on.

Thanks!

0 Kudos
6 Replies
lamw
Community Manager
Community Manager

I think I understand your ask, but just let me clarify.

Are you trying to determine the ESX(i) host a given VM is running on using only vCenter? If so, yes this is possible.

In VirtualMachine managed object -http://vijava.sourceforge.net/vSphereAPIDoc/ver5/ReferenceGuide/vim.VirtualMachine.html there is a property under runtime->host (http://vijava.sourceforge.net/vSphereAPIDoc/ver5/ReferenceGuide/vim.vm.RuntimeInfo.html) which gives you a reference to the ESX(i) host that is running it.

0 Kudos
vtphilk
Contributor
Contributor

I will give that a shot however I have not tried to use the java api stuff yet...  Any idea how to do it in the perl SDK?  All my scripts are running in perl using the VMware-vSphere-CLI-5.0.0-422456.

0 Kudos
lamw
Community Manager
Community Manager

The link is not related to VI Java, it's just that API Reference Guide which is applicable to any of the VMware SDK including the vSphere SDK for Perl.

It's easier to link to using the old style API reference guide.

$hostRef = Vim::get_view(mo_ref = $vm->runtime->host)
vtphilk
Contributor
Contributor

Hrmm..that returns something:

'host' => bless( {
                                                                                  'value' => 'host-12',
                                                                                  'type' => 'HostSystem'
                                                                                }, 'ManagedObjectReference' ),

not sure what 'host-12' is and how to get that to a hostname thou.  The idea here is we have a vcenter with 3 hosts.  The guest's are spread out across and have vnc ports open with the RemoteDisplay vmx mod.  I want to be able to dynamically tell which host the image is on so I know where to VNC to .

0 Kudos
lamw
Community Manager
Community Manager

Yes, it returns you a reference to the ESX(i) host, host-12 is just the MoRef ID. I assume since you mentioned Perl SDK, that you're familar with the syntax.

To get the system name, you can do the following:

$systemName = $host->name;

vtphilk
Contributor
Contributor

I will play with it and get my perl guy to help.  Maybe I lead on my skills a little Smiley Happy.  I just have been using the vminfo.pl script that the perl SDK has as an example and editing that to get the host info above.  I'm not certain I can find the right syntaxt to pull the hostname from that  Smiley Sad

Thanks !!  I think this is what I needed.  If i get a complete example working I'll try and post..if anyone has anything that would work I would appreciate it!

0 Kudos