In your day to day operations as part of managing a virtual VMware datacenter, you may need to gather or perform a particular operation on a cluster alone or a individual datastore alone .. or something like that. Here is a sample Perl script that lists down the VMware Tools status on a particular datastore in your vsphere datacenter. This script is just a sample and helps you to understand how it works. You can modify this script to suit your need.

Note :

  • Please make sure you have Perl VI SDK installed on the box from which you are running this script.
  • Run it as ./scriptname. pl -server <VcenterServerNAme> -username <VcenterUserName> -dsname <DataStoreName>
  • The script will prompt for vcenter password.

#!/usr/bin/perl

use strict;
use warnings;
use VMware::VIRuntime;    #VMware vshere API
use Data::Dumper;
my %opts = (
    dsname => {
        type     => “=s”,
        help     => “Name of the datastore”,
        required => 1,
    }
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();
my $server   = Opts::get_option(‘server’);      # vsphere name
my $username = Opts::get_option(‘username’);    # vsphere username
my $password = Opts::get_option(‘password’);    # vsphere password
my $dsname   = Opts::get_option(‘dsname’);      # datastore name

Util::connect();
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 );
foreach (@$datastores) {
    if ( $_->name eq $dsname ) {
        print “Datastore name : ” . $_->name . ” nn”;
        performVMOps($_);
    }
}

Util::disconnect();
exit;
# Completed

sub performVMOps {
    my ($datastore) = @_;
   
    my $vm_views = Vim::get_views( mo_ref_array => $datastore->vm );
    foreach (@$vm_views) {
        if ( !( $_->config->template ) ) {
            my $host = $_->name;
            if ( $_->runtime->powerState->val eq ‘poweredOn’ ) {
                if ( $_->guest->toolsRunningStatus eq ‘guestToolsRunning’ ) {
                    print “t ” . $_->name . ” – VMware tools is running n”;
                }
                else {
                    print “t ”
                      . $_->name
                      . ” – VMware tools status is not OK n”;
                }
            }
            else {
                print “t ” . $_->name . ” – is powered-off n”;
            }
        }
    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *