Hi,
I am new to VRO and I would like to create a workflow that will give me the number and details of vMotions that has taken place within a cluster in last 7 days.
Please help.
eventTypeId is a property of VcEventFilterSpec, not of VcEventFilterSpecByTime. VcEventFilterSpecByTime type contains values to specify time range used to filter the events, it doesn't make sense for it to also specify eventTypeId.
The code should be something like:
var spec = new VcEventFilterSpec();
spec.eventTypeId = ["DrsVmMigratedEvent", "VmMigratedEvent"];
spec.entity = new VcEventFilterSpecByEntity();
spec.entity.entity = ...
spec.entity.recursion = ...
spec.time = new VcEventFilterSpecByTime();
spec.time.beginTime = ...
spec.time.endTime = ...
// call eventManager.queryEvents(spec)
Note how the top-level spec of type VcEventFilterSpec contains several sub-specs to filter on some event aspect (entity, time, etc.)
Can anyone please update if it is possible in vRO?
You might be able to do this by looking at the vCenter Events or Tasks
see here for vRO examples on collecting Tasks - https://code.vmware.com/forums/3055/vrealize-orchestrator#520597
see here for Event queries (using PowerShell but the underlying APIs are the same) - http://www.lucd.info/2013/03/31/get-the-vmotionsvmotion-history/
It is possible to get this information from vCenter events.
Here is some sample code showing how to collect vMotion-related events for all VMs in a cluster. The input parameter is variable named cluster of type VC:ClusterComputeResource
var spec = new VcEventFilterSpec();
spec.eventTypeId = ["DrsVmMigratedEvent", "VmMigratedEvent"];
spec.entity = new VcEventFilterSpecByEntity();
spec.entity.entity = cluster;
spec.entity.recursion = VcEventFilterSpecRecursionOption.children;
var events = cluster.sdkConnection.eventManager.queryEvents(spec);
for each (var ev in events) {
System.log("Event: " + ev);
}
Note that this code will fetch all events, not only those from the last 7 days. It is easy to add a filter to limit the returned events by time (by initializing the time field of the spec object in the code above). I'd suggest to try to add this filter yourself as an exercise Let me know if you need help with it.
Thanks Ilian Iliev for your help. I have written the below code and getting the desired result.
However for learning purpose I would like to know how to add a filter to limit the returned events by time (by initialising the time field of the spec object in the code ab
var vMotion7days = new Array()
var past7days = new Date()
var last7days = past7days.getDate() - 7;
past7days.setDate(last7days);
var spec = new VcEventFilterSpec();
spec.eventTypeId = ["DrsVmMigratedEvent", "VmMigratedEvent"];
spec.entity = new VcEventFilterSpecByEntity();
spec.entity.entity = cluster[0];
spec.entity.recursion = VcEventFilterSpecRecursionOption.children;
var events = cluster[0].sdkConnection.eventManager.queryEvents(spec);
for each (var ev in events) {
var evdate = new Date(ev.createdTime)
if (evdate >= past7days){
var vMotion = {}
vMotion["CreatedTime"] = ev.createdTime
if(ev.userName){
vMotion["UserName"] = ev.userName
}else{
vMotion["UserName"] = "System"
}
if(ev.sourceDatastore.name == ev.ds.name){
vMotion["Type"] = "vMotion"
}else{
vMotion["Type"] = "svMotion"
}
vMotion["VM"] = ev.vm.name
vMotion["Cluster"] = cluster[0].name
vMotion["SrcVMHost"] = ev.sourceHost.name.split(".")[0]
if (ev.host.name != ev.sourceHost.name){
vMotion["TgtVMHost"] = ev.host.name.split(".")[0]
}else{
vMotion["TgtVMHost"] = ''
}
vMotion["SrcDatastore"] = ev.sourceDatastore.name
if(ev.ds.name != ev.sourceDatastore.name){
vMotion["TgtDatastore"] = ev.ds.name
}else{
vMotion["TgtDatastore"] = ''
}
vMotion7days.push(vMotion)
}
}
if(vMotion7days.length > 0){
System.log(JSON.stringify(vMotion7days))
}
I tried to use the class VcEventFilterSpecByTime(). However eventTypeId property is not there for VcEventFilterSpecByTime().
eventTypeId is a property of VcEventFilterSpec, not of VcEventFilterSpecByTime. VcEventFilterSpecByTime type contains values to specify time range used to filter the events, it doesn't make sense for it to also specify eventTypeId.
The code should be something like:
var spec = new VcEventFilterSpec();
spec.eventTypeId = ["DrsVmMigratedEvent", "VmMigratedEvent"];
spec.entity = new VcEventFilterSpecByEntity();
spec.entity.entity = ...
spec.entity.recursion = ...
spec.time = new VcEventFilterSpecByTime();
spec.time.beginTime = ...
spec.time.endTime = ...
// call eventManager.queryEvents(spec)
Note how the top-level spec of type VcEventFilterSpec contains several sub-specs to filter on some event aspect (entity, time, etc.)
Thanks a lot. Learnt a new thing. Really appreciated.