6 Replies Latest reply on Jul 10, 2008 10:36 AM by kaartik

    Perl script to list DRS affinity rules

    kaartik Novice

      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,

        • 1. Re: Perl script to list DRS affinity rules
          njain Expert
          VMware Employees

           

          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 https://<vc/ Server IP&gt;/sdk/vimService --username &lt;username&gt; --password &lt;password&gt; --clusterName &lt;cluster name&gt;

           

           

          Hope the above information resolves your query.

           

           

          Regards,

          Neha

          • 2. Re: Perl script to list DRS affinity rules
            kaartik Novice

             

            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 ?

             

             

            • 3. Re: Perl script to list DRS affinity rules
              njain Expert
              VMware Employees

               

              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

              • 4. Re: Perl script to list DRS affinity rules
                kaartik Novice

                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' =&gt; '1',

                'key' =&gt; '26',

                'name' =&gt; 'rule for x1',

                'vm' =&gt; [

                bless( {

                'type' =&gt; 'VirtualMachine',

                'value' =&gt; 'vm-1458'

                }, 'ManagedObjectReference' ),

                bless( {

                'type' =&gt; 'VirtualMachine',

                'value' =&gt; 'vm-1460'

                }, 'ManagedObjectReference' )

                ]

                }, 'ClusterAntiAffinityRuleSpec' )

                ];

                 

                 

                If I run the foll, I get

                my $visrvs = $rule-&gt;vm;

                foreach (@$visrvs) {

                my $visrv = $_;

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

                Output

                -


                Virtual Server vm-1458

                Virtual Server vm-1460

                • 5. Re: Perl script to list DRS affinity rules
                  njain Expert
                  VMware Employees

                   

                  Hi Kaartik,

                   

                   

                  If you refer the "ClusterComputeResource" managed object in VI API Reference Guide (http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.ClusterComputeResource.html), 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-&gt;vm;

                  foreach (@$visrvs) {

                      my $visrv = $_;

                      print "Virtual Server " . $visrv-&gt;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-&gt;vm;

                  foreach (@$visrvs) {

                      my $visrv = $_;

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

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

                  }

                   

                   

                  Hope the above explanation will give you a better clarity.

                   

                   

                  Regards,

                  Neha

                   

                   

                  • 6. Re: Perl script to list DRS affinity rules
                    kaartik Novice

                    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 ?