How to get Datastore Summary for all data-stores connected to a ESXi host using vSphere API

This post is motivated by this VMTN thread , also I wanted to start with small tutorials on vSphere APIs using JAVA, I thought lets start with answering this query which can cover tutorial on DatastoreSummary data object as well. I hope you have already setup your eclipse environment by referring my  Getting started  blog post. Here we go.

Query on VMTN was: How to get datastore type connected to ESXi programmatically using vSphere APIs. As datastore type is part of DatastoreSummary data object, lets see how to get datastore summary (Datastore Name, Datastore Type, Datastore Capacity, Datastore free capacity etc.)

DatastoreSummary Data Object Description:

Datastore Summary Data object description

Complete code :


public class DatastoreSummary {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: java SearchDatastore "
+ "username password");

* you need to pass 3 parameters
* 1. https://ESXi_IP/sdk
* 2. username
* 3. password
ServiceInstance si = new ServiceInstance(new URL(args[0]), args[1],
args[2], true);
String hostname = ""; //Pass the FQDN i.e. DNS name of the ESXi host, ESXi host IP will not work
Folder rootFolder = si.getRootFolder();
HostSystem host = null;

host = (HostSystem) new InventoryNavigator(rootFolder)
.searchManagedEntity("HostSystem", hostname);

if (host == null) {
System.out.println("Host not found");

HostDatastoreBrowser hdb = host.getDatastoreBrowser();

System.out.println("Datastore Summary connected to ESXi host");
Datastore[] ds = hdb.getDatastores();

for (int i = 0; ds != null && i < ds.length; i++) {
System.out.println("DatastoreName:" + ds[i].getName() + " "
+ "DSType:" + ds[i].getSummary().getType() + " "
+ "TotalCapacity(in GB):"
+ (ds[i].getSummary().getCapacity()) / (1024 * 1024 * 1024)
+ " " + "FreeSpace (in GB): "
+ (ds[i].getSummary().getFreeSpace())
/ (1024 * 1024 * 1024) + " ");



Program Output::

Datastore Summary connected to host

DatastoreName:Local Datastore DSType:VMFS TotalCapacity(in GB):131 FreeSpace (in GB): 130

DatastoreName:Shared DS_3 DSType:VMFS TotalCapacity(in GB):24 FreeSpace (in GB): 8

DatastoreName:Shared DS_1 DSType:VMFS TotalCapacity(in GB):49 FreeSpace (in GB): 8

DatastoreName:Shared DS_4 DSType:VMFS TotalCapacity(in GB):49 FreeSpace (in GB): 13

DatastoreName:Shared DS_2 DSType:VMFS TotalCapacity(in GB):49 FreeSpace (in GB): 15

Compare output with datastore summary from VI client
Datastore summary

You could see output is matching with screenshot from VI client, is not it cool?

Refer: vSphere API reference documentation I highly recommend you to spend some time on getting familiarize with vSPhere API reference.

Stay tuned for more specific/basic tutorials. please let me know if you want specific tutorial on vSphere API

2 thoughts on “How to get Datastore Summary for all data-stores connected to a ESXi host using vSphere API

  1. freespace is showing different value with different esxi hosts. could you please explain why it happens so

    1. do you mean as follows?

      Datastore is shared with multiple hosts and when you get datastore free space connecting to 1st host is different from when retrieved the free space for same datastore but connecting from 2nd host? Let me know what version of VC/ESXi you are using as well.

Comments are closed.