# diagnose.ps1 - emit a single-line JSON health verdict for a Steam GPU host # No AWS write permissions needed. Safe to run on a schedule via SSM State Manager. # # Output contract: the last line of stdout is a compact JSON object with an "ok" # boolean and a "failures" array of tokens. CloudWatch metric filters key off # `{ $.ok = false }`. $failures = @() $findings = [ordered]@{} $cachePath = 'C:\ProgramData\SteamGpu\nvidia-smi.path' New-Item -ItemType Directory -Force -Path (Split-Path $cachePath) | Out-Null # --- 1. GPU present and licensed ---------------------------------------- $smi = $null if (Test-Path $cachePath) { $smi = (Get-Content $cachePath -Raw).Trim() } if (-not $smi -or -not (Test-Path $smi)) { $found = Get-ChildItem -Path C:\ -Recurse -Filter 'nvidia-smi.exe' -ErrorAction SilentlyContinue | Select-Object -First 1 if ($found) { $smi = $found.FullName; $smi | Out-File $cachePath -Encoding ascii } } if ($smi -and (Test-Path $smi)) { try { $q = & $smi -q 2>&1 | Out-String $findings.gpuPresent = $true $findings.gpuLicensed = [bool]($q -match 'License Status\s*:\s*Licensed') $findings.gpuProduct = ([regex]::Match($q, 'Product Name\s*:\s*(.+)')).Groups[1].Value.Trim() $findings.gpuName = (& $smi --query-gpu=name --format=csv,noheader 2>$null | Out-String).Trim() $findings.gpuTempC = (& $smi --query-gpu=temperature.gpu --format=csv,noheader 2>$null | Out-String).Trim() $findings.encoderUtilPct = (& $smi --query-gpu=utilization.encoder --format=csv,noheader 2>$null | Out-String).Trim() } catch { $findings.gpuPresent = $false $findings.gpuError = "$_" } } else { $findings.gpuPresent = $false $findings.gpuError = 'nvidia-smi.exe not found - driver missing or install failed' } # --- 2. DCV ------------------------------------------------------------- $svc = Get-Service dcvserver -ErrorAction SilentlyContinue $findings.dcvServiceStatus = if ($svc) { "$($svc.Status)" } else { 'not-installed' } $findings.dcvListening8443 = [bool](Get-NetTCPConnection -LocalPort 8443 -State Listen -ErrorAction SilentlyContinue) $findings.dcvSessionCount = @(try { dcv list-sessions 2>$null } catch { @() }).Count # --- 3. Storage --------------------------------------------------------- foreach ($dl in @('C', 'D')) { $v = Get-Volume -DriveLetter $dl -ErrorAction SilentlyContinue if ($v) { $findings["drive${dl}FreeGiB"] = [math]::Round($v.SizeRemaining / 1GB, 1) } else { $findings["drive${dl}FreeGiB"] = 'missing' } } # --- 4. Steam offline state -------------------------------------------- $steamProc = Get-Process steam -ErrorAction SilentlyContinue $steamRoot = (Get-ItemProperty 'HKCU:\Software\Valve\Steam' -ErrorAction SilentlyContinue).SteamPath $findings.steamRunning = [bool]$steamProc $findings.steamInstalled = [bool]$steamRoot if ($steamRoot) { $vdf = Join-Path $steamRoot 'config\loginusers.vdf' if (Test-Path $vdf) { $raw = Get-Content $vdf -Raw $findings.steamSavedCredentials = [bool]($raw -match 'RememberPassword\s*"1"') } # Is the library actually on the durable volume? $libFile = Join-Path $steamRoot 'steamapps\libraryfolders.vdf' if (Test-Path $libFile) { $findings.steamLibraryOnD = [bool]((Get-Content $libFile -Raw) -match 'D:\\\\SteamLibrary') } } # --- 5. Identity: is the instance role actually the credential in use? --- try { $ident = aws sts get-caller-identity --output json 2>$null | ConvertFrom-Json $findings.iamArn = $ident.Arn if ($ident.Arn -notmatch ':assumed-role/SteamGpuInstanceRole/') { $failures += 'iam-not-instance-role' } } catch { $findings.iamArn = 'unavailable' $failures += 'aws-cli-unavailable' } # --- Verdict ------------------------------------------------------------ if (-not $findings.gpuPresent) { $failures += 'gpu-missing' } elseif (-not $findings.gpuLicensed) { $failures += 'gpu-unlicensed' } if ($findings.dcvServiceStatus -ne 'Running') { $failures += 'dcv-not-running' } if (-not $findings.dcvListening8443) { $failures += 'dcv-port-closed' } if ($findings.driveDfreeGiB -eq 'missing') { $failures += 'game-volume-missing' } if ($findings.driveCfreeGiB -is [double] -and $findings.driveCfreeGiB -lt 10) { $failures += 'system-disk-low' } if ($findings.steamLibraryOnD -eq $false) { $failures += 'steam-library-not-on-d' } # --- Instance id via IMDSv2 -------------------------------------------- $instanceId = 'unknown' try { $token = Invoke-RestMethod -Method PUT -Uri 'http://169.254.169.254/latest/api/token' ` -Headers @{ 'X-aws-ec2-metadata-token-ttl-seconds' = '60' } $instanceId = Invoke-RestMethod -Uri 'http://169.254.169.254/latest/meta-data/instance-id' ` -Headers @{ 'X-aws-ec2-metadata-token' = $token } } catch { } $result = [ordered]@{ timestamp = (Get-Date).ToUniversalTime().ToString('o') instanceId = $instanceId ok = ($failures.Count -eq 0) failures = $failures detail = $findings } $result | ConvertTo-Json -Depth 5 -Compress