How can I get data about worker machine usage from tasks in Octopus Deploy?

Currently, using the web interface, there is no way to get an idea of worker usage. Whilst not ideal, there is a way to get details of worker usage by task by using the Octopus REST API.

Using the tasks endpoint, you can interrogate the log to get the worker machine used. We can see in the Octopus task log view this is available in the verbose view.

Leased worker 21-05-11-2301-h6hds from pool Hosted Windows (lease Leases-29586148).

To retrieve this data for many tasks, call the tasks API, filtering on just Deploy and RunbookRun task types, and interrogate the log messages to get the worker information.

    $tasks = Invoke-RestMethod -Uri "$octopusURL/api/tasks?skip=$($skip)&take=$($take)&spaces=$($spaceId)&includeSystem=false&name=deploy,runbookrun" -Headers $header 

Tip - using the take and skip API parameters, you can reduce resource usage on the Octopus server and network.

Delving into the task detail, using the tasks/{id}/details?verbose=true endpoint, we can find the worker information.

Using a little string manipulation, the worker data can be extracted to a file:


$taskProperties = [System.Collections.ArrayList]::new();
$taskProperties.Add(@("TaskId","TaskName","TaskDescription","Started","Ended","MessageText","WorkerName","WorkerPool"))

function Get-WorkerInfo($activityLogElement){
    foreach ($logChild1 in $activityLogElement.Children) {
        foreach ($logElement in $logChild1.LogElements) {
            if ($logElement.MessageText -clike 'Leased worker*') {

                # Get worker detail from the message
                $splitMessage = $logElement.MessageText.Split(' ')
                $workerName = $splitMessage[2]
                $workerPoolItemSection = $splitMessage.Length - 5
                $workerPoolAndLease = ($splitMessage | Select-Object -Last $workerPoolItemSection) -join " "
                $workerPoolName = $workerPoolAndLease.Split('(')[0]

                $taskProperties.Add(@(
                        $task.Id,
                        $task.Name,
                        $task.Description,
                        $task.StartTime,
                        $task.CompletedTime,
                        $logElement.MessageText,
                        $workerName,
                        $workerPoolName)
                )
            }
        }
    }
}

The full script to retrieve this information can be found in our GitHub Octopus-API repository.

And the output file looks like this:

TaskId^TaskName^TaskDescription^Started^Ended^MessageText^WorkerName^WorkerPool
ServerTasks-372162^Deploy^Deploy Release Orchestration release 2021.1.2 to Staging^12/05/2021 20:27:51^12/05/2021 20:29:11^Leased worker 21-05-11-2301-h6hds from pool Hosted Windows (lease Leases-29586148).^21-05-11-2301-h6hds^Hosted Windows 
ServerTasks-372162^Deploy^Deploy Release Orchestration release 2021.1.2 to Staging^12/05/2021 20:27:51^12/05/2021 20:29:11^Leased worker 21-05-11-2301-h6hds from pool Hosted Windows (lease Leases-29586150).^21-05-11-2301-h6hds^Hosted Windows 
ServerTasks-297984^Deploy^Deploy Release Orchestration release 2021.1.1-RC4 to Prod Approval^03/03/2021 22:15:40^^Leased worker 21-03-03-2025-l1qfn from pool Hosted Windows (lease Leases-25482365).^21-03-03-2025-l1qfn^Hosted Windows 
ServerTasks-286531^Deploy^Deploy Release Orchestration release 2021.1.1-RC4 to Staging^22/02/2021 18:16:47^22/02/2021 18:19:08^Leased worker 21-02-22-0451-j24wv from pool Hosted Windows (lease Leases-24887000).^21-02-22-0451-j24wv^Hosted Windows 
ServerTasks-286531^Deploy^Deploy Release Orchestration release 2021.1.1-RC4 to Staging^22/02/2021 18:16:47^22/02/2021 18:19:08^Leased worker 21-02-22-0451-j24wv from pool Hosted Windows (lease Leases-24887023).^21-02-22-0451-j24wv^Hosted Windows 

Note Some tasks will have used multiple worker machines, as multiple steps could use a worker, therefore will show multiple rows in the output.