[vRO] Retrieve the last 24h events from vCenter

Ce message est également disponible en : French

vra-product-icon-mac_0

 

 

 

 

How to retrieve the last 24h events from your vCenter with more than 999 events.

As you know, by default the maximum number of events that you can retrieve from the vCenter is 999. I will show you how to display the last 24h of events in your vRealize Orchestrator Workflow.

In this exemple i want to retrieve all the events in my vCenter since the last 24h.

First, you need to add as an attribute the vCenter (Type :sdkconnection).

sdk connection

Then you will initiliaze the events from the last 24h and create a collector. By default your collector will keep only 999 events.

System.debug("Initialize all the Events of the infrastructure minus 24 hours");
//Initialize all the Events of the infrastructure minus 24 hours
var vcEventFilterSpec = new VcEventFilterSpec() ;
var vcEventFilterSpecByTime = new VcEventFilterSpecByTime() ;
var vcEventManager = sdk.eventManager;
var HostEvents = [];

var beginTime = new Date();

beginTime.setHours(beginTime.getHours()-24);

vcEventFilterSpecByTime.beginTime = beginTime;

System.debug("BeginTime : "+beginTime);

System.debug("Define Filter by time");
vcEventFilterSpec.time = vcEventFilterSpecByTime;


var collector = vcEventManager.createCollectorForEvents(vcEventFilterSpec);

Then what i want to do is to create a loop , and create a new collector while i have events to display.

//Define Max task number per Collector (999 maximum)
var tasknumber = 999;

var tasks =[];

var collector = vcEventManager.createCollectorForEvents(vcEventFilterSpec);

var dummy = collector.rewindCollector();

var tasks = collector.readNextEvents(tasknumber);

while (tasks.length == tasknumber) {
    var last = tasks.length-1;
System.debug("Number of event to analyze: "+tasks.length);
System.debug("First Event :"+tasks[0].createdTime);
System.debug("Last event :"+tasks[last].createdTime);

for each (var task in tasks){
    if(task.fullFormattedMessage != undefined ) {
    System.debug(task.createdTime+" "+task.fullFormattedMessage);
    }
}
var tasks = collector.readNextEvents(tasknumber);
}

Once the maximum number of events is lower than 999, i will create a simple loop and display the last events


var last = tasks.length-1;

System.debug("Number of event to analyze: "+tasks.length);
System.debug("First Event :"+tasks[0].createdTime);
System.debug("Last event :"+tasks[last].createdTime);

for each (var task in tasks){
if(task.fullFormattedMessage != undefined ) {
System.debug(task.createdTime+" "+task.fullFormattedMessage);
}
}

And at the end, don’t forget to destroy your collectors.

collector.destroyCollector();

And now you will display all the event , line by line for the last 24h.

screen-shot-2016-10-09-at-20-00-24

Julien Varela
Follow me

Julien Varela

Virtualisation & Cloud manager at Excellium Services
Consultant Virtualisation/Cloud au sein de Excellium Services et blogger @vThink. Je suis passionné par les nouvelles technologies. Et tout particulièrement, celles concernant la virtualisation. j'ai su apporter par mon autonomie, ma curiosité, ma pugnacité et ma technicité mon expertise dans ce domaine.
Julien Varela
Follow me

Leave a Reply