Getting info from your ESX server using the VMware Infrastructure (vSphere) Java API (Part 1)

VMware Infrastructure (vSphere) Java API provides a full set of libraries to manage VMware Infrastructure and vSphere. It is hosted on.

In order to use it you have to add both the vijava5 and dom4j jar files to your classpath.

First to connect to the ESX server, you need to get an instance of the vim server:

String url = "https://" + SERVER_NAME + "/sdk/vimService";
ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);

You should use the username and password you use in the VMWare vSphere Client. The server name is either the name or IP address of the ESX server or of the vCenter Server (if you manage multiple hosts).

Now you can use the InventoryNavigator to retrieve container contents from specified parent i.e. get data regarding hosts, virtual machines…

For example, if you want to get the list of hosts, you can use the following:

ManagedEntity[] managedEntities = new InventoryNavigator(si.getRootFolder()).searchManagedEntities("HostSystem");
for (ManagedEntity managedEntity : managedEntities) {
	HostSystem host = (HostSystem) managedEntity;
	System.out.println("Host: '" + host.getName() + "'");
}

This will return something like:

Host: '192.168.190.34'
Host: '192.168.190.40'

You basically always use new InventoryNavigator(rootFolder).searchManagedEntities(…) to get objects corresponding to the different entities in your virtual infrastucture:

  • HostSystem
  • Datacenter
  • VirtualMachine
  • ResourcePool

Once you have the entity you’re looking for you can navigate through the object tree to get more data e.g. if you want to get some info about the hardware of each host:

HostHardwareInfo hw = host.getHardware();
System.out.println("Model: " + hw.getSystemInfo().getModel());
System.out.println("Memory in Bytes: " + hw.getMemorySize());
System.out.println("# of CPU Cores: " + hw.getCpuInfo().getNumCpuCores());
HostCpuPackage[] cpuPkg = hw.getCpuPkg();
for (int i = 0; i < cpuPkg.length; i++) {
	HostCpuPackage pkg = cpuPkg[i];
	System.out.println(pkg.getIndex() + " : " + pkg.getDescription() + " (vendor: " + pkg.getVendor() + ")");
}

This will return the following for host 192.168.190.34:

Model: ProLiant DL380 G6
Memory in Bytes: 42937085952
# of CPU Cores: 8
0 : Intel(R) Xeon(R) CPU           E5530  @ 2.40GHz (vendor: intel)
1 : Intel(R) Xeon(R) CPU           E5530  @ 2.40GHz (vendor: intel)

Here the complete sample code:

package com.benohead.esx;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import com.vmware.vim25.HostCpuPackage;
import com.vmware.vim25.HostHardwareInfo;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;

public class ListHosts {
	static final String SERVER_NAME = "192.168.190.87";
	static final String USER_NAME = "Administrator";
	static final String PASSWORD = "AdminXXXX";

	public static void main(String[] args) {
		String url = "https://" + SERVER_NAME + "/sdk/vimService";
		// List host systems
		try {
			ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);
			ManagedEntity[] managedEntities = new InventoryNavigator(si.getRootFolder()).searchManagedEntities("HostSystem");
			for (ManagedEntity managedEntity : managedEntities) {
				HostSystem host = (HostSystem) managedEntity;
				System.out.println("Host: '" + host.getName() + "'");
				HostHardwareInfo hw = host.getHardware();
				System.out.println("Model: " + hw.getSystemInfo().getModel());
				System.out.println("Memory in Bytes: " + hw.getMemorySize());
				System.out.println("# of CPU Cores: " + hw.getCpuInfo().getNumCpuCores());
				HostCpuPackage[] cpuPkg = hw.getCpuPkg();
				for (int i = 0; i < cpuPkg.length; i++) {
					HostCpuPackage pkg = cpuPkg[i];
					System.out.println(pkg.getIndex() + " : " + pkg.getDescription() + " (vendor: " + pkg.getVendor() + ")");
				}
			}
			si.getServerConnection().logout();
		} catch (InvalidProperty e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RuntimeFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

7 thoughts on “Getting info from your ESX server using the VMware Infrastructure (vSphere) Java API (Part 1)

  1. Thanks for this post. It is very useful. I will try the sample code. Fortunately I will not need to do much changes in the code ;).

    1. Hope I’ll have some time to write the second part regarding how to extract counters (CPU, memory…). Guess this will be especially interesting for you…

  2. How can I get the username and password of esx hosts in the vcentre?
    I need to return the username and password of esx host to another module which can use it to login into esx host and deploy an ovf.

Leave a Reply

Your email address will not be published. Required fields are marked *