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

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

# 初始化总分
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 "==== VIP配置检查 ===="

if (ssh $host03 "ip a show eth0 | grep -q $vip" || \
    ssh $host04 "ip a show eth0 | grep -q $vip") && \
   ping -c1 -W1 $vip &>/dev/null; then
    echo "a: 正确 (5分)!"
    total_score=$((total_score + 5))
else
    echo "a：VIP配置失败！"
fi

# 2. 域名解析检查（5分） - 验证域名解析到VIP
echo -e "\n==== 域名解析检查 ===="
passed=true

# 检查host03的解析
passed=true
allhosts=("$host01" "$host02" "$host03" "$host04" "$host05")

for host in "${allhosts[@]}"; do
    # 检查www.linuxds.com解析
    if ! ssh $host "getent hosts www.linuxds.com | grep -q $vip"; then
        echo "b：$host: www.linuxds.com 未解析到 $vip"
        passed=false
    fi
    
    # 检查down.linuxds.com解析
    if ! ssh $host "getent hosts down.linuxds.com | grep -q $vip"; then
        echo "b：$host: down.linuxds.com 未解析到 $vip"
        passed=false
    fi
done

if $passed; then
    echo "b: 正确 (5分)!"
    total_score=$((total_score + 5))
else
    echo "b：域名解析配置错误！"
fi

# 3. 调度策略检查（5分）
echo -e "\n==== 调度策略检查 ===="
if ssh $host03 "grep -q 'roundrobin' /etc/haproxy/haproxy.cfg" || \
       ssh $host04 "grep -q 'roundrobin' /etc/haproxy/haproxy.cfg"; then
    echo "c：正确(5分)！"
    total_score=$((total_score + 5))
else
    echo "c：调度策略配置错误！"
fi

# 4. 域名调度检查（8分）
echo -e "\n==== 域名ACL调度检查 ===="
if ssh $host05 "curl -s -m 5 -H 'Host: www.linuxds.com' http://$vip" | grep -q "welcome to my website"; then
    score_d1=4
else
    score_d1=0
    echo "d：ACL调度解析错误！"
fi

# down.linuxds.com 调度检查
if ssh $host05 "curl -s -m 5 -H 'Host: down.linuxds.com' http://$vip" | grep -q "nginx_1.29.0.tar"; then
    score_d2=4
else
    score_d2=0
    echo "d：ACL调度解析错误！"
fi

total_score=$((total_score + score_d1 + score_d2))
echo "d: 正确 ($((score_d1 + score_d2))分）!"

# 5. 统计页面检查（5分）
echo -e "\n==== 统计页面检查 ===="
if curl -s -u admin:redhat1234 --connect-timeout 5 "http://$vip:8080/stats" | grep -q "Statistics"; then
    echo "e：正确(5分)！"
    total_score=$((total_score + 5))
else
    echo "e：统计页面配置错误！"
fi

# 最终结果
echo -e "\n第二题得分/总分: $total_score/28分\n\n"
