#!/usr/bin/env bash
# check_mcluster_network.sh — Minimal cluster network health check (pretty table)

set -e

NODES=(baobab ebony tapia autonas1 autonas2)
CLUSTER_IPS=(192.168.10.91 192.168.10.92 192.168.10.93 192.168.10.95 192.168.10.96)
MGMT_IPS=(192.168.2.91 192.168.2.92 192.168.2.93 192.168.2.95 192.168.2.96)
SSH_OPTS="-o BatchMode=yes -o ConnectTimeout=5 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR"

# Thunderbridge/thunderbolt status (unchanged)
for i in "${!NODES[@]}"; do
    node="${NODES[$i]}"
    mgmt_ip="${MGMT_IPS[$i]}"
    if [[ "$node" == autonas* ]]; then
        continue
    fi
    mtu=$(ssh $SSH_OPTS root@$mgmt_ip "ip link show thunderbridge 2>/dev/null | grep mtu | awk '{print \$5}'" || echo "fail")
    ports=$(ssh $SSH_OPTS root@$mgmt_ip "bridge link | grep master.*thunderbridge | awk '{print \$2}'" | xargs)
    echo "$node: thunderbridge mtu=$mtu ports=$ports"
    ssh $SSH_OPTS root@$mgmt_ip "ip -o link show | grep 'thunderbolt'" | while read -r line; do
        iface=$(echo "$line" | awk '{print $2}')
        mtu=$(echo "$line" | awk '{print $5}')
        up=$(echo "$line" | grep -q 'UP' && echo "up" || echo "down")
        forwarding=$(ssh $SSH_OPTS root@$mgmt_ip "bridge link show dev $iface 2>/dev/null" | grep -q 'state forwarding' && echo "forwarding" || echo "not-forwarding")
        echo "  $iface mtu=$mtu $up $forwarding"
    done
done

echo
# Table header
printf "%-10s |" "Node"
for node in "${NODES[@]}"; do
    printf " %10s |" "$node"
done
echo
# localhost row
printf "%-10s |" "localhost"
for j in "${!NODES[@]}"; do
    dst_cluster="${CLUSTER_IPS[$j]}"
    if ping -c 1 -W 1 $dst_cluster >/dev/null 2>&1; then
        printf " %10s |" "OK"
    else
        printf " %10s |" "FAILED"
    fi
done
echo
# baobab row
printf "%-10s |" "baobab"
baobab_mgmt="${MGMT_IPS[0]}"
for j in "${!NODES[@]}"; do
    dst_cluster="${CLUSTER_IPS[$j]}"
    if ssh $SSH_OPTS root@$baobab_mgmt "ping -c 1 -W 1 $dst_cluster >/dev/null 2>&1"; then
        printf " %10s |" "OK"
    else
        printf " %10s |" "FAILED"
    fi
done
echo
