VMware {code} Community
ashvitech
Contributor
Contributor

How to use WaitForUpdates in perl?

I want to get notifications from VC for specifc events. My design does not allowed to use polling mechnanism. I read WaitForUpdates used to subscrbe the events. I have some questions regarding this. I know how to use it in Java and C#.

To call WaitForUpdates should I use seprate thread? Is perl supports threads? Is there any other way to subscribe the events to VC and get notification?

Thanks,

AshVI

0 Kudos
6 Replies
ashvitech
Contributor
Contributor

I am using waitForUpdate as below, but my thread is not returning if I do operations on specified VM. (Yet I have not used threading)

I have tried for rootfolder (wait on object) by using history collector, not succeed. I commented the code which uses historycollector. In second case I tried Wait on virtualMachine's events so used view_type => 'VirtualMachine' and MS-DIOS is vm name. I am trying to implement it, somewhat as VmPowerOps.cs sample is C#.

Please correct me and let me know whether I am on right track.

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

use strict;

use warnings;

use FindBin;

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

use VMware::VIRuntime;

use VMware::VILib;

sub usage;

sub validate;

#Opts::add_options(%opts);

Opts::parse();

Opts::validate();

Util::connect();

latestEvent();

Util::disconnect();

sub latestEvent {

my $begin;

my $mor = Vim::get_service_content()->eventManager;

my $mor_rootFolder = Vim::get_service_content()->rootFolder;

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

filter => { name => 'MS-DOS' });

my $eventmanager_view = Vim::get_view(mo_ref => $mor);

my $latestEvent = $eventmanager_view->latestEvent;

my $my_filterSpec = EventFilterSpec->new();

my $EventFilterSpecByEntity_spec ;

$EventFilterSpecByEntity_spec = EventFilterSpecByEntity->new(

recursion => "EventFilterSpecRecursionOption.children",

entity => $virtualmachine_view, );

  1. $my_filterSpec = EventFilterSpec->new(EventFilterSpecByEntity => $EventFilterSpecByEntity_spec, type =>

  1. );

#my $eventHistoryCollector = $eventmanager_view->CreateCollectorForEvents(filter => $my_filterSpec);

my $propertyspec = PropertySpec->new (all => 1, pathSet => "", type => "VirtualMachine") ;

my @propspecarray = new PropertySpec;

my @selectionspecarray = new SelectionSpec;

my $rarray2 = \@propspecarray;

my $objspec = ObjectSpec-> new( obj => $virtualmachine_view, skip => 0, selectSet =>$rarray2 );

my @objspecarray = new ObjectSpec;

my $rarray1 = \@objspecarray;

my $propertyfilterspec = PropertyFilterSpec->new(objectSet => $rarray1, propSet => $rarray2);

my $propcoll = Vim::get_service_content()->propertyCollector;

print "111111:\n";

Vim::get_vim_service()->CreateFilter( _this => $propcoll, spec => $propertyfilterspec, partialUpdates => 0 );

print "2222:\n";

my $update = Vim::get_vim_service()->WaitForUpdates(_this => $propcoll, version => "");

if (!$update) {

print "Update Received:\n";

} else {

print "Update Not Received:\n";

}

}

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

Output:

1111:

2222:

....

0 Kudos
PhilPollard
Enthusiast
Enthusiast

Did you ever get this working? I am trying to get a real-time capture of events working in perl and am having similar issues.

I am available for consulting: http://www.tsslink.com/ My focus is automation and programming with VMware.
0 Kudos
stumpr
Virtuoso
Virtuoso

It isn't pretty, but might get you started. (See attached).

Sample output as I changed the name of a Datacenter object in my vCenter instance:

perl WaitForUpdate.pl --server=172.16.50.25 --username=administrator --password=VMware1 --datacenter=Hine2
Got an update!
Version: 1
   Changed: name = Hine2 (op=assign)
Got an update!
Version: 2
   Changed: name = Hine3 (op=assign)
Got an update!
Version: 3
   Changed: name = Hine4 (op=assign)
Got an update!
Version: 4
   Changed: name = Hine5 (op=assign)
Got an update!
Version: 5
   Changed: name = Hine (op=assign)
^C

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
PhilPollard
Enthusiast
Enthusiast

That is exactly what I was looking for. It really helped me figure out the PropertyCollector side of things. I've not got an EventHistoryCollector working as well. Thank you!

I am available for consulting: http://www.tsslink.com/ My focus is automation and programming with VMware.
0 Kudos
stumpr
Virtuoso
Virtuoso

I may have a snippet on using the eventcollector as well. I will post it when I have some downtime.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
lamw
Community Manager
Community Manager

Here is a vSphere SDK for Perl script that shows you how to use CreateCollectorForEvents

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

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos