vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.02k stars 922 forks source link

Listening for vsphere events using Pyvmomi #641

Open padkrish opened 4 years ago

padkrish commented 4 years ago

Hello, I am writing some prototypes where in my code gets called when a specific event happens. I looked at some sample code in https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/relocate_events.py. This seems to be querying for some specific events that has happened in the past.

What i am looking for is for my function to be called asynchronously when certain events of interest occurs in the future. Or the events can be put in the queue that my module defines.

'govmomi' seems to have something similar as in: err = eventManager.Events(ctx, refs, 10, false, false, handleEvent)

Is there a way to achieve this using pyvmomi?

Thanks, KP

prziborowski commented 4 years ago

I'm not familiar with govmomi and how that works as a comparison. I think for pyVmomi you would probably have to write a more asynchronous framework to use callback.

From the service instance content, you would use the eventManager and call CreateCollector and give it a EventFilterSpec. That will return an EventHistoryCollector. This gives you a list of 'latestPage' of events. If you want to simulate some asynchronous behavior, you would probably have this running in a different thread that calls a different function when new events appear.

You would likely have to also leverage the PropertyCollector. An example would be https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/waitforupdates.py (That sample is pretty large and maybe confusing. I can probably provide a smaller example that is narrower to the minimum needed to listen to the 'latestPage' property of the EventHistoryCollector object).

padkrish commented 4 years ago

Thanks for the response and pointers. If you can please help me with the right object/properties ('latestPage'), it will be very useful. As you said, i can easily simulate the asynchronous behavior after that.

prziborowski commented 4 years ago

Very sorry for the delay. I added a gist: https://gist.github.com/prziborowski/65872c4c32e850495cdd16d366200830 Hopefully that is enough to get you going along.

padkrish commented 4 years ago

Thanks a lot. Let me try it out.

padkrish commented 4 years ago

This works. Just one question relating to events in general. I need some DVS events for which i am able to get the event_id like 'DvsEvent', 'DvsHostJoinedEvent' and so on.

I need events for: a) VLAN modified on a DVS port group or a regular port group b) Any CDP/LLDP neighbour change like the vnic or connected switch information c) A regular Port-Group getting deleted

Is there any top level event or specific event ID associated with the above?

Thanks, KP

prziborowski commented 4 years ago

I'm not too familiar with all the events and how they link up, especially on the network side. I think a lot of things now build off EventEx as it can allow new events and messages, but not need a new data object for it. If you have existing events for what you are interested in, you could modify the script to remove parts of the filter. I actually forgot to restrict the filtering to the datacenter. I'll try to update the gist to include that part, so it could be an example of narrowing the events.

padkrish commented 4 years ago

I am getting all the events in DVS. Do i need to do any modifications to get the 'Host' related events? For example, i get a DVS created event, but not a VS event. Before, i post, just want to make sure as this code snipped should work for events happening in the datacenter as well as the hosts in the data center, right?

Where's the 'dc' object getting used?

if args.datacenter: dc = get_dc(si, args.datacenter) else: dc = si.content.rootFolder.childEntity[0]

Thanks a lot again, this code snippet is very useful.

prziborowski commented 4 years ago

Sorry for the delay. I still haven't been able to update the snippet.

    byEntity = vim.event.EventFilterSpec.ByEntity(entity=dc, recursion='all')
    filterSpec = vim.event.EventFilterSpec(eventTypeId=ids, time=byTime, entity=byEntity)
padkrish commented 4 years ago

Thanks

padkrish commented 4 years ago

Hello, In terms of getting the events for a D-PG, it works great.

But, am not having much luck in getting the regular PG related events. I tried all the below ID's in the filterSpec that seems even remotely related.

ids = ['Event', 'GeneralEvent', 'GeneralHostInfoEvent', 'HostEvent', 'DvsHostJoinedEvent', 'DvsHostLeftEvent', 'DVPortgroupEvent', 'DVPortgroupReconfiguredEvent', 'DvsReconfiguredEvent', 'DVPortgroupCreatedEvent', 'DVPortgroupDestroyedEvent', 'VmRelocatedEvent', 'DrsVmMigratedEvent', 'VmMigratedEvent', 'DvsCreatedEvent', 'DvsPortCreatedEvent', 'DvsDestroyedEvent']

... filterSpec = vim.event.EventFilterSpec(eventTypeId=ids, time=byTime, entity=byEntity) ....

I tried creating a new issue on github, but did not get any replies. Any suggestions?