From f94d9566dfb3a7c864a2738c1da3e5ec6405a0b1 Mon Sep 17 00:00:00 2001 From: Daniel Carrillo Date: Sun, 12 Jan 2025 16:12:58 +0100 Subject: [PATCH] [polybar] Refactor network_status --- .config/polybar/scripts/network_status | 62 +++++++++++++++----------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/.config/polybar/scripts/network_status b/.config/polybar/scripts/network_status index 20d1c60..fdd8b40 100755 --- a/.config/polybar/scripts/network_status +++ b/.config/polybar/scripts/network_status @@ -1,35 +1,43 @@ #!/usr/bin/env bash -URL=${1:-1.0.0.1} +DEFAULT_HOST="1.0.0.1" +PING_COUNT=5 +RED="#FF0000" +ORANGE="#FFA500" +YELLOW="#FFFF00" +OUTAGE_SYMBOL="" +LATENCY_SYMBOL="" -output=$(fping --quiet --outage --count=5 "$URL" 2>&1) -outage_output="" -outage_color="FFFFFF" -avg_output="" -avg_color="FFFFFF" +format_output() { + local color=$1 + local symbol=$2 + echo "%{F$color}%{T2}$symbol%{F-}%{T-}" +} -avg=$(echo "$output" | cut -d "," -f 3 | cut -d "=" -f 2 | cut -d "/" -f 2 | cut -d "." -f 1) -outage=$(echo "$output" | cut -d "," -f 2 | cut -d "=" -f 2 | tr -d " " | cut -d "." -f 1) +get_ping_stats() { + fping --quiet --stats --count="$PING_COUNT" "$1" 2>&1 +} -if [ "$outage" -eq 0 ] || [ -z "$outage" ]; then - outage_output="" -elif [ "$outage" -lt 30 ]; then - outage_color=F5A70A +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 - outage_color=FF0000 -fi - -if [ -z "$avg" ] || [ "$avg" -gt 60 ]; then - avg_color=FF0000 -elif [ "$avg" -lt 40 ]; then - avg_output="" -else - avg_color=F5A70A -fi - -if [ -n "$outage_output" ] || [ -n "$avg_output" ]; then - echo "%{F#$outage_color}%{T2}${outage_output}%{F-}%{T-}%{F#$avg_color}%{T2} ${avg_output}%{F-}%{T-}" -else - echo "" + 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"