#!/bin/bash # Health check script for the Home Server Agent # This script can be used with monitoring tools like Nagios, Zabbix, etc. AGENT_URL="${AGENT_URL:-http://localhost:3000}" API_TOKEN="${API_TOKEN:-}" TIMEOUT="${TIMEOUT:-10}" # Function to check if agent is healthy check_health() { local response local status_code if [ -n "$API_TOKEN" ]; then response=$(curl -s -w "%{http_code}" -m "$TIMEOUT" \ -H "Authorization: Bearer $API_TOKEN" \ "$AGENT_URL/health" 2>/dev/null) else response=$(curl -s -w "%{http_code}" -m "$TIMEOUT" \ "$AGENT_URL/health" 2>/dev/null) fi status_code="${response: -3}" if [ "$status_code" = "200" ]; then echo "OK: Home Server Agent is healthy" return 0 else echo "CRITICAL: Home Server Agent is not responding (HTTP $status_code)" return 2 fi } # Function to check game server status check_gameserver() { local server_name="$1" local response local status_code if [ -z "$server_name" ]; then echo "UNKNOWN: Server name not provided" return 3 fi if [ -z "$API_TOKEN" ]; then echo "UNKNOWN: API_TOKEN not provided" return 3 fi response=$(curl -s -w "%{http_code}" -m "$TIMEOUT" \ -H "Authorization: Bearer $API_TOKEN" \ "$AGENT_URL/api/gameserver/$server_name/status" 2>/dev/null) status_code="${response: -3}" response_body="${response%???}" if [ "$status_code" = "200" ]; then # Parse JSON response to get status status=$(echo "$response_body" | grep -o '"status":"[^"]*"' | cut -d'"' -f4) case "$status" in "running") echo "OK: $server_name is running" return 0 ;; "stopped") echo "WARNING: $server_name is stopped" return 1 ;; *) echo "CRITICAL: $server_name status unknown ($status)" return 2 ;; esac else echo "CRITICAL: Cannot check $server_name status (HTTP $status_code)" return 2 fi } # Main execution case "$1" in "health") check_health ;; "gameserver") check_gameserver "$2" ;; *) echo "Usage: $0 {health|gameserver }" echo "Environment variables:" echo " AGENT_URL - URL of the Home Server Agent (default: http://localhost:3000)" echo " API_TOKEN - Authentication token for API access" echo " TIMEOUT - Request timeout in seconds (default: 10)" exit 3 ;; esac