VMware vSphere/IOPS-Limit per virtueller Disk

From wiki.krogloth.de
< VMware vSphere
Revision as of 17:03, 11 June 2013 by Alex (Talk | contribs) (Created page with " <code> #!/usr/bin/perl -w use strict; use warnings; use 5.010; use FindBin; use lib "/usr/lib/vmware-vcli/apps"; use VMware::VIRuntime; use AppUtil::HostUtil; use AppUtil...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

  1. !/usr/bin/perl -w

use strict; use warnings; use 5.010;

use FindBin; use lib "/usr/lib/vmware-vcli/apps";

use VMware::VIRuntime; use AppUtil::HostUtil; use AppUtil::VMUtil;

use Data::Dump qw(pp);

my %opts = (

  name => {
     type => "=s",
     help => "Name of the Datastore",
     required => 0,
  },
  todo => {
     type => "=s",
     help => "What to do? [list|set]",
     required => 1,
  },

);

my @ausnahmen = ("Win7-Bench","veeam-proxy-nbg6a-1.vmware.noris.de");

Opts::add_options(%opts); Opts::parse(); Opts::validate();

my @valid_properties;

Util::connect();

my $datastores = find_datastores();

foreach my $datastore (@$datastores) {

   my $vm_views = Vim::get_views( mo_ref_array => $datastore->vm );
   foreach my $vm_view (@$vm_views) {
       if ( !( $vm_view->config->template ) ) {
           my $vm_entity_view = Vim::find_entity_views( view_type => 'VirtualMachine', filter => { 'name' => $vm_view->name } );
           foreach my $vm ( sort { $a->summary->config->name cmp $b->summary->config->name } @$vm_entity_view ) {
               if ( $vm->summary->runtime->connectionState->val eq 'connected' ) {
                   if ( !$vm->config->template ) {
                       my $vmname = $vm->summary->config->name;
                       my $devices = $vm->config->hardware->device;
                       my $disk_count = 0;
                       foreach my $device (@$devices) {
                          if ( $device->isa('VirtualDisk') ) {
                             my $datastore;
                             my $diskName = $device->backing->fileName;
                            if ( $diskName =~ m/\[([^\]]*)\]/ ) {
                                $datastore = $1;
                             }
                             my $datastore_attribute = Opts::get_option('name');
                             if ( defined $datastore && $datastore eq $datastore_attribute ) {
                                $disk_count++;
                             }
                          }
                       }
                       foreach my $device (@$devices) {
                           if ( $device->isa('VirtualDisk') ) {


                               my $datastore;
                               my $diskName = $device->backing->fileName;
                               if ( $diskName =~ m/\[([^\]]*)\]/ ) {
                                   $datastore = $1;
                               }
                               my $datastore_attribute = Opts::get_option('name');
                               if ( defined $datastore && $datastore eq $datastore_attribute ) {
                                   my $limit;
                                   my $label       = $device->deviceInfo->label;
                                   my $scsiadapter = $device->controllerKey;
                                   $scsiadapter =~ s/100//;
                                   my $scsitarget = $device->unitNumber;
                                   my $advOptions = $vm->config->extraConfig;
                                 ADVOPTIONS:
                                   foreach my $option ( sort { $a->key cmp $b->key } @$advOptions ) {
                                       if ( $option->key =~ /^sched\.scsi$scsiadapter:$scsitarget\.throughputCap/ ) {
                                           $limit = $option->value;
                                           last ADVOPTIONS;
                                       }
                                       else {
                                           $limit = "off";
                                       }
                                   }
                                   if(Opts::get_option('todo') eq "set"){
                                      my $iops_limit;
                                      given ($datastore) {
                                         when(/^stor1-900-10k-\d+$/) { $iops_limit = 2000/$disk_count; }
                                         when(/^stor1-1000-7.2k-\d+$/) { $iops_limit = 1200/$disk_count; }
                                         when(/^stor1-3000-7.2k-\d+$/) { $iops_limit = 500/$disk_count; }
                                         default {
                                            print "Unbekannter Datastore";
                                            die;
                                         }
                                    }
                                      unless ( grep /^$vmname$/, @ausnahmen ) {
                                          if ( $vm && $limit ne $iops_limit ) {
                                              my $vm_config_spec = VirtualMachineConfigSpec->new(
                                                  name        => $vmname,
                                                  extraConfig => [
                                                      OptionValue->new(
                                                          key   => "sched.scsi$scsiadapter:$scsitarget.throughputCap",
                                                          value => $iops_limit
                                                      ),
                                                  ]
                                              );
                                              $vm->ReconfigVM_Task( spec => $vm_config_spec );
                                               print "IOPS-Limit gesetzt fuer: " . $vmname . "\n";
                                          }
                                      }
                                   }
                                   if(Opts::get_option('todo') eq "list"){
                                      print "VM: \t" . $vm->name . "\n";
                                      print "Disk: \t" . $diskName . "\n";
                                      print "IOPS-Limit: \t" . $limit . "\n\n";
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
   }

}

Util::disconnect();

sub find_datastores {

   my $datastore_name = Opts::get_option('name');
   my $dc             = Vim::find_entity_views( view_type => 'Datacenter' );
   my @ds_array       = ();
   foreach (@$dc) {
       if ( defined $_->datastore ) {
           @ds_array = ( @ds_array, @{ $_->datastore } );
       }
   }
   my $datastores = Vim::get_views( mo_ref_array => \@ds_array );
   @ds_array = ();
   foreach my $ds (@$datastores) {
       my $match_flag = 1;
       if ( $ds->summary->accessible ) {
           if ( $match_flag && $datastore_name ) {
               if ( $ds->summary->name eq $datastore_name ) {
                   # Do Nothing
               }
               else {
                   $match_flag = 0;
               }
           }
           if ($match_flag) {
               @ds_array = ( @ds_array, $ds );
           }
       }
   }
   return \@ds_array;