#!/usr/bin/env bash
# 检查三路子代理端口与 /status(在服务器上运行)
set -e

check_one() {
  local name="$1" port="$2"
  echo "=== ${name} :${port} ==="
  if command -v ss >/dev/null 2>&1; then
    ss -tlnp 2>/dev/null | grep ":${port} " && echo "  端口: 已被占用" || echo "  端口: 空闲"
  fi
  if command -v curl >/dev/null 2>&1; then
    local body
    body=$(curl -sf --max-time 8 "http://127.0.0.1:${port}/status" 2>/dev/null) || {
      echo "  /status: 无法连接(agent 未启动或崩溃)"
      return
    }
    echo "  /status: ${body:0:200}"
    if echo "${body}" | grep -q '"ok":true'; then
      echo "  结果: OK"
    else
      echo "  结果: ok=false,见上 JSON"
    fi
  else
    echo "  (未安装 curl,跳过 HTTP 检测)"
  fi
  echo
}

check_one "binance" 15200
check_one "okx" 15201
check_one "gate" 15202

echo "PM2 状态:"
pm2 status 2>/dev/null | grep -E 'manual-agent|manual-trading' || true
