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

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

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

In this part, we’ll get started with performance counters. Your ESX system collects data at runtime about performance for all aspects of the system—CPU, disk, memory, network, and so on. The data in the counters is accessible through the PerformanceManager class.

You can get an instance of the PerformanceMangager like this:

ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);
PerformanceManager perfMgr = si.getPerformanceManager();

The performance manager has a getPerfCounters method which will return a list of all available counters:

PerfCounterInfo[] perfCounters = perfMgr.getPerfCounter();
for (int i = 0; i < perfCounters.length; i++) {
	PerfCounterInfo perfCounterInfo = perfCounters[i];
	String perfCounterString = perfCounterInfo.getNameInfo().getLabel() + " (" + perfCounterInfo.getGroupInfo().getKey() + ") in "
			+ perfCounterInfo.getUnitInfo().getLabel() + " (" + perfCounterInfo.getStatsType().toString() + ")";
		System.out.println(perfCounterInfo.getKey() + " : " + perfCounterString);
}

This will return a list like this:

1 : Usage (cpu) in Percent (rate)
...
336 : Queue write latency (disk) in Millisecond (absolute)
337 : Physical device command latency (disk) in Millisecond (absolute)
338 : Kernel command latency (disk) in Millisecond (absolute)

perfCounterInfo.getKey() returns the key with which you can identify the counter to get some information about the performance of the system.

Here the whole code for this example:

package com.benohead.esx;

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

import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.PerfCounterInfo;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.mo.PerformanceManager;
import com.vmware.vim25.mo.ServiceInstance;

public class ListCounters {

	static final String SERVER_NAME = "192.168.190.87";
	static final String USER_NAME = "Administrator";
	static final String PASSWORD = "xxxxxx";

	public static void main(String[] args) {
		String url = "https://" + SERVER_NAME + "/sdk/vimService";
		// List counters
		try {
			ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);
			PerformanceManager perfMgr = si.getPerformanceManager();

			PerfCounterInfo[] perfCounters = perfMgr.getPerfCounter();
			for (int i = 0; i < perfCounters.length; i++) {
				PerfCounterInfo perfCounterInfo = perfCounters[i];
				String perfCounterString = perfCounterInfo.getNameInfo().getLabel() + " (" + perfCounterInfo.getGroupInfo().getKey() + ") in "
						+ perfCounterInfo.getUnitInfo().getLabel() + " (" + perfCounterInfo.getStatsType().toString() + ")";
				System.out.println(perfCounterInfo.getKey() + " : " + perfCounterString);
			}
			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();
		}
	}

}

