Tuesday, March 3, 2009

VMWare VIMA and Snapshots ...

VMWare is something I have been working with for a bit, but I run into the same issue over and over, the limitation of the VCenter scheduler. While it is a great tool, we must revert to scripting more times than not to get the results we desire.
My specific issue was to use VIMA to find all snapshots for specific hosts whithin a VI3 HA cluster, which we all know, if DRS is set to Fully Automated, we will never know which host a particular VM is running on, whithout looking of course. Well, I am no expert programmer, but I have pieced together a perl script from the examples and internet sources that will loop through all hosts managed by VIMA and search for specific VMs and once it locates a VM, it will list what host it is located on and all snapshots for that VM.
For use in your specific environment, just change the array @SArray (it's commented, so it is easy to find).
This script was only tested on a small six host ESX HA configuration, so any more than that, I cannot say what the performance will be like, and like any home brewed scripts, use at your own risk, I cannot be held liable for damage that this script may create by testing in a production environment. Remember, this script was frankensteined for VIMA, I'm sure it can be modded for running on other platforms.

Script below:


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

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../";

use VMware::VIRuntime;
# import the fastpass lib
use VMware::VIFPLib;

$SIG{__DIE__} = sub{Util::disconnect()};
$Util::script_version = "1.0";

my %opts = (
aboutservice => {
type => "=s",
help => "Information about the service",
required => 0,
default => 'all',
},

aboutviperl => {
type => "=s",
help => "Information about the viPerl toolkit",
required => 0,
default =>'version',
},

'expand' => {
type => ":s",
help => "If provided, will list additional information about the snapshots.",
required => 0,
default => "false"
}

);

#Change the array below to your list of servers
my @SArray = qw(server1 server2 server3);

Opts::add_options(%opts);
Opts::parse();
if (Opts::option_is_set('aboutviperl')) {
get_toolkitversion();
} else {
Opts::set_option("passthroughauth", 1);
Opts::validate(\&validate);
#Instead of util::connect(), use login_by_fastpass()
#Util::connect();
# Get list of fastpass targets using enumerate_targets()
my @targets = VIFPLib::enumerate_targets();
foreach my $target(@targets) {
#print "Version on $target: \n";
#print "--------------------------------------------------------------------------------\n";
#connect to the lib using fastpass
VIFPLib::login_by_fastpass($target);

foreach my $SArray(@SArray) {

my $VMs = Vim::find_entity_views( view_type => 'VirtualMachine' );
foreach my $vm (@$VMs) {
if ($SArray eq $vm->name) {
print "$SArray located on $target \n";
if ($vm->snapshot) {
foreach (@{$vm->snapshot->rootSnapshotList}) {
printSnaps ($_, 2);
}
}
print "--------------------------------------------------------------------------------\n";
}
}
}
Util::disconnect();
}
}

sub printSnaps {
my ($snapshotTree, $indent) = @_;
if ($indent < indent =" 2;">name
. " " x ($indent + 3) . "Created: " . $snapshotTree->createTime . "\n";
# if requested, display some additional information
if (Opts::get_option('expand') ne "false") {
print " " x ($indent + 3) . "Quiesced: " . $snapshotTree->quiesced
. " " x ($indent + 3) . "State: " . $snapshotTree->state->val
. " " x ($indent + 3) . "Description: " . $snapshotTree->description;
}
# recurse through the tree of snaps
if ($snapshotTree->childSnapshotList) {
# loop through any children that may exist
foreach (@{$snapshotTree->childSnapshotList}) {
printSnaps($_, $indent + 2);
}
}
}

sub validate {
my $valid = 1;
if (Opts::option_is_set('aboutservice')) {
my $service_option = Opts::get_option('aboutservice');
if(!(($service_option eq 'apiinfo')($service_option eq 'product')
($service_option eq 'version')($service_option eq 'ostype')
($service_option eq 'all'))) {
Util::trace(0, "Invalid aboutservice option "
. "supported options are apiinfo,product,version,ostype,all \n");
$valid = 0;
}
}
if (Opts::option_is_set('aboutviperl')) {
my $viperl_option = Opts::get_option('aboutviperl');
if(!($viperl_option eq 'version')) {
Util::trace(0, "Invalid aboutviperl option "
. "supported options is version\n");
$valid = 0;
}
}
return $valid;
}

Sorry the script is so flat, I had some issues with spacing and tabbing in the blog editor. I will work the kinks out for next time.

My next step in this process is to add in functionality to remove old snapshots automatically so I do not have to manually prune old snaps out.

Till next time...

No comments:

Post a Comment