VMware {code} Community
kaartik
Contributor
Contributor
Jump to solution

Perl script to list DRS affinity rules

I have a script to query DRS configurations, but I am looking for a way to access this object to list the affinity /anti affinity rules in DRS.

clustercomputeresource->configuration->rule

name

enabled

status

Does anybody have a perl script to list the affinity rules / antiaffinity rules?

Thks,

Reply
0 Kudos
1 Solution

Accepted Solutions
njain
Expert
Expert
Jump to solution

Hi Kaartik,

If you refer the "ClusterComputeResource" managed object in VI API Reference Guide (), the configuration.rule property is of type "ClusterRuleInfo" which is a data object. This data object is the base type for all the rules and contains the common information for all rule types. Also, as understood by you, this data object has the following properties, that gives the common information for all the rules, like "enabled", "key", "name" and "status".

Now, to differentiate among the different rules and mainly identifying them as either "affinity" or "anti-affinity" rules, you need to determine that which data object further extends "ClusterRuleInfo" (as "ClusterRuleInfo" only gives the common details for rules). As you would see in the VI API Reference Guide, this data object is further extended by "ClusterAffinityRuleSpec" and "ClusterAntiAffinityRuleSpec". These data objects help in identifying whether the rule is "affinity" or "anti-affinity".

As you would notice in the dump, the rule is of type 'ClusterAntiAffinityRuleSpec'. In order to extract this information, I used ref($rule) which actually gives the data object for the actual rule. Hope this would clarify your doubts on how to identify "affinity" or "anti-affinity" rule.

Now, if the following code snippet:

my $visrvs = $rule->vm;

foreach (@$visrvs) {

my $visrv = $_;

print "Virtual Server " . $visrv->value . "\n";

}

variable "$visrv" is actually a MOR to the Virtual Machine object. You need to get the view first and you can then access the properties of the VM. Please see below for the updated code snippet:

my $visrvs = $rule->vm;

foreach (@$visrvs) {

my $visrv = $_;

my $vm_view = Vim::get_view(mo_ref => $visrv);

print "Virtual Machine Name: " . $vm_view->config->name . "\n";

}

Hope the above explanation will give you a better clarity.

Regards,

Neha

View solution in original post

Reply
0 Kudos
6 Replies
njain
Expert
Expert
Jump to solution

Hi Kaartik,

In order to access the object, you can use the Perl libraries and simply get the view of the desired ClusterComputeResource . You can then easily access the configuration.rule property to get the list of the rules. Please refer to the attached script that lists the rules for the cluster and modify it as per your needs, if required. You can place this script in the perl/apps/general folder and execute it using the following command:

perl ListAffinityRules.pl --url Server IP>/sdk/vimService --username <username> --password <password> --clusterName <cluster name>

Hope the above information resolves your query.

Regards,

Neha

Reply
0 Kudos
kaartik
Contributor
Contributor
Jump to solution

Thanks for the script, this is very helpful. I was able to elaborate the rule names that were configured. How can I tell if this is a rule to seperate the virtual machines or to keep the virtual machines together. Would it be possible to list the virtual servers that have been included in this rule ?

Reply
0 Kudos
njain
Expert
Expert
Jump to solution

Hi Kaartik,

The "ClusterRuleInfo" class is further extended by "ClusterAffinityRuleSpec" and "ClusterAntiAffinityRuleSpec". You can find which object extends the defined rule and determine whether it is an affinity rule or an anti-affinity rule. For example, suppose you have already extracted all the rules using the configuration.rule property, then you can determine the rule to affinity or anti-affinity as follows:

if (ref($rule) eq 'ClusterAffinityRuleSpec') {

print "This is an affinity rule\n";

} elsif (ref($rule) eq 'ClusterAntiAffinityRuleSpec') {

print "This is an anti-affinity rule\n";

}

Further, if you want to list all the virtual machines that are part of the specific rule, then you can use "vm" property of "ClusterAffinityRuleSpec" or "ClusterAntiAffinityRuleSpec" data objects, whichever is applicable, to get the list.

Hope the above information is helpful.

Regards,

Neha

Reply
0 Kudos
kaartik
Contributor
Contributor
Jump to solution

If I look at the programming guide under the ClusterComputeResource Managed Object Model, it only shows Clusterruleinfo

as the last level

ClusterRuleInfo

enabled

key

name

status

When you specified ref($rule), what did you exactly do, how did you know that would give you a value of either ClusterAntiAffinityRuleSpec

or ClusterAffinityRuleSpec ? I did a dump and obtained the structure, but I am not sure how I should reference the virtual machine

names.

$VAR1 = [

bless( {

'enabled' => '1',

'key' => '26',

'name' => 'rule for x1',

'vm' => [

bless( {

'type' => 'VirtualMachine',

'value' => 'vm-1458'

}, 'ManagedObjectReference' ),

bless( {

'type' => 'VirtualMachine',

'value' => 'vm-1460'

}, 'ManagedObjectReference' )

]

}, 'ClusterAntiAffinityRuleSpec' )

];

If I run the foll, I get

my $visrvs = $rule->vm;

foreach (@$visrvs) {

my $visrv = $_;

print "Virtual Server " . $visrv->value . "\n";

Output

-


Virtual Server vm-1458

Virtual Server vm-1460

Reply
0 Kudos
njain
Expert
Expert
Jump to solution

Hi Kaartik,

If you refer the "ClusterComputeResource" managed object in VI API Reference Guide (), the configuration.rule property is of type "ClusterRuleInfo" which is a data object. This data object is the base type for all the rules and contains the common information for all rule types. Also, as understood by you, this data object has the following properties, that gives the common information for all the rules, like "enabled", "key", "name" and "status".

Now, to differentiate among the different rules and mainly identifying them as either "affinity" or "anti-affinity" rules, you need to determine that which data object further extends "ClusterRuleInfo" (as "ClusterRuleInfo" only gives the common details for rules). As you would see in the VI API Reference Guide, this data object is further extended by "ClusterAffinityRuleSpec" and "ClusterAntiAffinityRuleSpec". These data objects help in identifying whether the rule is "affinity" or "anti-affinity".

As you would notice in the dump, the rule is of type 'ClusterAntiAffinityRuleSpec'. In order to extract this information, I used ref($rule) which actually gives the data object for the actual rule. Hope this would clarify your doubts on how to identify "affinity" or "anti-affinity" rule.

Now, if the following code snippet:

my $visrvs = $rule->vm;

foreach (@$visrvs) {

my $visrv = $_;

print "Virtual Server " . $visrv->value . "\n";

}

variable "$visrv" is actually a MOR to the Virtual Machine object. You need to get the view first and you can then access the properties of the VM. Please see below for the updated code snippet:

my $visrvs = $rule->vm;

foreach (@$visrvs) {

my $visrv = $_;

my $vm_view = Vim::get_view(mo_ref => $visrv);

print "Virtual Machine Name: " . $vm_view->config->name . "\n";

}

Hope the above explanation will give you a better clarity.

Regards,

Neha

Reply
0 Kudos
kaartik
Contributor
Contributor
Jump to solution

Got it thanks, appreciate your help. I was able to list all the details with respect for the affinity rules. There seems to be little documentation when it comes to referencing managed objects, the ref function. Are there documents (besides the programming guide / getting started gude / porting guide) on the VMWare website or are there good books that I could refer to which will talk about nuances such as these ?

Reply
0 Kudos