#!/bin/bash
# Server health monitoring — creates GitHub Issue on critical resource usage
# Cron: every 15 min
# Alerts: RAM > 90%, Disk > 85%, Swap > 80%, API unhealthy, containers down

set -uo pipefail

REPO="whiteknightonhorse/APIbase"
ALERTS=""
SEVERITY="MEDIUM"

# --- RAM Check ---
RAM_TOTAL=$(free -m | awk '/Mem:/ {print $2}')
RAM_USED=$(free -m | awk '/Mem:/ {print $3}')
RAM_PCT=$((RAM_USED * 100 / RAM_TOTAL))
if [ "$RAM_PCT" -gt 95 ]; then
  ALERTS="${ALERTS}\n- **CRITICAL:** RAM at ${RAM_PCT}% (${RAM_USED}/${RAM_TOTAL} MB)"
  SEVERITY="CRITICAL"
elif [ "$RAM_PCT" -gt 90 ]; then
  ALERTS="${ALERTS}\n- **HIGH:** RAM at ${RAM_PCT}% (${RAM_USED}/${RAM_TOTAL} MB)"
  [ "$SEVERITY" != "CRITICAL" ] && SEVERITY="HIGH"
fi

# --- Swap Check ---
SWAP_TOTAL=$(free -m | awk '/Swap:/ {print $2}')
SWAP_USED=$(free -m | awk '/Swap:/ {print $3}')
if [ "$SWAP_TOTAL" -gt 0 ]; then
  SWAP_PCT=$((SWAP_USED * 100 / SWAP_TOTAL))
  if [ "$SWAP_PCT" -gt 80 ]; then
    ALERTS="${ALERTS}\n- **HIGH:** Swap at ${SWAP_PCT}% (${SWAP_USED}/${SWAP_TOTAL} MB)"
    [ "$SEVERITY" = "MEDIUM" ] && SEVERITY="HIGH"
  fi
fi

# --- Disk Check ---
DISK_PCT=$(df / | awk 'NR==2 {gsub(/%/,""); print $5}')
if [ "$DISK_PCT" -gt 90 ]; then
  ALERTS="${ALERTS}\n- **CRITICAL:** Disk at ${DISK_PCT}%"
  SEVERITY="CRITICAL"
elif [ "$DISK_PCT" -gt 85 ]; then
  ALERTS="${ALERTS}\n- **HIGH:** Disk at ${DISK_PCT}%"
  [ "$SEVERITY" != "CRITICAL" ] && SEVERITY="HIGH"
fi

# --- API Health ---
API_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 https://apibase.pro/health/ready 2>/dev/null || echo "000")
if [ "$API_STATUS" != "200" ]; then
  ALERTS="${ALERTS}\n- **CRITICAL:** API health check failed (HTTP ${API_STATUS})"
  SEVERITY="CRITICAL"
fi

# --- Unhealthy Containers ---
UNHEALTHY=$(sudo docker ps --filter "name=apibase" --format "{{.Names}} {{.Status}}" 2>/dev/null | grep -v "healthy" | grep -v "^$" || true)
if [ -n "$UNHEALTHY" ]; then
  ALERTS="${ALERTS}\n- **HIGH:** Unhealthy containers:\n\`\`\`\n${UNHEALTHY}\n\`\`\`"
  [ "$SEVERITY" = "MEDIUM" ] && SEVERITY="HIGH"
fi

# --- Top Memory Processes ---
TOP_MEM=$(ps aux --sort=-%mem | head -6 | awk '{printf "  %-10s %5s MB  %s\n", $1, int($6/1024), $11}')

# --- Send Alert if needed ---
if [ -n "$ALERTS" ]; then
  # Deduplicate: don't create issue if one was created in last 6 hours
  LAST_ISSUE=$(gh issue list --repo "$REPO" --search "[Server Health]" --state open --limit 1 --json createdAt -q '.[0].createdAt' 2>/dev/null || echo "")
  if [ -n "$LAST_ISSUE" ]; then
    LAST_TS=$(date -d "$LAST_ISSUE" +%s 2>/dev/null || echo "0")
    NOW_TS=$(date +%s)
    DIFF=$(( (NOW_TS - LAST_TS) / 3600 ))
    if [ "$DIFF" -lt 6 ]; then
      exit 0  # Skip — recent issue exists
    fi
  fi

  TITLE="[Server Health] ${SEVERITY} — $(date +%Y-%m-%d\ %H:%M)"
  BODY="$(cat <<BODY_EOF
## Server Health Alert — ${SEVERITY}

**Time:** $(date -u +%Y-%m-%dT%H:%M:%SZ)
**Server:** 5.78.135.159 (apibase.pro)

### Issues
$(echo -e "$ALERTS")

### Resource Summary
| Resource | Usage |
|----------|-------|
| RAM | ${RAM_PCT}% (${RAM_USED}/${RAM_TOTAL} MB) |
| Swap | ${SWAP_PCT:-0}% (${SWAP_USED:-0}/${SWAP_TOTAL:-0} MB) |
| Disk | ${DISK_PCT}% |
| API | HTTP ${API_STATUS} |

### Top Memory Processes
\`\`\`
${TOP_MEM}
\`\`\`

### Recommended Actions
$(if echo "$ALERTS" | grep -q "RAM\|Swap"; then echo "- Restart long-running Claude CLI sessions to free memory"; fi)
$(if echo "$ALERTS" | grep -q "Disk"; then echo "- Run \`/disk-cleanup\` to free disk space"; fi)
$(if echo "$ALERTS" | grep -q "API"; then echo "- Check: \`sudo docker compose ps\` and \`sudo docker logs apibase-api-1 --tail 20\`"; fi)
$(if echo "$ALERTS" | grep -q "Unhealthy"; then echo "- Restart unhealthy containers: \`sudo docker compose restart <name>\`"; fi)

---
*Auto-generated by server-health-check.sh (every 15 min)*
BODY_EOF
)"

  gh issue create --repo "$REPO" --title "$TITLE" --body "$BODY" 2>/dev/null || true
fi
