Skip to content

Instantly share code, notes, and snippets.

@tcsantanello
Last active April 18, 2021 19:02
Show Gist options
  • Save tcsantanello/f008ecc77aba6398c1cd641951600957 to your computer and use it in GitHub Desktop.
Save tcsantanello/f008ecc77aba6398c1cd641951600957 to your computer and use it in GitHub Desktop.
Multi-host Ping Monitoring
#!/bin/bash
set -o pipefail
IPS=( "$@" )
MAX=$(( $( tput lines ) - 3 )) ## Heading + 1 = 3
test ${MAX} -lt 10 && MAX=10
function print_row( ) {
printf "| "
printf "%20s |" "${@}"
echo ""
}
for (( loop = 0; ; ++loop )); do
PING_HISTORY_0=( "$( date +%j-%H%M%S.%N )" ${PING_HISTORY_0[@]:0:${MAX}-1} )
for (( i = 1; i <= ${#IPS[@]}; ++i )); do
val=$( ping -W 0.2 -c 1 ${IPS[$i-1]} | sed '/bytes from/!d;s, ,,g;s,.*time=,,g' )
case $? in
0) ;;
1) val="TIMEOUT"; ;;
*) val="ERROR"; ;;
esac
eval PING_HISTORY_$i=\( "$val" "\${PING_HISTORY_$i[@]:0:${MAX}-1}" \)
done
clear
print_row "" ${IPS[@]}
print_row "-" ${IPS[@]/*/-}
for (( n = 0; n < ${#PING_HISTORY_0[@]}; ++n )); do
print_row $(
for (( i = 0; i <= ${#IPS[@]}; ++i )); do
eval echo "\${PING_HISTORY_$i[$n]}"
done
)
done
sleep 0.2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment