#!/bin/bash # Network latency monitoring - pings the commit server and records latency # Managed by Ansible - do not edit manually METRICS_DIR="{{ p4prometheus_metrics_dir }}" METRICS_FILE="${METRICS_DIR}/p4_network_latency.prom" TMP_FILE="${METRICS_FILE}.$$" TARGET="{{ commit_dns }}" # Send 3 pings with 1 second timeout ping_output=$(ping -c 3 -W 1 "$TARGET" 2>&1) ping_rc=$? if [ $ping_rc -eq 0 ]; then target_reachable=1 # Extract avg latency from "min/avg/max/mdev = x/x/x/x ms" avg_ms=$(echo "$ping_output" | tail -1 | sed -E 's|.*/([0-9.]+)/.*|\1|') packet_loss=$(echo "$ping_output" | grep -oP '[0-9.]+(?=% packet loss)') else target_reachable=0 avg_ms=0 packet_loss=100 fi cat > "$TMP_FILE" <