3 Replies Latest reply on Jul 22, 2012 9:50 AM by mcescalante

    Perl Metric Scripting Question

    mcescalante Novice

      To start, ultimately, I want these (among other metrics which I have already written the script to collect) to go into a CSV file.


      I would like to grab the names of the datacenters, and then the number of hosts and VMs in each. There isn't any built in metric for any of those... spitting out the datacenter names is a matter of grabbing them, but how can I get the number of hosts and VMs in each datacenter? Is the best way with some sort of funky (I'd guess for) loop?


      Also, is there an easy way to only grab certain metrics (performance counters)? Like if I wanted to ONLY grab the total CPU usage what is the best way to approach that without letting the user specify in the opts?


      Sorry if some of these questions are horrible, I just started working with vMA and I may not completely understand the API yet... haven't finished all the documentation (there seems to be quite a lot of it!).

        • 1. Re: Perl Metric Scripting Question
          lamw Guru
          VMware EmployeesCommunity Warriors



          It looks like you're asking two different type of questions, let me see if I can help answer them.


          1) For inventory information, yes you can extract the name of the Datacenter and their respective Hosts and VMs by getting a hold of the Datacenter object as shown in the vSphere API Reference guide - http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc_50%2Fvim.Datacenter.html


          If you just want to get the number of hosts/VMs for a given Datacenter, here is a quick snippet:


          #!/usr/bin/perl -w
          # William Lam


          use strict;
          use warnings;
          use VMware::VIRuntime;
          use VMware::VILib;


          my %opts = (
             datacenter => {
                type => "=s",
                help => "Name of vCenter Datacenter",
                required => 1,




          my $datacenter = Opts::get_option('datacenter');


          my $datacenter_view = Vim::find_entity_view(view_type => 'Datacenter', filter => {'name' => $datacenter});


          unless($datacenter_view) {
                  print "Unable to find " . $datacenter . "\n";
                  exit 1;


          my $host_views = Vim::find_entity_views(view_type => 'HostSystem',begin_entity => $datacenter_view);
          my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine',begin_entity => $datacenter_view);


          print "Datacenter: " . $datacenter_view->name. "\n";
          print "Number of Hosts: " . @$host_views . "\n";
          print "Number of VMs: " . @$vm_views . "\n";





          2) For performance metrics, you can extract specific metrics using the perfManager as decribed in the vSphere API here - http://pubs.vmware.com/vsphere-50/topic/com.vmware.wssdk.apiref.doc_50/vim.PerformanceManager.html You can also take a look at sample code from vSphere SDK for Perl Utities viperformance.pl which is include as part of installing vCLI - http://www.vmware.com/support/developer/viperltoolkit/viperl40/doc/vsperl_util_index.html


          I would also recommend you take a look at the programming guide here for more details on how to use the vSphere SDK for Perl - http://www.vmware.com/support/developer/viperltoolkit/ and the following articles here about the vSphere API and how to find or perform certain operations http://blogs.vmware.com/vsphere/2012/05/introduction-to-the-vsphere-api-part-3-inventory-hierarchy.html


          There are also a lot of sample code in my code repository that you can use as references to help get you started - http://communities.vmware.com/docs/DOC-9852

          1 person found this helpful
          • 2. Re: Perl Metric Scripting Question
            mcescalante Novice

            This is great. I've already got a lot of the documentation and I think that I just haven't read it enough.


            Looking back at what I'm working on, I'm also going to need a bunch of "summary" statistics that don't seem available via the performance counters (yes, I read the PDF with every counter listed). I want the following:


            • Physical Mem
            • VM Mem
            • Virtual CPUs
            • Total Disk Storage
            • Provisioned Disk Storage


            Are these available through the SDK and vMA? What's the best approach to grab these "summaries" since the performance counters seem to be more closely related to live monitoring or tracking?


            Thanks again for the post, I'm going to work through this stuff now.


            EDIT: After looking through some more documentation, I still can't find how to access the above information. Can I use a filter somehow? I couldn't find documentation that showed that PropertyFilter would let you do much beyond name and a few other things.

            • 3. Re: Perl Metric Scripting Question
              mcescalante Novice

              I did end up finishing this script and actually did some interesting things with it as well. I plan on pushing it to github or another repo soon, and I will post the link here for any curious minds who happen to stumble upon this thread!


              The key was reading their documentation and a lot of playing. Mess with code and learn how the objects interact within the VMware API.