VMware Cloud Community
jBuri
Contributor
Contributor
Jump to solution

Monitor non-blocking task with Perl VI API

I am writing a perl script that needs to ...

  1. Suspend a VM
  2. Do "some other monitoring" while the VM us in the process of suspending
  3. Verify that the suspend task completed

I've been looking at the API documentation as well as the sample code and there are plenty of examples that have helped me figure out how to accomplish #1 and to some extent #3 by calling SuspendVM() on the vm managed object (see "Option 1" in my sample code).  But this isn't perfect for #3 and doesn't allow me to do #2 at all.  The API documentation leads me to believe that I should be able to use SuspendVM_Task() on the VM object to get a monitor-able task object (see "Option 2") in my sample code.)

However, I can't figure out how to use this task object to monitor the progress.  I would think it should be something like "Monitor block" in my code, but that doesn't seem to work.  Looking at a dump of the task object the "info" property doesn't even seem to be there.  I can bless the object to its actual sub-class (instead of the generic ManagedObjectReference it starts out as), but then the info property is just empty.

I know I'm probably just missing some simple perl/perlOO/perlReference nuance, but can't pinpoint where I'm going wrong ... any help out there?

Dumper output of the task object

========================

$VAR1 = bless( {
                 'value' => 'haTask-1808-vim.VirtualMachine.suspend-1852795401',
                 'type' => 'Task'
               }, 'ManagedObjectReference' );

Sample code

==========

#!/usr/bin/perl -w

use strict;

use warnings;

use Data::Dumper;

use VMware::VIRuntime;

# ./vmSuspend.pl --password XXXX --username XXXX --server XXX.XXX.XXX.XXX --vmname XXXX

my %opts = (

   'vmname' => {

      type => "=s",

      help => "The name of the virtual machine",

      required => 1,

   },

   'filter' => {

      type => "=s",

      help => "The filter used to select matching virtual machines",

      required => 0,

   },

);

Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Util::connect();

my $retval = 0;

my $name = Opts::get_option('vmname');

my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine',

                                        filter => {'name' => $name});

my $state = $vm_view->runtime->powerState->val;

print "$name is $state\n";

# Option 1 - blocking

# $vm_view->SuspendVM();

# Option 2 - non-blocking

my $suspendTask = $vm_view->SuspendVM_Task();

print Dumper($suspendTask);

# Monitor block

# while ($suspendTask->info->state->val eq 'running' || $suspendTask->info->state->val eq 'queued') {

#     <<Do some other monitoring>>

#}

$vm_view->update_view_data();

$state = $vm_view->runtime->powerState->val;

print "$name is $state\n";

0 Kudos
1 Solution

Accepted Solutions
lamw
Community Manager
Community Manager
Jump to solution

Take a look at this script as an example: http://communities.vmware.com/docs/DOC-10269 There is a method in there called getStatus() which shows you how to wait for a task to be completed before continuing on. You're on the right track, but you need to do a few more things to get the view of the task and make sure you update the view after you sleep/wait and then check again.

View solution in original post

0 Kudos
2 Replies
lamw
Community Manager
Community Manager
Jump to solution

Take a look at this script as an example: http://communities.vmware.com/docs/DOC-10269 There is a method in there called getStatus() which shows you how to wait for a task to be completed before continuing on. You're on the right track, but you need to do a few more things to get the view of the task and make sure you update the view after you sleep/wait and then check again.

0 Kudos
jBuri
Contributor
Contributor
Jump to solution

Thanks, lamw.  That was exactly the kind of example I needed.

0 Kudos