1
0
mirror of https://github.com/dcarrillo/dotfiles.git synced 2025-01-22 03:36:46 +00:00
dotfiles/.config/polybar/scripts/network_status

44 lines
884 B
Bash
Executable File

#!/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"