VMware Cloud Community
kaustubh_aphale
Contributor
Contributor

Is it possible to extract data for a given timeframe using readnextevents method.

@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 

0 Kudos
4 Replies
LucD
Leadership
Leadership

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

kaustubh_aphale
Contributor
Contributor

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?


  1. Set end date to 24-October-2021
    1. Date endDate = “24-Oct-2021”;
  2. The start date is set to (End Date – 7).  So start date will be 17-Oct-2021
    1. StartDate = endDate -7
  3. Set the dates to GregorianCalendar (both for start date and end date).
    1. GregorianCalendar startG = new GregorianCalendar(StartDate);
    2. GregorianCalendar endG = new GregorianCalendar(endDate);
  4. Convert the GregorianCalendar to XMLGregorianCalendar  like

XMLGregorianCalendar xmlStartTime = DatatypeFactory.newInstance()

.newXMLGregorianCalendar(startG);

 

  1. Created new instance of EventFilterSpecByTime
  2. Set the start and end time to EventFilterSpecByTime  instance
    1. Time = new EventFilterSpecByTime
    2. Time.setBeginTime(xmlStartTime)
    3. Time.setEndTime(xmlEndTime)
  3. Set the EventFilterSpecByTime  to EventFilterSpec
    1. Filter = EventFilterSpec
    2. Filter.setTime(time);
  4. The max count (1000) is set to EventFilterSpec.
    1. Filter.setMaxCount(1000)
  5. Execute the RewindCollector at VimPortType
    1. vimPortType. RewindCollector
  6. ManagedObjectReference eventCollector = vimPortType.CreateCollectorForEvents(eventManager, Filter);
  7. List<Events> = vimPortType.readNextEvents(eventCollector, MaxEventsCount)

    Thanks
    Kaustubh
0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
_vladi_
VMware Employee
VMware Employee

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

0 Kudos