cron.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. #
  3. # Measurement script for the dn42 peer finder, see http://peerfinder.polynome.dn42
  4. # Dependencies: curl, sed, ping
  5. #
  6. # This script is designed to be run in cron every minute, like this:
  7. #
  8. # * * * * * /home/foo/cron.sh
  9. #
  10. # If you also want to provide IPv6 measurements, run it a second time with:
  11. #
  12. # * * * * * /home/foo/cron.sh ipv6
  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. LOGFILE='/dev/null' # Set to /dev/null to only receive errors.
  18. # Set to /dev/stdout to receive mail for each ping.
  19. # Set to a file writable by the cron runner to record pings.
  20. # (Errors will be sent in cron mail)
  21. # This avoids synchronisation (everybody fetching jobs and running
  22. # measurements simultaneously)
  23. RANDOM_DELAY=30
  24. [ -e $LOGFILE ] || touch $LOGFILE
  25. [ -z "$1" ] && FAMILY="ipv4" || FAMILY="ipv6"
  26. [ "$FAMILY" = "ipv4" ] && PING="ping -n -q" || PING="ping6 -n -q"
  27. SLEEP=$((RANDOM % RANDOM_DELAY))
  28. sleep "$SLEEP"
  29. curl -s "$PEERFINDER"/target/"$UUID"/"$FAMILY" > /tmp/"$FAMILY"
  30. read -r id ip < /tmp/"$FAMILY"
  31. # Avoid empty fields
  32. [ -z "$id" -o -z "$ip" ] && exit
  33. # Make sure "id" is an integer
  34. printf "%d" "$id" > /dev/null 2>&1 || exit
  35. # Parsing ping output, for Linux
  36. if ! output="$($PING -c "$NB_PINGS" -- "$ip" | grep -A1 "packets transmitted")"
  37. then
  38. sent=0
  39. received=0
  40. args="avgrtt=NaN"
  41. echo "Target $id ($ip) is unreachable"
  42. else
  43. echo $output | sed -e 's#^\([0-9]*\) packets transmitted, \([0-9]*\) received.*#\1\t\2#' > /tmp/"$FAMILY"_tmp1
  44. read sent received < /tmp/"$FAMILY"_tmp1
  45. if [ "$received" -eq 0 ]
  46. then
  47. args="avgrtt=NaN"
  48. echo "Target $id ($ip) is unreachable"
  49. else
  50. echo $output | sed -e 's#.*rtt min/avg/max/mdev = \(.*\)/\(.*\)/\(.*\)/\(.*\) ms$#\1\t\2\t\3\t\4#' > /tmp/"$FAMILY"_tmp2
  51. read minrtt avgrtt maxrtt jitter < /tmp/"$FAMILY"_tmp2
  52. [ -z "$avgrtt" ] && exit
  53. echo "RTT to target $id ($ip) is $avgrtt" >> $LOGFILE
  54. args="minrtt=${minrtt}&avgrtt=${avgrtt}&maxrtt=${maxrtt}&jitter=${jitter}"
  55. fi
  56. fi
  57. # Report results back to peerfinder
  58. curl -s -d "target=${id}&probes_sent=${sent}&probes_received=${received}&${args}" "$PEERFINDER"/result/report/"$UUID" >> $LOGFILE