#!/bin/bash
# Ahel WAN speedtest -> M1 pushgateway (migrated from Pi systemd timer, Phase 1)
PG="http://127.0.0.1:9091/metrics/job/speedtest"
R=$(/opt/homebrew/bin/librespeed-cli --json 2>/dev/null) || { printf 'speedtest_up 0\n' | curl -s --data-binary @- "$PG"; exit 0; }
read DL UL P < <(echo "$R" | python3 -c "import json,sys; d=json.load(sys.stdin)[0]; print(d['download'], d['upload'], d['ping'])")
printf 'speedtest_download_bits_per_second %f\nspeedtest_upload_bits_per_second %f\nspeedtest_ping_latency_milliseconds %s\nspeedtest_up 1\n' \
  "$(python3 -c "print($DL*1e6)")" "$(python3 -c "print($UL*1e6)")" "$P" | curl -s --data-binary @- "$PG"
echo "pushed DL=${DL} UL=${UL} ping=${P}"
