@LucD Thank you for your valuable guidance. I was wondering if you could help me with this requirement.
Can we make an arrangement in the eventFilter or eventHistoryCollector to get records falling in the specified startTime and endTime, provided in the filter. Or is there any other way by which we can expedite the required records in the specified time frame.
Also can you please take a look at my comment in -
https://communities.vmware.com/t5/VMware-vSphere-Discussions/Total-count-of-Vsphere-events-using-eve...
Thanks
Kaustubh
The EventFilterSpec that you use when calling CreateCollectorForEvents has a Time property, behind which is an EventFilterSpecByTime object, that allows specifying a Begin- and End-Time.
For example, to retrieve all events from the past day, you could do the following.
$now = Get-Date
$maxCount = 1000
$eventMgr = Get-View EventManager
$filter = New-Object -TypeName VMware.Vim.EventFilterSpec
$time = New-Object -TypeName VMware.Vim.EventFilterSpecByTime
$time.BeginTime = $now.AddDays(-1)
$time.EndTime = $now
$filter.Time = $time
$filter.MaxCount = $maxCount
$eCollectorMoRef = $eventMgr.CreateCollectorForEvents($filter)
$eCollector = Get-View -Id $eCollectorMoRef
$eCollector.RewindCollector()
$result = $eCollector.ReadNextEvents($maxCount)
while($result){
$result[0].CreatedTime
$result[-1].CreatedTime
$result = $eCollector.ReadNextEvents($maxCount)
}
Note that this snippet shows the creation timestamp for the 1st and the last event on the retrieved page.
Since it uses the RewindCollector at the beginning, the pages will return all events from oldest to newest.
And yes, 1000 is the hard-coded maximum events on a page.
You can go lower, but not higher.
The current default page size seems to be 10 (vSphere 7).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi @LucD, I have implemented the following logic but not getting the expected result in the defined timeframe. Can you please help me spot what am I doing wrong here?
XMLGregorianCalendar xmlStartTime = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(startG);
Sorry, but my Java knowledge is zero (and I do my utmost to keep it like that).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Kaustubh,
Something like this should work:
final DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
final Date endDate = formatter.parse("24-Oct-2021");
final Calendar endG = new GregorianCalendar(endDate);
final Calendar startG = new GregorianCalendar(endDate);
startG.add(Calendar.DATE, -7);
Also I don't think you need to use XMLGregorianCalendar - the EventFilterSpec should support startG and endG directly. The rest seems fine.
Please try it out and let's see. If there are still issues providing the actual output would help with troubleshooting.
Cheers,
Vladi