File size: 5,091 Bytes
f471034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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