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”;
            }
        }
    }

 

0 Shares:
Leave a Reply

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

You May Also Like
Read More

Some Netapp terms..

Snapshot – is a technology that helps to create point-in-time copies of file systems, which you can use…
Read More

Few /proc files

/proc/cmdline – file has the parameters passed to the kernel while the system boots-up. /proc/kcore – This file…
Read More

Steps to create initrd

Creating initrd using mkinitrd # mkinitrd  -v -f –with=e1000 /tmp/initrd.gz 2.6.9-55 Extracting the initrd image to add contents…
Read More

Netapp Snapvault guide

Netapp SnapVault is a heterogeneous disk-to-disk backup solution for Netapp filers and heterogeneous OS systems (Windows, Linux ,…
Read More

Sticky bit

What is the “sticky bit”? (t and T) The sticky bit is primarily used on shared directories.  It…