#!/bin/sh

# Ping should respond in < 1 ms on a dedicated network, maybe up to a few ms
# on a campus network.  If ping supports it, reduce wait to < 1 second.
case $(auto-ostype) in
FreeBSD)
    ping_flags="-W 100" # FreeBSD -W takes milliseconds, use it to reduce wait
    ;;

RHEL|NetBSD)
    ping_flags="-w 1"   # Best we can do it 1 second
    ;;

esac

status=0
tmpfile=cluster-node-status-uptime
printf "%-20s %-10s %s\n" "Host" "Ping" "ssh node uptime"
for node in `cluster-all-nodes`; do
    printf "%-20s " $node
    if ! ping -c 1 $ping_flags $node > /dev/null 2>&1; then
	printf "%-10s %s\n" "Failed" "-"
	status=1
    else
	printf "%-10s " "OK"
	if ! ssh -o ConnectTimeout=10 $node uptime > $tmpfile 2> /dev/null; then
	    printf "Failed\n"
	    status=1
	else
	    awk -F , '{ print $1 }' $tmpfile | awk -F 'up ' '{ printf("Up %s\n", $2); }'
	fi
	rm $tmpfile
    fi
done
exit $status

