How to quickly get vSphere Cluster resource usage( cpu, mem, storage) using API: cool API in vSphere 6.0

Some time back, I wanted to get the cluster resource usage (cpu, memory, storage)for one of my project requirements. I was exploring the ways to get cluster resource usage and to my surprise, vSphere 6.0 has introduced new API which can fetch detailed cluster resource usage. It was tricky to get this info earlier and now single API call will help to get this. How cool it is! If you are not using vSphere HA/DRS, this API is super life saver for you in order to keep the track of the cluster resources and monitor these resources on all the cluster across vCenter. New method that I am talking about is “getResourceUsage()”. This method is introduced in vSphere 6.0 under “ClusterComputeResource” managed object. It is mean that you need to have this managed object to call this useful method. i.e. getResourceUsage(). This API can fetch not only the resource capacity but also current resource usage.

Below is the complete code sample which can help you quickly to get Cluster resource usage (cpu,memory and storage)..

[java]

//:: # Author: Vikas Shitole
//:: # Website: www.vThinkBeyondVM.com
//:: # Product/Feature: vCenter Server/DRS
//:: # Description: Script to quickly get Cluster resource usage (cpu,memory and storage).

package com.vmware.yavijava;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import com.vmware.vim25.ClusterResourceUsageSummary;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ServiceInstance;

public class GetClusterResourceUsage {

public static void main(String[] args) throws Exception {
if(args.length!=3)
{
System.out.println("Usage: GetClusterResourceUsage url username password");
System.exit(-1);
}

URL url = null;
try
{
url = new URL(args[0]);
} catch ( MalformedURLException urlE)
{
System.out.println("The URL provided is NOT valid. Please check it…");
System.exit(-1);
}
String username = args[1];
String password = args[2];
String ClusterName = "Cluster1"; //Your cluster Name

// Initialize the system, set up web services
ServiceInstance si = new ServiceInstance(url, username,
password, true);
Folder rootFolder = si.getRootFolder();

ClusterComputeResource cluster = null;
cluster = (ClusterComputeResource) new InventoryNavigator(rootFolder)
.searchManagedEntity("ClusterComputeResource", ClusterName);

System.out.println();
System.out.println("Resource Usagae Summary for cluster::"+cluster.getName());
//Get the Cluster resource summary object
ClusterResourceUsageSummary resourceSummary= cluster.getResourceUsage();
System.out.println("CPU Capacity::"+resourceSummary.getCpuCapacityMHz()+" MHz");
System.out.println("CPU used::"+resourceSummary.getCpuUsedMHz()+" MHz");
System.out.println("Memory Capacity::"+resourceSummary.getMemCapacityMB()+" MB");
System.out.println("Memory used::"+resourceSummary.getMemUsedMB()+" MB");
System.out.println("Storage Capacity::"+resourceSummary.getStorageCapacityMB()+" MB");
System.out.println("Storage used::"+resourceSummary.getStorageUsedMB()+" MB");
si.getServerConnection().logout();
}
}

[/java]

Note:
– For the sake of simplicity, I have hard-coded DRS cluster name, you can change it based on your environment.
– You can scale the same code to all the clusters in a datacenter or multiple datacenter.

If you have still not setup your YAVI JAVA Eclipse environment:Getting started tutorial

Important tutorials to start with: Part I & Part II

4 thoughts on “How to quickly get vSphere Cluster resource usage( cpu, mem, storage) using API: cool API in vSphere 6.0

  1. Hi Vikas

    This is a great post again, have you had to get the same cluster information but making use of pyvmomi? I currently make use of a PowerCli script to export loads of cluster related information daily, but as the process is taking 4+hours over 14vc’s we started experimenting with python to see if we can replace the original PowerCli script.

    Any advise or response will be appreciated.

  2. Hi Johan,

    Thanks for your comment. Its same API ClusterResourceUsageSummary resourceSummary= cluster.getResourceUsage(); available in python SDK as well and it should be easy to implement there. If you need help on python for this particular cluster API, please let me know. Also, In powerCLi as well, we can do programming directly using .Net View objects (instead of PowerCLI cmdlets). PowerCLI .Net View objects have 1:1 mapping with APIs.

    In this week, I will publish another blog post which will demonstrate how to achieve (getResourceUsage) using PowerCLI .Net view objects (no cmdlets) with one more small script.

    1. Hi Vikas, I have been trying to get this translated to pyvomomi and facing trouble with the below lines

      ClusterComputeResource cluster = null;
      cluster = (ClusterComputeResource) new InventoryNavigator(rootFolder)
      .searchManagedEntity(“ClusterComputeResource”, ClusterName);

      But have been facing difficulty. Could you please take a look and help me out if possible.

  3. which vijava version you used to show this class and method ClusterResourceUsageSummary resourceSummary= cluster.getResourceUsage(); ?

Comments are closed.