#!/bin/bash
# verify-tuning.sh - verifies apply-tunings.sh results on target hosts.
set -uo pipefail
SSH_OPTS=(-o BatchMode=yes -o ConnectTimeout=8 -o StrictHostKeyChecking=accept-new)

for host in "$@"; do
    echo "================================================================"
    echo "[$host] verification"
    echo "================================================================"

    echo "--- TCP congestion control ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'sysctl net.ipv4.tcp_congestion_control net.core.default_qdisc 2>/dev/null'

    echo "--- vm.swappiness ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'sysctl vm.swappiness 2>/dev/null'

    echo "--- scaling_governor (cpu0) ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null'

    echo "--- NFS mount options (first 2 mounts) ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'nfsstat -m 2>/dev/null | head -30'

    echo "--- storage.cfg: any options lines? ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'grep -c "options" /etc/pve/storage.cfg 2>/dev/null || echo 0'

    echo "--- storage.cfg: NFS stanzas (first 3) ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'grep -A 6 "^nfs:" /etc/pve/storage.cfg 2>/dev/null | head -25'

    echo "--- nconnect TCP connections to NFS servers ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'ss -tn state established "( dport = :nfs or sport = :nfs )" 2>/dev/null | head -20; echo "count:"; ss -tn state established "( dport = :nfs or sport = :nfs )" 2>/dev/null | tail -n +2 | wc -l'

    echo "--- VMs still running? ---"
    ssh "${SSH_OPTS[@]}" "root@$host" 'qm list 2>/dev/null | head -15'

    echo ""
done
