Nearly real time monitoring

The VMTN Communities have always been a useful source of inspiration for writing scripts. This week, for example, there was an intriguing question raised in the Onyx Community. The user wanted to know if it was possible to monitor changes in the vSphere environment in real time.

As far as I know there is no API in the vSphere SDK to do real time monitoring. But the EventHistoryCollector can deliver something that I prefer to call nearly real time monitoring. The following script is a very basic function, written primarily to show that this principle of nearly real time monitoring works. The function displays some selected properties of each event it sees.

The script

Note: this script requires PowerCLI 4.1 !

Annotations

Line 30: The Entity variable is defined with the basic type InventoryItemImpl. That way different types of vSphere entities can be passed.

Line 36: The ReadNextEvents method uses a display window that scrolls/slides over all the events. This constant defines the size of that window. If the vSphere environment is extreme;y active and produces more than ViewSize events during the Pause interval, some events will be missed by the function.

Line 40-49: The set up of the filter for the EventHistoryCollector.

Line 53-58: The loop that fetches and displays all events till the Finish time is reached.

Line 60: Since you can only have a limited number of collectors it is good practice to destroy the collectors that are not needed anymore.

Sample runs

The most simple call of the function will display all events for all entities.

The following screenshot shows the events that were captured while an ESX server was placed in maintenance mode.

The following call to the function will show all events for the next minute with 5 second intervals between each access to the event history collector.

When the Entity parameter is used, the function will only show the events for that specific entity and all the entities children. Note that for folders and resource pools you will only see the events for the children.
If you want to see the events for a folder or for a resource pool, you will have to specify a parent entity.

The Invoke-VIEventMonitor function can come in handy when you want to capture what is actually happening while you run one of your automation scripts. It is easier to get the information this way instead of extracting the events later from the vCenter database through the Get-VIEvent cmdlet.
And for those without a vCenter, it gives you a way of keeping a log of events.

But like I said in the beginning of this post, this is just a minimal function to prove the functionality.

The script can surely be expanded to do a more profound analysis of events, and the script could even take reactive actions for some of the events that it sees. Enjoy.

4 Comments

    Aleksandar

    I would suggest using “#requires -pssnapin VMware.VimAutomation.Core -version 4.1” instead of the note “this script requires PowerCLI 4.1 !”

      LucD

      @Aleksandar. Thanks, a very good idea. I will update the script.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

1 + 4 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.