A query for performance counter is done using the PerformanceManager. In order to query for counters you need to create an instance of PerfQuerySpec which defines what we’ll be querying. You need to define the following:

  • which entity should be queried (in our case the host)
  • which metrics should be queried (e.g. the counter number 25 (active memory in KB)
  • the query interval: The interval (sampling period) in seconds for which performance statistics are queried. There is a set of intervals for historical statistics. To retrieve the greatest available level of detail, the provider’s refreshRate may be used for this property

First let’s get the metric ID we want to use to filter the results:

PerfMetricId[] queryAvailablePerfMetric = perfMgr.queryAvailablePerfMetric(host, null, null, perfInterval);
ArrayList<PerfMetricId> list = new ArrayList<PerfMetricId>();
for (int i2 = 0; i2 < queryAvailablePerfMetric.length; i2++) {
	PerfMetricId perfMetricId = queryAvailablePerfMetric[i2];
	if (SELECTED_COUNTER_ID == perfMetricId.getCounterId()) {
		list.add(perfMetricId);
	}
}
PerfMetricId[] pmis = list.toArray(new PerfMetricId[list.size()]);

where SELECTED_COUNTER_ID is a constant we’ve define containing the counter ID we want to retrieve (e.g. 25).

Then let’s get the interval ID:

PerfProviderSummary summary = perfMgr.queryPerfProviderSummary(host);
int perfInterval = summary.getRefreshRate();

This will return the greatest available level of detail (i.e. 20 seconds).

Now we can create a PerfQuerySpec:

PerfQuerySpec qSpec = new PerfQuerySpec();
qSpec.setEntity(host.getMOR());
qSpec.setMetricId(pmis);
qSpec.intervalId = perfInterval;

Running the query is just a call to the queryPerf method of the performance manager:

PerfEntityMetricBase[] pembs = perfMgr.queryPerf(new PerfQuerySpec[] { qSpec });

Now we can iterate through the results and print the counter values:

for (int i = 0; pembs != null && i < pembs.length; i++) {
	PerfEntityMetricBase val = pembs[i];
	PerfEntityMetric pem = (PerfEntityMetric) val;
	PerfMetricSeries[] vals = pem.getValue();
	PerfSampleInfo[] infos = pem.getSampleInfo();

	for (int j = 0; vals != null && j < vals.length; ++j) {
		PerfMetricIntSeries val1 = (PerfMetricIntSeries) vals[j];
		long[] longs = val1.getValue();
		for (int k = 0; k < longs.length; k++) {
			System.out.println(infos[k].getTimestamp().getTime() + " : " + longs[k]);
		}
		System.out.println();
	}
}

This will return something like:

Tue Jul 31 12:30:15 CEST 2012 : 1165824
Tue Jul 31 12:30:35 CEST 2012 : 1333596
Tue Jul 31 12:30:55 CEST 2012 : 1165824
Tue Jul 31 12:31:15 CEST 2012 : 1244900
Tue Jul 31 12:31:35 CEST 2012 : 1244900
Tue Jul 31 12:31:55 CEST 2012 : 1244900
Tue Jul 31 12:32:15 CEST 2012 : 1256692
Tue Jul 31 12:32:35 CEST 2012 : 1256692
Tue Jul 31 12:32:55 CEST 2012 : 1592236
Tue Jul 31 12:33:15 CEST 2012 : 1639196
Tue Jul 31 12:33:35 CEST 2012 : 1982884
Tue Jul 31 12:33:55 CEST 2012 : 1647340
Tue Jul 31 12:34:15 CEST 2012 : 1652860
Tue Jul 31 12:34:35 CEST 2012 : 1023712
Tue Jul 31 12:34:55 CEST 2012 : 1023712
Tue Jul 31 12:35:15 CEST 2012 : 949708
Tue Jul 31 12:35:35 CEST 2012 : 1201368
Tue Jul 31 12:35:55 CEST 2012 : 1209508
Tue Jul 31 12:36:15 CEST 2012 : 1209508
Tue Jul 31 12:36:35 CEST 2012 : 957852
Tue Jul 31 12:36:55 CEST 2012 : 974136
Tue Jul 31 12:37:15 CEST 2012 : 1009320
Tue Jul 31 12:37:35 CEST 2012 : 1040628
Tue Jul 31 12:37:55 CEST 2012 : 1040628
Tue Jul 31 12:38:15 CEST 2012 : 997300
Tue Jul 31 12:38:35 CEST 2012 : 1123128
Tue Jul 31 12:38:55 CEST 2012 : 1123128
Tue Jul 31 12:39:15 CEST 2012 : 1107476
Tue Jul 31 12:39:35 CEST 2012 : 1652732
Tue Jul 31 12:39:55 CEST 2012 : 1652732
Tue Jul 31 12:40:15 CEST 2012 : 1647840
Tue Jul 31 12:40:35 CEST 2012 : 1621556
Tue Jul 31 12:40:55 CEST 2012 : 1286012
Tue Jul 31 12:41:15 CEST 2012 : 1301032
Tue Jul 31 12:41:35 CEST 2012 : 1091316
Tue Jul 31 12:41:55 CEST 2012 : 1091316
Tue Jul 31 12:42:15 CEST 2012 : 1076296
Tue Jul 31 12:42:35 CEST 2012 : 1411840
Tue Jul 31 12:42:55 CEST 2012 : 1747384
Tue Jul 31 12:43:15 CEST 2012 : 1720340
Tue Jul 31 12:43:35 CEST 2012 : 1468684
Tue Jul 31 12:43:55 CEST 2012 : 1141280
Tue Jul 31 12:44:15 CEST 2012 : 1170316
Tue Jul 31 12:44:35 CEST 2012 : 1002540
Tue Jul 31 12:44:55 CEST 2012 : 1002540
Tue Jul 31 12:45:15 CEST 2012 : 1003172
Tue Jul 31 12:45:35 CEST 2012 : 919288
Tue Jul 31 12:45:55 CEST 2012 : 1087060
Tue Jul 31 12:46:15 CEST 2012 : 1057116
Tue Jul 31 12:46:35 CEST 2012 : 1308772
Tue Jul 31 12:46:55 CEST 2012 : 1141000
Tue Jul 31 12:47:15 CEST 2012 : 1133760
Tue Jul 31 12:47:35 CEST 2012 : 1133760
Tue Jul 31 12:47:55 CEST 2012 : 1133760
Tue Jul 31 12:48:15 CEST 2012 : 1292764
Tue Jul 31 12:48:35 CEST 2012 : 1208880
Tue Jul 31 12:48:55 CEST 2012 : 1208880
Tue Jul 31 12:49:15 CEST 2012 : 1264212
Tue Jul 31 12:49:35 CEST 2012 : 1515868
Tue Jul 31 12:49:55 CEST 2012 : 1524008
Tue Jul 31 12:50:15 CEST 2012 : 1340856
Tue Jul 31 12:50:35 CEST 2012 : 1718344
Tue Jul 31 12:50:55 CEST 2012 : 1550572
Tue Jul 31 12:51:15 CEST 2012 : 1513116
Tue Jul 31 12:51:35 CEST 2012 : 1515736
Tue Jul 31 12:51:55 CEST 2012 : 1431848
Tue Jul 31 12:52:15 CEST 2012 : 1662864
Tue Jul 31 12:52:35 CEST 2012 : 1411208
Tue Jul 31 12:52:55 CEST 2012 : 1662868
Tue Jul 31 12:53:15 CEST 2012 : 1480704
Tue Jul 31 12:53:35 CEST 2012 : 1564588
Tue Jul 31 12:53:55 CEST 2012 : 1483324
Tue Jul 31 12:54:15 CEST 2012 : 1426964
Tue Jul 31 12:54:35 CEST 2012 : 1343080
Tue Jul 31 12:54:55 CEST 2012 : 1190960
Tue Jul 31 12:55:15 CEST 2012 : 1182816
Tue Jul 31 12:55:35 CEST 2012 : 1098928
Tue Jul 31 12:55:55 CEST 2012 : 1157156
Tue Jul 31 12:56:15 CEST 2012 : 1165296
Tue Jul 31 12:56:35 CEST 2012 : 1416956
Tue Jul 31 12:56:55 CEST 2012 : 1291128
Tue Jul 31 12:57:15 CEST 2012 : 1282712
Tue Jul 31 12:57:35 CEST 2012 : 1114940
Tue Jul 31 12:57:55 CEST 2012 : 1031052
Tue Jul 31 12:58:15 CEST 2012 : 1039468
Tue Jul 31 12:58:35 CEST 2012 : 1139004
Tue Jul 31 12:58:55 CEST 2012 : 1139004
Tue Jul 31 12:59:15 CEST 2012 : 1196276
Tue Jul 31 12:59:35 CEST 2012 : 1206760
Tue Jul 31 12:59:55 CEST 2012 : 1332588
Tue Jul 31 13:00:15 CEST 2012 : 1291880
Tue Jul 31 13:00:35 CEST 2012 : 1291880
Tue Jul 31 13:00:55 CEST 2012 : 1585484
Tue Jul 31 13:01:15 CEST 2012 : 1534920
Tue Jul 31 13:01:35 CEST 2012 : 1534920
Tue Jul 31 13:01:55 CEST 2012 : 1660748
Tue Jul 31 13:02:15 CEST 2012 : 1694676
Tue Jul 31 13:02:35 CEST 2012 : 1789048
Tue Jul 31 13:02:55 CEST 2012 : 2040704
Tue Jul 31 13:03:15 CEST 2012 : 2040704
Tue Jul 31 13:03:35 CEST 2012 : 2292360
Tue Jul 31 13:03:55 CEST 2012 : 2124588
Tue Jul 31 13:04:15 CEST 2012 : 1836664
Tue Jul 31 13:04:35 CEST 2012 : 1532428
Tue Jul 31 13:04:55 CEST 2012 : 1532428
Tue Jul 31 13:05:15 CEST 2012 : 1406600
Tue Jul 31 13:05:35 CEST 2012 : 1409220
Tue Jul 31 13:05:55 CEST 2012 : 1414464
Tue Jul 31 13:06:15 CEST 2012 : 1307132
Tue Jul 31 13:06:35 CEST 2012 : 1223248
Tue Jul 31 13:06:55 CEST 2012 : 1055476
Tue Jul 31 13:07:15 CEST 2012 : 907644
Tue Jul 31 13:07:35 CEST 2012 : 991528
Tue Jul 31 13:07:55 CEST 2012 : 1007180
Tue Jul 31 13:08:15 CEST 2012 : 1071056
Tue Jul 31 13:08:35 CEST 2012 : 1322716
Tue Jul 31 13:08:55 CEST 2012 : 1322716
Tue Jul 31 13:09:15 CEST 2012 : 1293120
Tue Jul 31 13:09:35 CEST 2012 : 1377004
Tue Jul 31 13:09:55 CEST 2012 : 1379624
Tue Jul 31 13:10:15 CEST 2012 : 1481656
Tue Jul 31 13:10:35 CEST 2012 : 1649428
Tue Jul 31 13:10:55 CEST 2012 : 1649428
Tue Jul 31 13:11:15 CEST 2012 : 1665988
Tue Jul 31 13:11:35 CEST 2012 : 1414332
Tue Jul 31 13:11:55 CEST 2012 : 1414332
Tue Jul 31 13:12:15 CEST 2012 : 1325204
Tue Jul 31 13:12:35 CEST 2012 : 1249460
Tue Jul 31 13:12:55 CEST 2012 : 1417232
Tue Jul 31 13:13:15 CEST 2012 : 1362736
Tue Jul 31 13:13:35 CEST 2012 : 1619412
Tue Jul 31 13:13:55 CEST 2012 : 1619412
Tue Jul 31 13:14:15 CEST 2012 : 2476068
Tue Jul 31 13:14:35 CEST 2012 : 2476068
Tue Jul 31 13:14:55 CEST 2012 : 2308296
Tue Jul 31 13:15:15 CEST 2012 : 2867220
Tue Jul 31 13:15:35 CEST 2012 : 2752024
Tue Jul 31 13:15:55 CEST 2012 : 2754644
Tue Jul 31 13:16:15 CEST 2012 : 3030724
Tue Jul 31 13:16:35 CEST 2012 : 2870816
Tue Jul 31 13:16:55 CEST 2012 : 3208984
Tue Jul 31 13:17:15 CEST 2012 : 3033068
Tue Jul 31 13:17:35 CEST 2012 : 3316036
Tue Jul 31 13:17:55 CEST 2012 : 3148264
Tue Jul 31 13:18:15 CEST 2012 : 2873848
Tue Jul 31 13:18:35 CEST 2012 : 3109852
Tue Jul 31 13:18:55 CEST 2012 : 3109852
Tue Jul 31 13:19:15 CEST 2012 : 3065288
Tue Jul 31 13:19:35 CEST 2012 : 2394200
Tue Jul 31 13:19:55 CEST 2012 : 2394200
Tue Jul 31 13:20:15 CEST 2012 : 2360120
Tue Jul 31 13:20:35 CEST 2012 : 2527892
Tue Jul 31 13:20:55 CEST 2012 : 2527892
Tue Jul 31 13:21:15 CEST 2012 : 2684896
Tue Jul 31 13:21:35 CEST 2012 : 2113348
Tue Jul 31 13:21:55 CEST 2012 : 1945576
Tue Jul 31 13:22:15 CEST 2012 : 1910668
Tue Jul 31 13:22:35 CEST 2012 : 1826780
Tue Jul 31 13:22:55 CEST 2012 : 1826780
Tue Jul 31 13:23:15 CEST 2012 : 1807876
Tue Jul 31 13:23:35 CEST 2012 : 2117132
Tue Jul 31 13:23:55 CEST 2012 : 2452676
Tue Jul 31 13:24:15 CEST 2012 : 2447156
Tue Jul 31 13:24:35 CEST 2012 : 2442140
Tue Jul 31 13:24:55 CEST 2012 : 2190480
Tue Jul 31 13:25:15 CEST 2012 : 2182336
Tue Jul 31 13:25:35 CEST 2012 : 2271240
Tue Jul 31 13:25:55 CEST 2012 : 2271240
Tue Jul 31 13:26:15 CEST 2012 : 2260480
Tue Jul 31 13:26:35 CEST 2012 : 2092708
Tue Jul 31 13:26:55 CEST 2012 : 1715224
Tue Jul 31 13:27:15 CEST 2012 : 1712324
Tue Jul 31 13:27:35 CEST 2012 : 1681020
Tue Jul 31 13:27:55 CEST 2012 : 1848792
Tue Jul 31 13:28:15 CEST 2012 : 1867976
Tue Jul 31 13:28:35 CEST 2012 : 2043612
Tue Jul 31 13:28:55 CEST 2012 : 1917780
Tue Jul 31 13:29:15 CEST 2012 : 1917780
Tue Jul 31 13:29:35 CEST 2012 : 1781312
Tue Jul 31 13:29:55 CEST 2012 : 1781312

Here the complete code for the counter query:

package com.benohead.esx;

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

import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.PerfEntityMetric;
import com.vmware.vim25.PerfEntityMetricBase;
import com.vmware.vim25.PerfMetricId;
import com.vmware.vim25.PerfMetricIntSeries;
import com.vmware.vim25.PerfMetricSeries;
import com.vmware.vim25.PerfProviderSummary;
import com.vmware.vim25.PerfQuerySpec;
import com.vmware.vim25.PerfSampleInfo;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.PerformanceManager;
import com.vmware.vim25.mo.ServiceInstance;

public class DiplayHostCounter {

	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";
	private static final int SELECTED_COUNTER_ID = 25; // Active (mem) in KB (absolute)

	public static void main(String[] args) {
		String url = "https://" + SERVER_NAME + "/sdk/vimService";
		try {
			ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);
			HostSystem host = (HostSystem) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("HostSystem", HOSTNAME);
			PerformanceManager perfMgr = si.getPerformanceManager();
			System.out.println("Counter ID = " + SELECTED_COUNTER_ID);

			PerfProviderSummary summary = perfMgr.queryPerfProviderSummary(host);
			int perfInterval = summary.getRefreshRate();
			System.out.println("Refresh rate = " + perfInterval);

			PerfMetricId[] queryAvailablePerfMetric = perfMgr.queryAvailablePerfMetric(host, null, null, perfInterval);
			ArrayList<PerfMetricId> list = new ArrayList<PerfMetricId>();
			for (int i2 = 0; i2 < queryAvailablePerfMetric.length; i2++) {
				PerfMetricId perfMetricId = queryAvailablePerfMetric[i2];
				if (SELECTED_COUNTER_ID == perfMetricId.getCounterId()) {
					list.add(perfMetricId);
				}
			}
			PerfMetricId[] pmis = list.toArray(new PerfMetricId[list.size()]);

			PerfQuerySpec qSpec = new PerfQuerySpec();
			qSpec.setEntity(host.getMOR());
			qSpec.setMetricId(pmis);
			qSpec.intervalId = perfInterval;

			PerfEntityMetricBase[] pembs = perfMgr.queryPerf(new PerfQuerySpec[] { qSpec });
			for (int i = 0; pembs != null && i < pembs.length; i++) {
				PerfEntityMetricBase val = pembs[i];
				PerfEntityMetric pem = (PerfEntityMetric) val;
				PerfMetricSeries[] vals = pem.getValue();
				PerfSampleInfo[] infos = pem.getSampleInfo();

				for (int j = 0; vals != null && j < vals.length; ++j) {
					PerfMetricIntSeries val1 = (PerfMetricIntSeries) vals[j];
					long[] longs = val1.getValue();
					for (int k = 0; k < longs.length; k++) {
						System.out.println(infos[k].getTimestamp().getTime() + " : " + longs[k]);
					}
					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();
		}
	}
}

In the vShpere client, it looks like this:

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

  1. Hi,

    In my application I am fetching the details of various counters in a similar technique. But when I fetch the data related to any usage (in percent) counters, I see that if the data on the VCenter chart is 25.60, the data that my program returns is 2560. Is there any reason for this? Do I have to divide the value retrieved by 100. If yes, I would like to know why.

    Any help from your end will be truly appreciated. Thanks in advance!

Leave a Reply

Your email address will not be published.