#!/bin/bash # 服务器健康检查脚本 # 用法: ./health-check.sh [aliyun|website|all] set -e TOOLS_SERVER="root@101.200.136.200" TOOLS_KEY="~/.ssh/tools.pem" WEBSITE_SERVER="root@192.144.137.14" WEBSITE_KEY="~/.ssh/officialWebsite.pem" check_tools_server() { echo "======================================" echo "Tools 服务器 (101.200.136.200)" echo "======================================" ssh -i $TOOLS_KEY -o ConnectTimeout=5 $TOOLS_SERVER << 'EOF' echo "--- 系统负载 ---" uptime echo "" echo "--- 内存使用 ---" free -h echo "" echo "--- 磁盘使用 ---" df -h | grep -E '^/dev|Filesystem' echo "" echo "--- Docker 容器 ---" docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' echo "" echo "--- 系统服务 ---" echo -n "Jenkins: "; systemctl is-active jenkins echo -n "Nginx: "; systemctl is-active nginx echo -n "Docker: "; systemctl is-active docker echo "" echo "--- 端口检查 ---" netstat -tlnp 2>/dev/null | grep -E ':3000|:8080|:10022|:5000' | awk '{print $4, $7}' EOF } check_website_server() { echo "======================================" echo "Website 服务器 (192.144.137.14)" echo "======================================" ssh -i $WEBSITE_KEY -o ConnectTimeout=5 $WEBSITE_SERVER << 'EOF' echo "--- 系统负载 ---" uptime echo "" echo "--- 内存使用 ---" free -h echo "" echo "--- 磁盘使用 ---" df -h | grep -E '^/dev|Filesystem' echo "" echo "--- Nginx 状态 ---" systemctl is-active nginx || echo "nginx not running" EOF } case "${1:-all}" in aliyun|tools) check_tools_server ;; website) check_website_server ;; all) check_tools_server echo "" check_website_server ;; *) echo "用法: $0 [aliyun|website|all]" exit 1 ;; esac