#!/usr/bin/env bash DEFAULT_HOST="1.0.0.1" PING_COUNT=5 RED="#FF0000" ORANGE="#FFA500" YELLOW="#FFFF00" OUTAGE_SYMBOL="" LATENCY_SYMBOL="" format_output() { local color=$1 local symbol=$2 echo "%{F$color}%{T2}$symbol%{F-}%{T-}" } get_ping_stats() { fping --quiet --stats --count="$PING_COUNT" "$1" 2>&1 } host=${1:-$DEFAULT_HOST} stats=$(get_ping_stats "$host") output="" unreachable=$(awk '/unreachable/ {print $1}' <<< "$stats") if [ "$unreachable" -eq 1 ]; then output=$(format_output "$RED" "$OUTAGE_SYMBOL") else avg=$(awk '/avg round/ {print $1}' <<< "$stats") avg=${avg%.*} 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") fi echo "$output"