feat: add device traffic metrics with gateway telemetry reporting and admin UI display
Add rx_bytes and tx_bytes fields to Device type and API responses. Add formatDataSize helper for human-readable byte formatting with units from B to TB. Add Received and Sent columns to devices table in admin UI with formatted traffic totals. Add traffic metrics display to device action panel. Add TelemetrySnapshot and PeerTelemetry types for gateway runtime stats. Add gateway telemetry endpoint at POST /gateway
This commit is contained in:
@@ -144,6 +144,54 @@ EOF
|
||||
echo "Applied WireGuard config from ${WG_CONF}"
|
||||
echo "Applied nftables config from ${NFT_CONF}"
|
||||
wg show "${IFACE}" latest-handshakes transfer 2>/dev/null || true
|
||||
post_telemetry || true
|
||||
}
|
||||
|
||||
post_telemetry() {
|
||||
if [ ! -f "${STATE_JSON}" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
TELEMETRY_URL="${SYNC_BASE_URL}/${NEXAVPN_GATEWAY_ID}/telemetry"
|
||||
TMP_TELEMETRY_JSON="/tmp/nexavpn-gateway-telemetry.json"
|
||||
|
||||
{
|
||||
printf '{\"collected_at\":\"%s\",\"peers\":[' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
|
||||
FIRST=1
|
||||
|
||||
while IFS="$(printf '\t')" read -r PUBLIC_KEY _PRESHARED _ENDPOINT _ALLOWED_IPS LATEST_HANDSHAKE RX_BYTES TX_BYTES _KEEPALIVE; do
|
||||
if [ -z "${PUBLIC_KEY:-}" ] || [ "${PUBLIC_KEY}" = "private_key" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
DEVICE_ID="$(jq -r --arg public_key "${PUBLIC_KEY}" '.peers[]? | select(.public_key == $public_key) | .device_id' "${STATE_JSON}" | head -n1)"
|
||||
if [ -z "${DEVICE_ID:-}" ] || [ "${DEVICE_ID}" = "null" ]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ "${FIRST}" -eq 0 ]; then
|
||||
printf ','
|
||||
fi
|
||||
FIRST=0
|
||||
|
||||
if [ "${LATEST_HANDSHAKE:-0}" -gt 0 ] 2>/dev/null; then
|
||||
printf '{\"device_id\":\"%s\",\"public_key\":\"%s\",\"rx_bytes\":%s,\"tx_bytes\":%s,\"latest_handshake_at\":%s}' \
|
||||
"${DEVICE_ID}" "${PUBLIC_KEY}" "${RX_BYTES:-0}" "${TX_BYTES:-0}" "${LATEST_HANDSHAKE}"
|
||||
else
|
||||
printf '{\"device_id\":\"%s\",\"public_key\":\"%s\",\"rx_bytes\":%s,\"tx_bytes\":%s}' \
|
||||
"${DEVICE_ID}" "${PUBLIC_KEY}" "${RX_BYTES:-0}" "${TX_BYTES:-0}"
|
||||
fi
|
||||
done < <(wg show "${IFACE}" dump 2>/dev/null | tail -n +2)
|
||||
|
||||
printf ']}'
|
||||
} > "${TMP_TELEMETRY_JSON}"
|
||||
|
||||
curl -fsSL \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "X-Gateway-Bootstrap-Token: ${GATEWAY_BOOTSTRAP_TOKEN}" \
|
||||
-X POST \
|
||||
--data @"${TMP_TELEMETRY_JSON}" \
|
||||
"${TELEMETRY_URL}" >/dev/null
|
||||
}
|
||||
|
||||
while true; do
|
||||
|
||||
Reference in New Issue
Block a user