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

In the first part, we’ve seen how to get general information about the ESX hosts.

In this part, we’ll read some information about the virtual machines running on a given host.

VirtualMachines like almost everything in ESX are managed entities. So you can query them using InventoryNavigator.searchManagedEntities:

ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);
HostSystem host = (HostSystem) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("HostSystem", HOSTNAME);
ManagedEntity[] managedEntities = new InventoryNavigator(host).searchManagedEntities("VirtualMachine");

Note: if you use si.getRootFolder() instead of host you will get all VMs running on all hosts managed by your ESX infrastructure.

Once you have a virtual machine, you can get the virtual machine configuration information using:

VirtualMachineConfigInfo config = vm.getConfig();

And from this VirtualMachineConfigInfo you can get data related to the configured virtual hardware:

private static void writeVmInfo(VirtualMachine vm) {
	System.out.println("Virtual Machine:" + vm.getName());
	VirtualMachineConfigInfo config = vm.getConfig();
	VirtualHardware hw = config.getHardware();
	System.out.println("Memory in MB: " + hw.getMemoryMB());
	System.out.println("# of CPUs: " + hw.getNumCPU());
	VirtualDevice[] devices = hw.getDevice();
	for (int i = 0; i < devices.length; i++) {
		VirtualDevice device = devices[i];
		Description deviceInfo = device.getDeviceInfo();
		System.out.println("Device (" + device.getKey() + "): " + deviceInfo.getLabel() + " : " + deviceInfo.getSummary());
	}
}

The virtual machine configuration information contains more than just the virtual hardware configuration:

  • Affinity settings for CPU and memory
  • Resource limits for CPU and memory
  • Datastores
  • And more…

Here the full code of our example class:

package com.benohead.esx;

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

import com.vmware.vim25.Description;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.VirtualDevice;
import com.vmware.vim25.VirtualHardware;
import com.vmware.vim25.VirtualMachineConfigInfo;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class ListVms {
	static final String SERVER_NAME = "192.168.190.87";
	static final String USER_NAME = "Administrator";
	static final String PASSWORD = "xxxxxx";
	private static final String HOSTNAME = "192.168.190.34";

	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);
			HostSystem host = (HostSystem) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("HostSystem", HOSTNAME);
			ManagedEntity[] managedEntities = new InventoryNavigator(host).searchManagedEntities("VirtualMachine");
			for (int i = 0; i < managedEntities.length; i++) {
				VirtualMachine vm = (VirtualMachine) managedEntities[i];
				writeVmInfo(vm);
				System.out.println();
			}
			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();
		}
	}

	private static void writeVmInfo(VirtualMachine vm) {
		System.out.println("Virtual Machine:" + vm.getName());
		VirtualMachineConfigInfo config = vm.getConfig();
		VirtualHardware hw = config.getHardware();
		System.out.println("Memory in MB: " + hw.getMemoryMB());
		System.out.println("# of CPUs: " + hw.getNumCPU());
		VirtualDevice[] devices = hw.getDevice();
		for (int i = 0; i < devices.length; i++) {
			VirtualDevice device = devices[i];
			Description deviceInfo = device.getDeviceInfo();
			System.out.println("Device (" + device.getKey() + "): " + deviceInfo.getLabel() + " : " + deviceInfo.getSummary());
		}
	}
}

Leave a Reply

Your email address will not be published.