fix: use ingest HTTP API instead of direct Postgres for VPS telemetry; cache speedtest results per interface
- hub/server.py: replace psycopg2 Postgres connection with urllib HTTP call to ingest /api/devices endpoint (fleet Postgres is Docker-internal, not reachable from the VPS directly) - telemetry-synology.sh: run_speedtest() now writes per-interface cache files (speedtest-result-<iface>); _cached_speedtest() reads cache if < 35 min old; _collect_wan() uses cached result so throughput shows in telemetry without blocking every 1-min cycle Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -286,8 +286,14 @@ _collect_wan() {
|
||||
_jitter=$(echo "$_ping_result" | awk '{print $2}')
|
||||
_loss=$(echo "$_ping_result" | awk '{print $3}')
|
||||
_score=$(_compute_score "$_lat" "$_loss")
|
||||
_dl="0.0"
|
||||
_ul="0.0"
|
||||
# Use cached speedtest result if available and fresh
|
||||
_st=$(_cached_speedtest "$_iface" 2>/dev/null)
|
||||
if [ -n "$_st" ]; then
|
||||
_dl=$(echo "$_st" | awk '{print $1}')
|
||||
_ul=$(echo "$_st" | awk '{print $2}')
|
||||
fi
|
||||
[ -z "$_dl" ] || [ "$_dl" = "0" ] && _dl="0.0"
|
||||
[ -z "$_ul" ] || [ "$_ul" = "0" ] && _ul="0.0"
|
||||
fi
|
||||
|
||||
printf '{"score":%s,"latency_ms":%s,"dl_mbps":%s,"ul_mbps":%s,"jitter_ms":%s,"loss_pct":%s}' \
|
||||
@@ -364,10 +370,10 @@ _eyeride_signal() {
|
||||
}
|
||||
|
||||
# ── Speedtest ──────────────────────────────────────────────────────────────
|
||||
# Run a speedtest bound to a specific interface. Reports dl_mbps ul_mbps.
|
||||
# Run a speedtest bound to a specific interface. Caches result per-interface.
|
||||
run_speedtest() {
|
||||
_iface="$1"
|
||||
_result="0 0"
|
||||
_result_file="${STATE_DIR}/speedtest-result-${_iface}"
|
||||
# Try speedtest-cli first
|
||||
if which speedtest-cli >/dev/null 2>&1; then
|
||||
_out=$(speedtest-cli --interface "$_iface" --json 2>/dev/null)
|
||||
@@ -377,27 +383,16 @@ run_speedtest() {
|
||||
if [ -n "$_dl" ] && [ -n "$_ul" ]; then
|
||||
_dl_mbps=$(awk "BEGIN { printf \"%.1f\", ${_dl} / 125000 }" 2>/dev/null)
|
||||
_ul_mbps=$(awk "BEGIN { printf \"%.1f\", ${_ul} / 125000 }" 2>/dev/null)
|
||||
printf '%s %s\n' "${_dl_mbps}" "${_ul_mbps}" > "$_result_file"
|
||||
echo "${_dl_mbps} ${_ul_mbps}"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
# Fallback: iperf3 single-stream test to public server
|
||||
if which iperf3 >/dev/null 2>&1; then
|
||||
_out=$(timeout 15 iperf3 -B "$_ip" -c iperf.he.net -p 5201 -J 2>/dev/null)
|
||||
if [ -n "$_out" ]; then
|
||||
_dl=$(_json_get_num "$_out" "bits_per_second")
|
||||
if [ -n "$_dl" ]; then
|
||||
_dl_mbps=$(awk "BEGIN { printf \"%.1f\", ${_dl} / 1000000 }" 2>/dev/null)
|
||||
echo "${_dl_mbps} 0"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
# Fallback: curl a known file and measure throughput
|
||||
_tmp="/tmp/speedtest-$$"
|
||||
_start=$(date +%s)
|
||||
if curl -s --interface "$_iface" --max-time 10 -o "$_tmp" http://speedtest.tele2.net/1MB.zip 2>/dev/null; then
|
||||
if curl -s --interface "$_iface" --max-time 15 -o "$_tmp" http://speedtest.tele2.net/10MB.zip 2>/dev/null; then
|
||||
_end=$(date +%s)
|
||||
_bytes=$(wc -c < "$_tmp" 2>/dev/null)
|
||||
rm -f "$_tmp"
|
||||
@@ -405,6 +400,7 @@ run_speedtest() {
|
||||
_duration=$(( _end - _start ))
|
||||
[ "$_duration" -lt 1 ] && _duration=1
|
||||
_dl_mbps=$(awk "BEGIN { printf \"%.1f\", ${_bytes} * 8 / ${_duration} / 1000000 }" 2>/dev/null)
|
||||
printf '%s 0\n' "${_dl_mbps:-0}" > "$_result_file"
|
||||
echo "${_dl_mbps:-0} 0"
|
||||
return 0
|
||||
fi
|
||||
@@ -413,6 +409,18 @@ run_speedtest() {
|
||||
echo "0 0"
|
||||
}
|
||||
|
||||
# Read cached speedtest result for an interface if < 35 minutes old.
|
||||
_cached_speedtest() {
|
||||
_iface="$1"
|
||||
_f="${STATE_DIR}/speedtest-result-${_iface}"
|
||||
[ -f "$_f" ] || return 1
|
||||
_mtime=$(stat -c %Y "$_f" 2>/dev/null || stat -f %m "$_f" 2>/dev/null)
|
||||
[ -n "$_mtime" ] || return 1
|
||||
_age=$(( $(date +%s) - _mtime ))
|
||||
[ "$_age" -lt 2100 ] || return 1
|
||||
cat "$_f"
|
||||
}
|
||||
|
||||
# ── Telemetry Collection ───────────────────────────────────────────────────
|
||||
telemetry_collect() {
|
||||
now=$(date +%s)
|
||||
|
||||
Reference in New Issue
Block a user