#!/bin/bash
#
# popcheck.sh
# (c) 2024 Cytracom LLC
# Unauthorized use restricted. All rights reserved.
#
######
VERSION="0.5"

declare -A TARGETS
declare -A RESULTS
declare -A BESTPOP
declare -A BESTPOPRESULT
declare -A BESTPOPCARRIER

#### CONFIG SECTION
# The routing table to install popcheck routes
ROUTE_TABLE="bonding"
# The number of pings to use in the latency test
NUMPING=5
# The POPs and targets therein to test
TARGETS["US-Northwest"]="166.0.65.250"
TARGETS["US-Midwest"]="104.234.31.250"
TARGETS["US-Northeast"]="104.234.21.250"
TARGETS["US-Southwest"]="166.0.64.250"
TARGETS["US-Southcentral"]="166.0.67.250"
TARGETS["US-Southeast"]="166.0.66.250"

### END CONFIG SECTION


#### HELPER FUNCTIONS
add_target() {
    local key=$1
    local element=$2
    TARGETS[$key]="${TARGETS[$key]} $element"
}

get_targets() {
    local key=$1
    local IFS=' ' # delimiter
    read -ra array <<< "${TARGETS[$key]}"
    echo "${array[@]}"
}

add_result() {
    local key=$1
    local element=$2
    RESULTS[$key]="${RESULTS[$key]} $element"
}

set_result() {
    local key=$1
    local element=$2
    RESULTS[$key]="$element"
}

get_results() {
    local key=$1
    local IFS=' ' # delimiter
    read -ra array <<< "${RESULTS[$key]}"
    echo "${array[@]}"
}

get_wan_gateways() {
    ip route show table all|grep "default via [0-9\.]* dev eth"|awk '{print $3"/"$5}'|tr '\n' ' '
}

latency_test() {
    local gateway=$1
    ping -c $NUMPING $gateway |tail -n -1 | awk -F/ '{print $5"/"$NF}'
}

add_route() {
    local destination=$1
    local gateway=$(echo $2 | awk -F/ '{print $1}')
    local device=$(echo $2 | awk -F/ '{print $2}')
    ip route add $destination/32 via $gateway dev $device table $ROUTE_TABLE
}

del_route() {
    local destination=$1
    local gateway=$(echo $2 | awk -F/ '{print $1}')
    local device=$(echo $2 | awk -F/ '{print $2}')
    ip route delete $destination/32 via $gateway dev $device table $ROUTE_TABLE
}

determine_carrier() {
    local path="$1"
    echo "$path"|grep -o "zayo\|cogent\|lumen\|level3\|cyxtera"|tail -n -1
}

cleanup() {
    for gateway in $(get_wan_gateways); do
        for targetlist in "${!TARGETS[@]}"; do
            for target in "$(get_targets "$targetlist")"; do
                del_route $target $gateway
            done
        done
    done
    exit 0
}
#### END HELPER FUNCTIONS
trap cleanup SIGINT

popcheck() {
while [ -f /tmp/popcheck.lock ]; do echo "/tmp/popcheck.lock exists, waiting for lock to free. If you are sure no popcheck is currently running, rm the lock file." >&2 ;sleep 2;done
touch /tmp/popcheck.lock
echo "popcheck.sh version $VERSION for $(hostname) - Generated at $(date)"
if [[ "$1" == "full" ]]; then
echo "Mode: Full test with throughput"
else
echo "Mode: Quick ping + trace test"
echo "-> for a full test with throughput run as: popcheck full"
fi
echo
echo "==="
echo "Test Targets:"
for target in "${!TARGETS[@]}"; do
    echo " - $target: $(get_targets "$target")"
done
echo "==="
echo "WAN Gateways:"
for gateway in $(get_wan_gateways); do
    echo " - $gateway"
done
echo "==="
echo
echo
echo "==="
for gateway in $(get_wan_gateways); do
    echo "==="
    echo "Beginning Regional Latency test for $gateway"
    echo "==="
    echo "Adding Test Routes to ${ROUTE_TABLE} route table for $gateway"
    for targetlist in "${!TARGETS[@]}"; do
        for target in "$(get_targets "$targetlist")"; do
            add_route $target $gateway
        done
    done
    echo "==="
    echo "Latency Results (avg/mdev ms):"
    for targetlist in "${!TARGETS[@]}"; do
        for target in "$(get_targets "$targetlist")"; do
            result=$(latency_test $target)
            avg=$(echo "$result"|awk -F/ '{print $1}')
            set_result $targetlist $avg
            echo " - $targetlist ($target): $result"
        done
    done
    for resultlist in "${!RESULTS[@]}"; do
        thisresult=${RESULTS[$resultlist]}
        currentbestresult=${RESULTS[${BESTPOP[$gateway]}]} 2>/dev/null
        echo "Comparing $thisresult with $currentbestresult for $resultlist"
        if awk -v val1="$thisresult" -v val2="$currentbestresult" "BEGIN {exit !(val1 < val2)}"; then
            BESTPOP["$gateway"]=$resultlist
            BESTPOPRESULT["$gateway"]=$thisresult
        fi
        if [[ $currentbestresult == "" ]]; then # handle the initial case
            BESTPOP["$gateway"]=$resultlist
            BESTPOPRESULT["$gateway"]=$thisresult
        fi

    done
    echo
    echo "Best POP for $gateway is ${BESTPOP[$gateway]} (RTT ${BESTPOPRESULT[$gateway]}ms)"
    echo "==="
    echo "Forward traceroute and iperf to ${BESTPOP[$gateway]}"
    for target in "$(get_targets ${BESTPOP[$gateway]})"; do
        trace=$(mtr -c3 -w $target)
        echo "$trace"
        BESTPOPCARRIER[$gateway]=$(determine_carrier "$trace")
        if which iperf3 > /dev/null && [[ "$1" == "full" ]]; then
            iperf3 -i 10 -O5 --get-server-output -P4 -c $target
            iperf3 -i 10 -O5 --get-server-output -P4 -R -c $target
        else
            echo "iperf3 not installed, skipping..."
        fi
    done
    echo "Removing Test Routes to ${ROUTE_TABLE} route table for $gateway"
    for targetlist in "${!TARGETS[@]}"; do
        for target in "$(get_targets "$targetlist")"; do
            del_route $target $gateway
        done
    done
    echo "==="
done
sleep 2 # wait for route to settle
echo
echo "Tunneled iperf to ${BESTPOP[$gateway]} (no security engine gateway)"
for target in "$(get_targets ${BESTPOP[$gateway]})"; do
        if which iperf3 > /dev/null && [[ "$1" == "full" ]]; then
            iperf3 -i -O5 --get-server-output -P4 -c $target
            iperf3 -i -O5 --get-server-output -P4 -R -c $target
        else
            echo "iperf3 not installed, skipping..."
        fi
done
for gateway in $(get_wan_gateways); do
        echo "Best POP for $gateway is ${BESTPOP[$gateway]} through ${BESTPOPCARRIER[$gateway]} ${BESTPOPRESULT[$gateway]}ms"
done
echo
rm /tmp/popcheck.lock
}

popcheck $1 | tee /tmp/$(hostname).txt
authkey=$(hostname)-$(date +%s)
curl -s -X POST -F "file=@/tmp/$(hostname).txt" "http://45.79.48.102:6949/upload?authkey=$authkey"
echo "==="
echo
echo
echo "View this popcheck.sh report at: http://peers.cytra.network:6949/retrieve/$authkey"

