Tag Archives: DRS affinity rules

How to get all DRS rules associated with each VM in a DRS cluster using API : Interesting API in vSphere 6.0

As we know, one VM can be associated with several DRS affinity rules. If your DRS cluster has several rules configured, it is really cumbersome to check what is the type of the rule  and how many rules are associated with each VM in a DRS cluster. There is a interesting method introduced in vSphere 6.0 which can address this pain point. New method that I am talking about is “findRulesForVm()”. This method is introduced in vSphere 6.0 under “ClusterComputeResource” managed object. It is mean that you need to have this managed object in order to call this useful method. i.e. findRulesForVm().

Note: This method can list all the VM-VM affinity/anti-affinity rules associated with particular VM. This method can not return the VM-Host affinity rules associated with the VM.

Below is the complete code sample which can help to get all the VM VM DRS rules associated with each VM in Cluster. Please do modify this sample as per your need.

[java]
//:: # Author: Vikas Shitole
//:: # Website: www.vThinkBeyondVM.com
//:: # Product/Feature: vCenter Server/DRS
//:: # Description: Script to find all the rules associated with all the VMs in the DRS cluster

package com.vmware.yavijava;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import com.vmware.vim25.ArrayUpdateOperation;
import com.vmware.vim25.ClusterAffinityRuleSpec;
import com.vmware.vim25.ClusterAntiAffinityRuleSpec;
import com.vmware.vim25.ClusterConfigSpec;
import com.vmware.vim25.ClusterRuleInfo;
import com.vmware.vim25.ClusterRuleSpec;
import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.ManagedObjectReference;
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.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;
import com.vmware.vim25.mo.util.MorUtil;

public class FindRulesForVMsInCluster {

public static void main(String[] args) throws Exception {
if(args.length!=3)
{
System.out.println("Usage: FindRulesForVMInCluster 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 = "My-Cluster"; //Your DRS cluster Name
ManagedEntity vms[] = null;

// 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("Cluster name::"+cluster.getName());

vms = (ManagedEntity[]) new InventoryNavigator(cluster)
.searchManagedEntities("VirtualMachine");

for(ManagedEntity vm:vms){
//New vSphere 6.0 method to find the VM affinity rules associated with particular VM
ClusterRuleInfo[] rules=cluster.findRulesForVm((VirtualMachine) vm);
System.out.println("Rules assciated with VM "+vm.getName()+" are::");
for(ClusterRuleInfo rule:rules){
System.out.println(rule.getName()+":" + rule.getEnabled());
}
System.out.println("================================");
}
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