2021-08-29 15:43:15 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2025-01-12 15:12:58 +00:00
|
|
|
DEFAULT_HOST="1.0.0.1"
|
|
|
|
PING_COUNT=5
|
|
|
|
RED="#FF0000"
|
|
|
|
ORANGE="#FFA500"
|
|
|
|
YELLOW="#FFFF00"
|
|
|
|
OUTAGE_SYMBOL=""
|
|
|
|
LATENCY_SYMBOL=""
|
2021-08-29 15:43:15 +00:00
|
|
|
|
2025-01-12 15:12:58 +00:00
|
|
|
format_output() {
|
|
|
|
local color=$1
|
|
|
|
local symbol=$2
|
|
|
|
echo "%{F$color}%{T2}$symbol%{F-}%{T-}"
|
|
|
|
}
|
2021-08-29 15:43:15 +00:00
|
|
|
|
2025-01-12 15:12:58 +00:00
|
|
|
get_ping_stats() {
|
|
|
|
fping --quiet --stats --count="$PING_COUNT" "$1" 2>&1
|
|
|
|
}
|
2021-08-29 15:43:15 +00:00
|
|
|
|
2025-01-12 15:12:58 +00:00
|
|
|
host=${1:-$DEFAULT_HOST}
|
|
|
|
stats=$(get_ping_stats "$host")
|
|
|
|
output=""
|
2021-08-29 15:43:15 +00:00
|
|
|
|
2025-01-12 15:12:58 +00:00
|
|
|
unreachable=$(awk '/unreachable/ {print $1}' <<< "$stats")
|
|
|
|
if [ "$unreachable" -eq 1 ]; then
|
|
|
|
output=$(format_output "$RED" "$OUTAGE_SYMBOL")
|
2021-08-29 15:43:15 +00:00
|
|
|
else
|
2025-01-12 15:12:58 +00:00
|
|
|
avg=$(awk '/avg round/ {print $1}' <<< "$stats")
|
|
|
|
avg=${avg%.*}
|
2021-08-29 15:43:15 +00:00
|
|
|
|
2025-01-12 15:12:58 +00:00
|
|
|
if [ "$avg" -gt 90 ]; then
|
|
|
|
color=$RED
|
|
|
|
elif [ "$avg" -gt 75 ]; then
|
|
|
|
color=$ORANGE
|
|
|
|
elif [ "$avg" -gt 60 ]; then
|
|
|
|
color=$YELLOW
|
|
|
|
fi
|
|
|
|
|
|
|
|
[ "$avg" -gt 60 ] && output=$(format_output "$color" "$LATENCY_SYMBOL")
|
2021-08-29 15:43:15 +00:00
|
|
|
fi
|
|
|
|
|
2025-01-12 15:12:58 +00:00
|
|
|
echo "$output"
|