Files
tailscale-tools/exitnode-bw.sh
alf ff370b054b Add exitnode-bw.sh: Tailscale Exit-Node Bandbreiten-Monitor
- Zeigt Up/Download-Bandbreite ausgewählter Geräte
- Balkendiagramm mit gum
- Partial-Match für Gerätenamen (case-insensitive)
- Mindest-Balkenlänge bei Aktivität
- Online/Offline-Status

Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-10-28 12:43:50 +01:00

85 lines
2.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# exitnode-bw.sh – Exit-Node-Bandbreite für festgelegte Geräte
# Benötigt: tailscale, jq, awk, gum
#
# Zeigt die Up-/Download-Bandbreite ausgewählter Geräte an,
# die über einen Tailscale Exit-Node verbunden sind.
INTERVAL=5
BARWIDTH=40
MIN_BAR=1 # Mindest-Balkenlänge bei Aktivität (>0)
DEVICES=("PC7-77" "s22") # Partial-Match: "s22" findet "s22-von-alf"
A=$(mktemp); B=$(mktemp)
trap 'rm -f "$A" "$B"' EXIT
# Erstelle Pipe-separierte Liste für partial match
DEVLIST=$(printf '%s|' "${DEVICES[@]}")
DEVLIST=${DEVLIST%|} # trailing | entfernen
snap() {
tailscale status --json | jq -r '.Peer[] | "\(.HostName) \(.TxBytes) \(.RxBytes) \(.Online)"'
}
snap > "$A"
while sleep "$INTERVAL"; do
snap > "$B"
clear
gum style --bold --foreground 212 "⇅ Exit-Node Traffic $(date +%H:%M:%S)"
echo ""
join "$A" "$B" | awk -v iv="$INTERVAL" -v w="$BARWIDTH" -v minbar="$MIN_BAR" \
-v devs="$DEVLIST" -v IGNORECASE=1 '
function mbit(kbits) {
return sprintf("%6.2f Mbit/s", kbits/1e3)
}
function bar(rate, max, s, n, i) {
if (rate <= 0) return ""
n = (max > 0) ? int(rate / max * w) : w
if (n < minbar) n = minbar # Mindest-Balken bei Aktivität
if (n > w) n = w
for (i = 0; i < n; i++) s = s "█"
return s
}
# Partial match: prüfe ob Hostname eines der Geräte enthält
function matches(host) {
n = split(devs, arr, "|")
for (i = 1; i <= n; i++) {
if (index(tolower(host), tolower(arr[i])) > 0) return 1
}
return 0
}
matches($1) {
dt = ($4 > $2) ? $4 - $2 : 0
dr = ($5 > $3) ? $5 - $3 : 0
online = ($6 == "true") ? 1 : 0
n++
host[n] = $1
u[n] = dt * 8 / iv / 1e3
d[n] = dr * 8 / iv / 1e3
ol[n] = online
if (u[n] > mx) mx = u[n]
if (d[n] > mx) mx = d[n]
if (mx == 0) mx = 1 # Verhindere Division durch 0
}
END {
if (n == 0) {
print "\033[90m (keines der Geräte gefunden)\033[0m"
print "\033[90m Geprüfte Namen: " devs "\033[0m"
}
for (i = 1; i <= n; i++) {
ubar = bar(u[i], mx)
dbar = bar(d[i], mx)
status = ol[i] ? "\033[32m●\033[0m" : "\033[90m○\033[0m"
printf "%s %-18s \033[36m↑\033[0m %s%-*s\033[0m %s \033[32m↓\033[0m %s%-*s\033[0m %s\n",
status,
host[i],
"\033[36m", w-length(ubar), ubar, mbit(u[i]),
"\033[32m", w-length(dbar), dbar, mbit(d[i])
}
}'
echo ""
gum style --foreground 240 " Interval: ${INTERVAL}s | Geräte: ${DEVICES[*]} | Strg+C zum Beenden"
mv "$B" "$A"
done