Default health check script

Do you have the default health check script available. It looks like was posted at https://octopus.com/docs/infrastructure/deployment-targets/machine-policies, but it seems to be partial as just below the script the text mentions a function name that is not included in the script.

I would like to create a custom health check by starting with the default.

Hi @brainerdj,

Thanks for getting in touch! The script in the documentation is technically the entire default health check script, however it is a part of a larger script where we define the functions you are referencing.

The below script is executed in the background and the custom health check script in the UI is injected into it at {{MachinePolicyCustomScript}}.

# Users can write their own custom script via a machine policy, which is appended here via MachinePolicyCustomScript.
$hasErrors = $false
$details = @()

function Convert-ServiceMessageValue([string]$value)
{
	$valueBytes = [System.Text.Encoding]::UTF8.GetBytes($value)
	return [Convert]::ToBase64String($valueBytes)
}

function Set-OctopusVariable([string]$name, [string]$value)
{
    $name = Convert-ServiceMessageValue($name)
    $value = Convert-ServiceMessageValue($value)

	Write-Host "##octopus[setVariable name='$($name)' value='$($value)']"
}

function Write-Warning([string]$message)
{
	Write-Host "##octopus[stdout-warning]"
	Write-Host $message
	Set-OctopusVariable "HasWarnings" "true"
	Write-Host "##octopus[stdout-default]"

	$global:details += "Warning: $message"
}

function Write-Error([string] $message)
{
	Write-Host "##octopus[stdout-error]"
	Write-Host $message
	$global:hasErrors = $true
	Write-Host "##octopus[stdout-default]"

	$global:details += "Error: $message"
}

function Fail-HealthCheck([string]$message)
{
	Write-Error $message
	Finish-HealthCheck
}

function Invoke-HealthCheck()
{
	$hostname = [Environment]::MachineName
	Write-Host "Host Name: $hostname"

	$domainName = [Environment]::UserDomainName
	$userName = [Environment]::UserName
	
	$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
	$windowsPrincipal = new-object 'System.Security.Principal.WindowsPrincipal' $windowsIdentity

	$isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
	Write-Host "Running As: $domainName\$userName (Local Administrator: $isAdmin)"

	Write-Host "Running Tentacle version ${env:TentacleVersion}"
	Set-OctopusVariable "TentacleVersion" ${env:TentacleVersion}

	Write-Host "Tentacle communication uses a '${env:TentacleCertificateSignatureAlgorithm}' certificate"
	Set-OctopusVariable "TentacleCertificateSignatureAlgorithm" ${env:TentacleCertificateSignatureAlgorithm}
	
	$hasLatestCalamari = ((Test-Path "${env:TentacleHome}\Calamari")) -and (Get-ChildItem "${env:TentacleHome}\Calamari" | ? { $_ -match "{{CalamariVersion}}" }).Count -ne 0
	if($hasLatestCalamari) {
		Write-Host "Running latest version of Calamari: {{CalamariVersion}}"
	} else {
		Write-Warning "Not running latest version of Calamari. Expected: {{CalamariVersion}}"
		if ((Test-Path "${env:TentacleHome}\Calamari") -eq $false) {
			Write-Warning "No versions of Calamari are installed."
		}
	}
	Set-OctopusVariable "HasLatestCalamariVersion" $hasLatestCalamari

	Try {
		$OS = Get-WmiObject -class Win32_OperatingSystem
		Set-OctopusVariable "OSName" $OS.Caption
	} Catch [System.Runtime.InteropServices.COMException] {
		Set-OctopusVariable "OSName" "Unknown"
	}
	Set-OctopusVariable "OSVersion" $([System.Environment]::OSVersion.Version.ToString())
	Set-OctopusVariable "ShellName" "PowerShell"
	Set-OctopusVariable "ShellVersion" $PSVersionTable.PSVersion.ToString()

	
	# -----------------------------------------------------------------
	# Variables
	# -----------------------------------------------------------------

	{{VariableDeclarations}}
	
	# -----------------------------------------------------------------
	# Machine policy custom script
	# -----------------------------------------------------------------

	{{MachinePolicyCustomScript}}

}

function CheckDriveCapacity($DriveDetails){
	if($DriveDetails.FreeSpace -lt $freeDiskSpaceThreshold) {	
		Write-Warning $("Drive {0} on {1} only has {2} available" -f $DriveDetails.Name, $hostName, $(Get-FileSizeString $DriveDetails.FreeSpace))		
	} else {			
		Write-Host $("Drive {0} has {1} available" -f $DriveDetails.Name, $(Get-FileSizeString $DriveDetails.FreeSpace))	
	}
}

function Get-FileSizeString([long]$bytes)
{
	if ($bytes -ge 1TB) { return ($bytes / 1TB).ToString("0 TB") }
	if ($bytes -ge 1GB) { return ($bytes / 1GB).ToString("0 GB") }
	if ($bytes -ge 1MB) { return ($bytes / 1MB).ToString("0 MB") }
	if ($bytes -ge 1KB) { return ($bytes / 1KB).ToString("0 KB") }
	return $bytes.ToString("0 bytes");
}

function Finish-HealthCheck() {
	if ($global:details.Length -gt 0) {
		$detailsStr = [string]::Join([Environment]::NewLine, $global:details)
		Set-OctopusVariable "OctopusHealthCheckMessages" $detailsStr
	}

	if ($hasErrors) {
		exit 1
	}
}

Invoke-HealthCheck
Finish-HealthCheck

Does this look like what you are after?

Let me know if you have any further questions here.

Best regards,
Daniel

Exactly what I was looking for and it makes the referenced documentation much clearer. Thank you so much.

1 Like

Hi @brainerdj,

Thanks for the update, I’m glad to hear this helps. :slight_smile:

Please don’t hesitate to get in touch at any time.

Best regards,
Daniel

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