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
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, );
$my_filterSpec = EventFilterSpec->new(EventFilterSpecByEntity => $EventFilterSpecByEntity_spec, type =>
#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:
....
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.
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
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 may have a snippet on using the eventcollector as well. I will post it when I have some downtime.
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:
VMware Code Central - Scripts/Sample code for Developers and Administrators
If you find this information useful, please award points for "correct" or "helpful".