#!/bin/bash
#***************************************************************#
# ScriptName: grade071002.sh
# Author: xhy
# Create Date: 2025-07-10 21:01
# Modify Author: xhy
# Modify Date: 2025-07-11 13:48
# Version: v1
#***************************************************************#

# 开始判分
echo "开始第三题判分！"
sleep 2s

# 验证函数
check_condition() {
    ssh -q -o StrictHostKeyChecking=no root@$1 "$2"
    if [ $? -eq 0 ]; then
        echo "✅ [成功] $3"
        return 0
    else
        echo "❌ [失败] $3"
        return 1
    fi
}

# 初始化总分
total_score=0

# 定义主机信息
host01="172.24.8.11"
host02="172.24.8.12"
host03="172.24.8.13"
host04="172.24.8.14"
host05="172.24.8.15"
vip="172.24.8.200"

# 1. VIP配置检查（5分）
echo "==== 容器部署检查 ===="

containers=("mysql-server" "zabbix-server-mysql" "zabbix-web-nginx-mysql")
all_running=true

for container in "${containers[@]}"; do
    if ssh $host05 "docker ps --format '{{.Names}}' | grep -q '$container'"; then
        echo "a: 正确 (8分)!" >> /dev/null
    else
        echo "错误：$container 未运行"
        all_running=false
    fi
done

if $all_running; then
    echo "a: 正确 (8分)！"
    total_score=$((total_score + 8))
else
    echo "a: 核心容器缺失!"
fi

# 2. 持久挂载检查（4）分
mysql_container=$(ssh $host05 "docker ps --format '{{.Names}} {{.Ports}}' | grep -E '3306->3306' | awk '{print \$1}'")

if [ -z "$mysql_container" ]; then
    echo "b：容器未运行！"
else
    # 检查挂载点
    check_condition $host05 "docker inspect $mysql_container | grep '/data/mysqldata'" &>> /dev/null
    if [ $? -eq 0 ]; then
        echo "b: 正确 (4分)"
        total_score=$((total_score + 4))
    else
        echo "b：挂载点不匹配!"
    fi
fi

# 3. 网桥配置检查（5分）
echo -e "\n==== 网桥配置检查 ===="
if ssh $host05 "docker network ls | grep myzabbixnet &>/dev/null"; then
    echo "c: 正确 (5分)!"
    total_score=$((total_score + 5))
else
    echo "c：未找到对应的网络！"
fi

# 最终结果
echo -e "\n第三题a/b/c小题总得分/总分: $total_score/17分\n\n"
echo -e "\n第三题d小题请访问： http://172.24.8.15 ，从 Zabbix 图形界面进行判分！\n\n"
