#!/bin/bash
# Function to format output as JSON
function format {
local tooltip=""
local first=true
while IFS= read -r line; do
if [[ -z "$line" ]]; then
continue
fi
# Check if the line starts with "interface" or "peer"
if [[ "$line" == interface:* ]]; then
if [ "$first" = false ]; then
tooltip+="\n"
fi
tooltip+="Interface: ${line#*: }"
first=false
elif [[ "$line" == peer:* ]]; then
if [ "$first" = false ]; then
tooltip+="\n"
fi
tooltip+="Peer: ${line#*: }"
first=false
elif [[ "$line" == *"received,"* ]]; then
# Extract received and sent data
received=$(echo "$line" | grep -oP '\d+\.\d+ (B|KiB|MiB|GiB) received' | awk '{print $1 " " $2}')
sent=$(echo "$line" | grep -oP '\d+\.\d+ (B|KiB|MiB|GiB) sent' | awk '{print $1 " " $2}')
tooltip+="\n $received $sent"
else
# For other lines
if [ "$first" = false ]; then
tooltip+="\n"
fi
key=$(echo "$line" | sed "s/ //" | awk -F ': ' '{print $1}')
value=$(echo "$line" | awk -F ': ' '{print $2}')
tooltip+="$key: $value"
first=false
fi
done
echo "$tooltip"
}
# Read wg show output and format
wg_show_output=$(sudo wg show)
if [[ -z $wg_show_output ]]; then
echo "{ \"text\": \"---\", \"tooltip\": \"disconnected\", \"class\": \"\", \"percentage\": 0 }"
else
tooltip=$(echo "$wg_show_output" | format)
echo "{ \"text\": \"$(echo $wg_show_output | grep -o 'wg[0-9]*')\", \"tooltip\": \"$tooltip\", \"class\": \"\", \"percentage\": 0 }"
fi