VMware Cloud Community
Lapsang28
Contributor
Contributor
Jump to solution

Retrieve tasks and events for a Virtual Machine

Hello everyone.

This question may look trivial to some people here, but I didn't manage to make it work : I'd like to use VCO to retrieve tasks and events for a specific vm (IN parameter). Can someone help me to do so ?

Best regards

Reply
0 Kudos
1 Solution

Accepted Solutions
virtualsabi
Enthusiast
Enthusiast
Jump to solution

As i said, you have to create a collector using the method createCollectorForTasks in VcTaskManager.

The parameter of this method is an instance of VcTaskFilterSpec in which you specify the object to filter (in your case the VM entity reference)

You will get a VcTaskHistoryCollector which can than be browsed using the readNextTasks and readPreviousTasks methods.

// Get TaskManager service

var sdktm = vm.sdkConnection.taskManager;

// Create FilterSpec containing vm reference to filter

var filter = new VcTaskFilterSpec();

var spec = new VcTaskFilterSpecByEntity();

spec.entity = vm.reference;

spec.recursion = VcTaskFilterSpecRecursionOption.self;

filter.entity = spec;

// Create collector

var collector = sdktm.createCollectorForTasks(filter);

collector.resetCollector();

// Browse all pages returned by collector (10 entries per page)

var taskPage;

while ((taskPage = collector.readPreviousTasks(10)) != null)

{

    for each (var task in taskPage)

    {

        System.log("Task: " + task.name + " -> " + task.startTime);

    }

}   

View solution in original post

9 Replies
virtualsabi
Enthusiast
Enthusiast
Jump to solution

Hi,

one way to achieve your goal is using the parent sdkConnection of your VM object and using the taskManager and eventManager.

For each one (Task and Event) create a FilterSpec (containing your VM) and trigger the createCollectorForTasks/createCollectorForEvents method.

The result will be a browsable/paginated collector where you can retrieve your tasks and events for your VM object.

Greetz

Reply
0 Kudos
Lapsang28
Contributor
Contributor
Jump to solution

Thanks, it's a way to start. But (of course, there is a but...) :

Supposing I have a vm object, and sdktm his task manager (var sdktm = vm.sdkConnection.taskManager). According the documentation (https://www.vmware.com/support/orchestrator/doc/vco_vsphere55_api/html/VcTaskManager.html), the only thing can use in this object is recentTask, taht only gathers the last 10 minutes. Is there a way to have older informations ?

Here is my code example :

var sdktm = vm.sdkConnection.taskManager;

System.log(sdktm.description);

System.log('--------------');

for each(task in sdktm.recentTask)

{

    System.log(task.info.startTime);

    System.log(task.info.completeTime);

    System.log(task.info.entityName);

    System.log(task.info.descriptionId);

    System.log(task.info.changeTag);

    System.log('--------------');

   

}

Reply
0 Kudos
virtualsabi
Enthusiast
Enthusiast
Jump to solution

As i said, you have to create a collector using the method createCollectorForTasks in VcTaskManager.

The parameter of this method is an instance of VcTaskFilterSpec in which you specify the object to filter (in your case the VM entity reference)

You will get a VcTaskHistoryCollector which can than be browsed using the readNextTasks and readPreviousTasks methods.

// Get TaskManager service

var sdktm = vm.sdkConnection.taskManager;

// Create FilterSpec containing vm reference to filter

var filter = new VcTaskFilterSpec();

var spec = new VcTaskFilterSpecByEntity();

spec.entity = vm.reference;

spec.recursion = VcTaskFilterSpecRecursionOption.self;

filter.entity = spec;

// Create collector

var collector = sdktm.createCollectorForTasks(filter);

collector.resetCollector();

// Browse all pages returned by collector (10 entries per page)

var taskPage;

while ((taskPage = collector.readPreviousTasks(10)) != null)

{

    for each (var task in taskPage)

    {

        System.log("Task: " + task.name + " -> " + task.startTime);

    }

}   

Lapsang28
Contributor
Contributor
Jump to solution

This is great. Thanks a lot, I will start from your code, it will help me a lot !

Reply
0 Kudos
Lapsang28
Contributor
Contributor
Jump to solution

I have another question (just in order to deserve my "noob" certificate on vco) : I'd like to filter by date. Here part of the code :

[code]

   // Get TaskManager service

    var sdktm = vm.sdkConnection.taskManager;

    

    // Create FilterSpec containing vm reference to filter 

    var filter = new VcTaskFilterSpec();

    var spec = new VcTaskFilterSpecByEntity();

    spec.entity = vm.reference;

    spec.recursion = VcTaskFilterSpecRecursionOption.self;

    // time filter

    var vtime = new VcTaskFilterSpecByTime();

    vtime.beginTime = new Date("2015-09-18");

       vtime.timeType = "startedTime";

  

  

    filter.entity = spec;

    filter.time = vtime;

    

    // Create collector

    var collector = sdktm.createCollectorForTasks(filter);

    collector.resetCollector();

    

    // Browse all pages returned by collector (10 entries per page)

    var taskPage;

    while ((taskPage = collector.readPreviousTasks(10)) != null)

    {

        for each (var task in taskPage)

        {

            if(task.name != 'LeaseMapDiskRegion')

            {

                System.log("Task: " + task.name + " -> " + task.startTime);

            }

        }

    }    

[/code]

But I got the issue : Cannot convert startedTime to com.vmware.vim.vi4.TaskFilterSpecTimeOption

I don't know what to put in this value to make it work, I tried everything that comes in my mind...

Reply
0 Kudos
virtualsabi
Enthusiast
Enthusiast
Jump to solution

Instead of using a string, provide the correct object/enum to your filter.

It's not:

vtime.timeType = "startedTime";

It's:

vtime.timeType = VcTaskFilterSpecTimeOption.startedTime;

The api is sometimes very "interesting", but after more than a decade working with it it's like reading the matrix Smiley Wink

Reply
0 Kudos
Lapsang28
Contributor
Contributor
Jump to solution

Thanks, I will try that as soon as possible.

Reply
0 Kudos
Lapsang28
Contributor
Contributor
Jump to solution

I was close... and then...

First of all, I'd like to thank virtualsabi for his great help.

Now I've got an issue. I made a lot of tests, using his code, it was working, and now I've got the message "The operation is not allowed in the current state" when I do " var collector = sdktm.createCollectorForTasks(filter);  "

According to google, this issue occurs when you create too much collectors, without destroying them after. Ok I get it, I did it without destroying them, so I guess there are a lot now.

My question is : how do I find the existing collectors, so I can use "collector.destroyCollector();" ?

Thanks in advance.

Reply
0 Kudos
Lapsang28
Contributor
Contributor
Jump to solution

OK, found it. I just had to reboot vco server Smiley Happy

Reply
0 Kudos