run.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. #
  3. # Measurement script for the dn42 peer finder, see http://peerfinder.polynome.dn42
  4. # Dependencies: curl, sed, ping
  5. #
  6. # Run without argument for IPv4-only, and with an argument for IPv6-only, e.g.
  7. #
  8. # ./script.sh &
  9. # ./script.sh ipv6 &
  10. #
  11. # The script will run in a loop forever, periodically fetching targets and
  12. # reporting results back to the peerfinder.
  13. # Put your UUID here, and keep it secret!
  14. UUID="{{ uuid|default('00000000-0000-0000-0000-000000000000') }}"
  15. PEERFINDER="{{ peerfinder }}"
  16. NB_PINGS=5
  17. DELAY=30
  18. # This avoids synchronisation (everybody fetching jobs and running
  19. # measurements simultaneously)
  20. RANDOM_DELAY=30
  21. [ -z "$1" ] && FAMILY="ipv4" || FAMILY="ipv6"
  22. [ "$FAMILY" = "ipv4" ] && PING="ping -n -q" || PING="ping6 -n -q"
  23. while :
  24. do
  25. SLEEP=$((DELAY + RANDOM % RANDOM_DELAY))
  26. sleep "$SLEEP"
  27. curl -s "$PEERFINDER"/target/"$UUID"/"$FAMILY" > /tmp/"$FAMILY"
  28. read -r id ip < /tmp/"$FAMILY"
  29. # Avoid empty fields
  30. [ -z "$id" -o -z "$ip" ] && continue
  31. # Make sure "id" is an integer
  32. printf "%d" "$id" > /dev/null 2>&1 || continue
  33. # Parsing ping output, for Linux
  34. output="$($PING -c "$NB_PINGS" -- "$ip" | grep -A1 "packets transmitted")"
  35. echo $output | sed -e 's#^\([0-9]*\) packets transmitted, \([0-9]*\) received.*#\1\t\2#' > /tmp/"$FAMILY"_tmp1
  36. read sent received < /tmp/"$FAMILY"_tmp1
  37. if [ "$received" -eq 0 ]
  38. then
  39. args="avgrtt=NaN"
  40. echo "Target $id ($ip) is unreachable"
  41. else
  42. echo $output | sed -e 's#.*rtt min/avg/max/mdev = \(.*\)/\(.*\)/\(.*\)/\(.*\) ms$#\1\t\2\t\3\t\4#' > /tmp/"$FAMILY"_tmp2
  43. read minrtt avgrtt maxrtt jitter < /tmp/"$FAMILY"_tmp2
  44. [ -z "$avgrtt" ] && continue
  45. echo "RTT to target $id ($ip) is $avgrtt"
  46. args="minrtt=${minrtt}&avgrtt=${avgrtt}&maxrtt=${maxrtt}&jitter=${jitter}"
  47. fi
  48. curl -s -d "target=${id}&probes_sent=${sent}&probes_received=${received}&${args}" "$PEERFINDER"/result/report/"$UUID"
  49. done