Health checking deployment targets using Octopus API

Is there an endpoint on Octopus’s API / code sample where we can check the health of a particular deployment target?

I saw a code sample for creating a health check: Run a health check - Octopus Deploy

But we already have periodic health checks scheduled in Octopus, so it’d be more efficient to specify the target name / id & grab the result of the last health check.

Hey @ali.akhtar,

Thanks for reaching out. Just to confirm, you are wanting to RUN a health check on a specific target, not just check the current health of that target (Is it healthy/not) and return that value, correct?

Looking forward to hearing back.

Best,
Jeremy

It’d be preferable to just check the health of a target using the API (using the last run health check)

Thanks for the confirmation.

I believe the easiest method would be to hit the endpoint for that specific machine:

https://serverurl/api/Spaces-####/machines/Machines-####

It will return a JSON and I believe the values you will want to utilize will be IsDisabled, HealthStatus, and StatusSummary.

So a quick powershell query could be:

# Define working variables
$OctopusURL = "https://" #for example https://local.octopus.com
$OctopusAPIKey = "" #for example API-############
$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }
$SpaceName = "" #input space name that has the machines here
$MachineIDs = @("Machines-501","Machines-991") #put 1 or more machine IDs here separated by commas

$Space = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/spaces/all" -Headers $Header) | Where-Object { $_.Name -eq $SpaceName }

foreach ($machineID in $MachineIDs){
$Machine = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/$($Space.id)/machines/$($machineID)" -Headers $Header)
Write-Host $machine.IsDisabled
Write-Host $machine.HealthStatus
Write-Host $machine.StatusSummary
}

Please test that thoroughly as I quickly wrote and tested it and can’t guarantee it will work in your scenario.

Octopus is API driven so anything you can do in the portal you should be able to do with scripting. The method that I got the above Endpoint and the JSON format in the response was to open Developer tools in Firefox/Chrome by pressing F12, going to the network tab, then clicking the Connectivity page within a target. You can then see all of the calls on any given page in the Network tab and click into them to get more information. Following this method while doing most things in the Octopus portal will show you the endpoints involved in the call as well as the required data and resulting data.

Please let me know if that helps or if you have any questions.

Best,
Jeremy

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.