Commit 574b5705 authored by Vitaly Lipatov's avatar Vitaly Lipatov

router/scripts: add iperf3 bandwidth measurement for Telegraf

parent 2e9b341e
#!/bin/bash
# Deploy iperf3 bandwidth measurement to a gateway container
# Usage: deploy-iperf3.sh TARGET_IP CTID
#
# TARGET_IP should be the dummy interface IP on the VDS (10.77.0.1)
# so that traffic goes through the tunnel, not directly to VDS public IP.
#
# VDS dummy IPs:
# 10.77.0.1 — common (on all VDS)
# 10.77.1.1 — hetzner, 10.77.2.1 — vdska, 10.77.3.1 — beget
#
# This script:
# 1. Copies iperf3-telegraf.sh to the container
# 2. Adds [[inputs.exec]] to telegraf.conf
# 3. Restarts telegraf
#
# Prerequisites:
# - iperf3 must be installed in the container
# - iperf3 server must be running on VDS (port 5201)
# - dummy0 interface must exist on VDS
# - Run from the PVE host (border)
set -e
TARGET_IP="${1:-10.77.0.1}"
CTID="$2"
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
SCRIPT_SRC="$SCRIPT_DIR/iperf3-telegraf.sh"
SCRIPT_DST="/usr/local/bin/iperf3-telegraf.sh"
TELEGRAF_CONF="/etc/telegraf/telegraf.conf"
if [ -z "$CTID" ]; then
echo "Usage: $0 [TARGET_IP] CTID" >&2
echo "Example: $0 10.77.0.1 668" >&2
echo "Default TARGET_IP: 10.77.0.1 (dummy0 on all VDS)" >&2
exit 1
fi
echo "=== Deploying iperf3 measurement to CT $CTID (target: $TARGET_IP) ==="
# Check container is running
if ! pct status "$CTID" 2>/dev/null | grep -q running; then
echo "ERROR: CT $CTID is not running" >&2
exit 1
fi
# Copy script
echo "Copying iperf3-telegraf.sh..."
pct push "$CTID" "$SCRIPT_SRC" "$SCRIPT_DST"
lxc-attach -n "$CTID" -- chmod +x "$SCRIPT_DST"
# Check if iperf3 is installed
if ! lxc-attach -n "$CTID" -- which iperf3 >/dev/null 2>&1; then
echo "Installing iperf3..."
lxc-attach -n "$CTID" -- epm -y install iperf3
fi
# Add telegraf config if not already present
if lxc-attach -n "$CTID" -- grep -q "iperf3-telegraf" "$TELEGRAF_CONF" 2>/dev/null; then
echo "Telegraf config already contains iperf3 — skipping"
else
echo "Adding iperf3 to telegraf.conf..."
lxc-attach -n "$CTID" -- bash -c "cat >> $TELEGRAF_CONF" <<EOF
[[inputs.exec]]
commands = ["/usr/local/bin/iperf3-telegraf.sh $TARGET_IP"]
interval = "30m"
timeout = "300s"
data_format = "influx"
EOF
fi
# Restart telegraf
echo "Restarting telegraf..."
lxc-attach -n "$CTID" -- systemctl restart telegraf
echo "=== Done: CT $CTID configured for iperf3 to $TARGET_IP ==="
#!/bin/bash
# iperf3 bandwidth measurement for telegraf
# Outputs data in InfluxDB line protocol format
# Usage: iperf3-telegraf.sh SERVER_IP [OFFSET]
#
# OFFSET (seconds) staggers tests so only one client
# hits each iperf3 server at a time.
SERVER="$1"
OFFSET="${2:-0}"
DURATION=5
if [ -z "$SERVER" ]; then
echo "Usage: $0 SERVER_IP [OFFSET]" >&2
exit 1
fi
sleep "$OFFSET"
result=$(iperf3 -c "$SERVER" -t "$DURATION" --json 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$result" ]; then
echo "iperf3,server=$SERVER success=0i,sent_bps=0,recv_bps=0,retransmits=0i"
exit 0
fi
python3 -c "
import json,sys
d=json.load(sys.stdin)
e=d.get('end',{})
s=e.get('sum_sent',{})
r=e.get('sum_received',{})
print(f'iperf3,server=$SERVER success=1i,sent_bps={s.get(\"bits_per_second\",0)},recv_bps={r.get(\"bits_per_second\",0)},retransmits={s.get(\"retransmits\",0)}i')
" <<< "$result"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment