Odin/scripts/health-nightwing-gjallar.ps1
2026-06-17 15:55:13 +01:00

133 lines
6.1 KiB
PowerShell

param(
[string] $GjallarRoot = "E:\Projects\Gjallar",
[string] $SshTarget = "nightwing",
[string] $RemoteManifest = "/opt/gamecult/gjallar/gamecult-gjallar-deploy-manifest.txt",
[string] $RemoteStatus = "/var/log/gjallar.status",
[string] $RemoteCultCache = "/var/lib/gamecult/gjallar/cultcache/gjallar.service.cc",
[int] $MaxStatusAgeSeconds = 120,
[int] $MinCatalogProviders = 1,
[int] $MinComposedProviders = 1
)
$ErrorActionPreference = "Stop"
function Fail-IdunnHealth {
param(
[string] $State,
[string] $Message
)
Write-Output "idunn.health.state=$State"
throw $Message
}
ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "systemctl is-active --quiet gjallar.service"
if ($LASTEXITCODE -ne 0) {
Fail-IdunnHealth -State "failed" -Message "gjallar.service is not active on Nightwing."
}
$localCommit = (git -C $GjallarRoot rev-parse HEAD).Trim()
if ([string]::IsNullOrWhiteSpace($localCommit)) {
throw "Could not determine local Gjallar git revision."
}
$manifest = ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "cat '$RemoteManifest' 2>/dev/null"
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($manifest)) {
Fail-IdunnHealth -State "stale-deployment" -Message "Nightwing Gjallar deploy manifest is missing."
}
$remoteCommit = ""
foreach ($line in $manifest) {
if ($line -like "gitCommit=*") {
$remoteCommit = $line.Substring("gitCommit=".Length).Trim()
}
}
if ($remoteCommit -ne $localCommit) {
Fail-IdunnHealth -State "stale-deployment" -Message "Nightwing Gjallar deploy is stale. remote=$remoteCommit local=$localCommit"
}
$statusJson = ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "cat '$RemoteStatus' 2>/dev/null"
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($statusJson)) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing at $RemoteStatus."
}
$status = $statusJson | ConvertFrom-Json
if ($status.schema -ne "gamecult.gjallar.frame.v1") {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness has unexpected schema '$($status.schema)'."
}
$updatedAt = [DateTimeOffset]::Parse($status.updatedAtUtc)
$ageSeconds = ([DateTimeOffset]::UtcNow - $updatedAt.ToUniversalTime()).TotalSeconds
if ($ageSeconds -gt $MaxStatusAgeSeconds) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is stale ($([math]::Round($ageSeconds))s old)."
}
if ($status.status -ne "rendered") {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar framebuffer status is '$($status.status)', expected rendered."
}
if ($null -eq $status.cultCacheWitness -or $status.cultCacheWitness.status -ne "published") {
$detail = if ($null -eq $status.cultCacheWitness) { "missing cultCacheWitness status" } else { $status.cultCacheWitness.error }
Fail-IdunnHealth -State "degraded" -Message "Nightwing Gjallar has not published the daemon-owned CultCache witness. $detail"
}
if ([string]::IsNullOrWhiteSpace([string]$status.receive.lastAttemptStatus)) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing receive.lastAttemptStatus."
}
if ([string]::IsNullOrWhiteSpace([string]$status.receive.lastSuccessfulAtUtc)) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing receive.lastSuccessfulAtUtc."
}
if ([int]$status.receive.staleAfterSeconds -le 0) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness has invalid receive.staleAfterSeconds=$($status.receive.staleAfterSeconds)."
}
if ($null -eq $status.receive.consecutiveFailures) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar status witness is missing receive.consecutiveFailures."
}
if ($status.receive.status -ne "catalog-composed") {
$detail = $status.receive.error
if ([string]::IsNullOrWhiteSpace($detail)) {
$detail = $status.receive.providerFetchError
}
Fail-IdunnHealth -State "dependency-unavailable" -Message "Nightwing Gjallar receive status is '$($status.receive.status)', expected catalog-composed. $detail"
}
if ([int]$status.receive.catalogProviders -lt $MinCatalogProviders) {
Fail-IdunnHealth -State "dependency-unavailable" -Message "Nightwing Gjallar catalog is empty: catalogProviders=$($status.receive.catalogProviders), expected at least $MinCatalogProviders."
}
if ([int]$status.receive.composedProviders -lt $MinComposedProviders) {
Fail-IdunnHealth -State "degraded" -Message "Nightwing Gjallar composed no provider panels: composedProviders=$($status.receive.composedProviders), expected at least $MinComposedProviders."
}
if ($null -eq $status.idunnRudpHealth -or $status.idunnRudpHealth.status -ne "published") {
$detail = if ($null -eq $status.idunnRudpHealth) { "missing idunnRudpHealth status" } else { $status.idunnRudpHealth.error }
Fail-IdunnHealth -State "degraded" -Message "Nightwing Gjallar has not published daemon-owned Idunn RUDP health. $detail"
}
$cultCacheStat = ssh.exe -o BatchMode=yes -o ConnectTimeout=5 $SshTarget "stat -c '%s %Y' '$RemoteCultCache' 2>/dev/null"
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($cultCacheStat)) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness is missing at $RemoteCultCache."
}
$cultCacheParts = $cultCacheStat.Trim() -split '\s+'
if ($cultCacheParts.Length -lt 2) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness stat output was malformed: $cultCacheStat"
}
$cultCacheSize = [int64]$cultCacheParts[0]
$cultCacheModifiedUtc = [DateTimeOffset]::FromUnixTimeSeconds([int64]$cultCacheParts[1])
$cultCacheAgeSeconds = ([DateTimeOffset]::UtcNow - $cultCacheModifiedUtc).TotalSeconds
if ($cultCacheSize -le 0) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness at $RemoteCultCache is empty."
}
if ($cultCacheAgeSeconds -gt $MaxStatusAgeSeconds) {
Fail-IdunnHealth -State "failed" -Message "Nightwing Gjallar CultCache witness is stale ($([math]::Round($cultCacheAgeSeconds))s old)."
}
Write-Host "Nightwing Gjallar is active, deployed at $localCommit, and composing $($status.receive.composedProviders)/$($status.receive.catalogProviders) providers."