111 lines
3.1 KiB
PowerShell
111 lines
3.1 KiB
PowerShell
|
|
# Health check script for the Home Server Agent (PowerShell)
|
||
|
|
# This script can be used with monitoring tools on Windows
|
||
|
|
|
||
|
|
param(
|
||
|
|
[Parameter(Mandatory=$true)]
|
||
|
|
[ValidateSet('health', 'gameserver')]
|
||
|
|
[string]$CheckType,
|
||
|
|
|
||
|
|
[Parameter(Mandatory=$false)]
|
||
|
|
[string]$ServerName,
|
||
|
|
|
||
|
|
[Parameter(Mandatory=$false)]
|
||
|
|
[string]$AgentUrl = "http://localhost:3000",
|
||
|
|
|
||
|
|
[Parameter(Mandatory=$false)]
|
||
|
|
[string]$ApiToken = $env:API_TOKEN,
|
||
|
|
|
||
|
|
[Parameter(Mandatory=$false)]
|
||
|
|
[int]$Timeout = 10
|
||
|
|
)
|
||
|
|
|
||
|
|
# Function to check if agent is healthy
|
||
|
|
function Test-AgentHealth {
|
||
|
|
try {
|
||
|
|
$headers = @{}
|
||
|
|
if ($ApiToken) {
|
||
|
|
$headers["Authorization"] = "Bearer $ApiToken"
|
||
|
|
}
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$AgentUrl/health" -Headers $headers -TimeoutSec $Timeout -ErrorAction Stop
|
||
|
|
|
||
|
|
if ($response.status -eq "healthy") {
|
||
|
|
Write-Output "OK: Home Server Agent is healthy"
|
||
|
|
return 0
|
||
|
|
} else {
|
||
|
|
Write-Output "CRITICAL: Home Server Agent reported unhealthy status"
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch {
|
||
|
|
Write-Output "CRITICAL: Home Server Agent is not responding - $($_.Exception.Message)"
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Function to check game server status
|
||
|
|
function Test-GameServerStatus {
|
||
|
|
param([string]$ServerName)
|
||
|
|
|
||
|
|
if (-not $ServerName) {
|
||
|
|
Write-Output "UNKNOWN: Server name not provided"
|
||
|
|
return 3
|
||
|
|
}
|
||
|
|
|
||
|
|
if (-not $ApiToken) {
|
||
|
|
Write-Output "UNKNOWN: API_TOKEN not provided"
|
||
|
|
return 3
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$headers = @{
|
||
|
|
"Authorization" = "Bearer $ApiToken"
|
||
|
|
}
|
||
|
|
|
||
|
|
$response = Invoke-RestMethod -Uri "$AgentUrl/api/gameserver/$ServerName/status" -Headers $headers -TimeoutSec $Timeout -ErrorAction Stop
|
||
|
|
|
||
|
|
switch ($response.status) {
|
||
|
|
"running" {
|
||
|
|
Write-Output "OK: $ServerName is running"
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
"stopped" {
|
||
|
|
Write-Output "WARNING: $ServerName is stopped"
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
default {
|
||
|
|
Write-Output "CRITICAL: $ServerName status unknown ($($response.status))"
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch {
|
||
|
|
Write-Output "CRITICAL: Cannot check $ServerName status - $($_.Exception.Message)"
|
||
|
|
return 2
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Main execution
|
||
|
|
$exitCode = 0
|
||
|
|
|
||
|
|
switch ($CheckType) {
|
||
|
|
"health" {
|
||
|
|
$exitCode = Test-AgentHealth
|
||
|
|
}
|
||
|
|
"gameserver" {
|
||
|
|
$exitCode = Test-GameServerStatus -ServerName $ServerName
|
||
|
|
}
|
||
|
|
default {
|
||
|
|
Write-Output "Usage: health-check.ps1 -CheckType {health|gameserver} [-ServerName <name>]"
|
||
|
|
Write-Output "Environment variables:"
|
||
|
|
Write-Output " API_TOKEN - Authentication token for API access"
|
||
|
|
Write-Output "Parameters:"
|
||
|
|
Write-Output " -AgentUrl - URL of the Home Server Agent (default: http://localhost:3000)"
|
||
|
|
Write-Output " -ApiToken - Authentication token for API access"
|
||
|
|
Write-Output " -Timeout - Request timeout in seconds (default: 10)"
|
||
|
|
$exitCode = 3
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
exit $exitCode
|