run.sh 1.8 KB

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