Created
April 5, 2023 00:45
-
-
Save julianschiavo/1c2f0033f9b0fdd1045e5e54e7adfb35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
clear | |
# Initialize variables | |
declare -a output | |
declare -a ping_history | |
max_ping_lines=$(($(tput lines) - 7)) | |
success_count=0 | |
timeout_count=0 | |
total_count=0 | |
interval=300 | |
last_interval_start=$SECONDS | |
# Functions to print in color and bold | |
print_green() { | |
printf "\033[32m$1\033[0m" | |
} | |
print_red() { | |
printf "\033[31m$1\033[0m" | |
} | |
print_bold() { | |
printf "\033[1m$1\033[0m" | |
} | |
# Function to update and display success and timeout statistics | |
update_stats() { | |
tput sc | |
tput cup $(($(tput lines) - 6)) 0 | |
tput ed | |
# Calculate success rates for different time intervals | |
success_rate_30m=$(calc_rate 1800) | |
success_rate_1h=$(calc_rate 3600) | |
success_rate_4h=$(calc_rate 14400) | |
printf "==========================\n" | |
printf "LAST 5 MINUTES:\n" | |
print_bold "Successes:\t"; print_green "$success_count\t\t"; print_bold "Timeouts:\t"; print_red "$timeout_count\n" | |
printf "OVERALL:\n" | |
print_bold "Last 30 minutes:\t"; print_green "$success_rate_30m%%\t\t"; print_bold "Last 1 hour:\t"; print_green "$success_rate_1h%%\t\t"; print_bold "Last 4 hours:\t"; print_green "$success_rate_4h%%\n" | |
tput rc | |
} | |
# Function to calculate success rate for a given time interval | |
calc_rate() { | |
local interval=$1 | |
local now=$(date +%s) | |
local success_count_interval=0 | |
local total_count_interval=0 | |
for i in "${ping_history[@]}"; do | |
IFS=';' read -ra ping_data <<< "$i" | |
ping_timestamp=${ping_data[0]} | |
ping_success=${ping_data[1]} | |
if (( now - ping_timestamp <= interval )); then | |
((total_count_interval++)) | |
if (( ping_success == 1 )); then | |
((success_count_interval++)) | |
fi | |
fi | |
done | |
echo $(awk "BEGIN {printf \"%.2f\", ($success_count_interval / $total_count_interval) * 100}") | |
} | |
# Function to save ping results to a CSV file | |
save_to_csv() { | |
local timestamp=$1 | |
local success=$2 | |
echo "$timestamp,$success" >> pings.csv | |
} | |
# Main loop | |
while true; do | |
# Reset success and timeout counts every 5 minutes | |
if (( $SECONDS - $last_interval_start >= $interval )); then | |
success_count=0 | |
timeout_count=0 | |
last_interval_start=$SECONDS | |
fi | |
# Ping a server and check for success | |
result=$(ping -c 1 -W 1 identity.cs128.org | grep -E '(^PING|data bytes)') | |
success=$(echo "$result" | grep "data bytes") | |
((total_count++)) | |
# Save the ping result with a timestamp | |
timestamp=$(date +%s) | |
human_readable_timestamp=$(date '+%Y-%m-%d %H:%M:%S') | |
if [[ -n "$success" ]]; then | |
((success_count++)) | |
one_line_result=$(echo -e "\033[32m$human_readable_timestamp - $result\033[0m" | tr '\n' ' ') | |
output+=("$one_line_result") | |
ping_history+=("$timestamp;1") | |
save_to_csv "\"$human_readable_timestamp\"" 1 | |
else | |
((timeout_count++)) | |
one_line_result=$(echo -e "\033[31m$human_readable_timestamp - $result\033[0m" | tr '\n' ' ') | |
output+=("$one_line_result") | |
ping_history+=("$timestamp;0") | |
save_to_csv "\"$human_readable_timestamp\"" 0 | |
fi | |
# Keep only the latest max_ping_lines results | |
if [ ${#output[@]} -gt $max_ping_lines ]; then | |
output=("${output[@]:1}") | |
fi | |
# Clear previous ping output and print the latest results | |
tput sc | |
tput cup 0 0 | |
tput ed | |
printf "%s\n" "${output[@]}" | |
tput rc | |
update_stats | |
sleep 10 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment