Tag Archives: getResourceUsage

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