docker-entrypoint.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env bash
  2. #
  3. # This is a rather minimal example Argbash potential
  4. # Example taken from http://argbash.readthedocs.io/en/stable/example.html
  5. #
  6. # ARG_OPTIONAL_SINGLE([speedtest],[s],[optional argument extra arguments to speedtest])
  7. # ARG_OPTIONAL_BOOLEAN([debug],[d],[boolean optional argument activate debug mode])
  8. # ARG_HELP([The general script's help msg])
  9. # ARGBASH_GO()
  10. # needed because of Argbash --> m4_ignore([
  11. ### START OF CODE GENERATED BY Argbash v2.9.0 one line above ###
  12. # Argbash is a bash code generator used to get arguments parsing right.
  13. # Argbash is FREE SOFTWARE, see https://argbash.io for more info
  14. # Generated online by https://argbash.io/generate
  15. # # When called, the process ends.
  16. # Args:
  17. # $1: The exit message (print to stderr)
  18. # $2: The exit code (default is 1)
  19. # if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1)
  20. # Example:
  21. # test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4
  22. die()
  23. {
  24. local _ret="${2:-1}"
  25. test "${_PRINT_HELP:-no}" = yes && print_help >&2
  26. echo "$1" >&2
  27. exit "${_ret}"
  28. }
  29. # Function that evaluates whether a value passed to it begins by a character
  30. # that is a short option of an argument the script knows about.
  31. # This is required in order to support getopts-like short options grouping.
  32. begins_with_short_option()
  33. {
  34. local first_option all_short_options='sdh'
  35. first_option="${1:0:1}"
  36. test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
  37. }
  38. # THE DEFAULTS INITIALIZATION - OPTIONALS
  39. _arg_speedtest=
  40. _arg_debug="off"
  41. # Function that prints general usage of the script.
  42. # This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments)
  43. # and it makes sense to remind the user how the script is supposed to be called.
  44. print_help()
  45. {
  46. printf '%s\n' "The general script's help msg"
  47. printf 'Usage: %s [-s|--speedtest <arg>] [-d|--(no-)debug] [-h|--help]\n' "$0"
  48. printf '\t%s\n' "-s, --speedtest: optional argument extra arguments to speedtest (no default)"
  49. printf '\t%s\n' "-d, --debug, --no-debug: boolean optional argument activate debug mode (off by default)"
  50. printf '\t%s\n' "-h, --help: Prints help"
  51. }
  52. # The parsing of the command-line
  53. parse_commandline()
  54. {
  55. while test $# -gt 0
  56. do
  57. _key="$1"
  58. case "$_key" in
  59. # We support whitespace as a delimiter between option argument and its value.
  60. # Therefore, we expect the --speedtest or -s value.
  61. # so we watch for --speedtest and -s.
  62. # Since we know that we got the long or short option,
  63. # we just reach out for the next argument to get the value.
  64. -s|--speedtest)
  65. test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
  66. _arg_speedtest="$2"
  67. shift
  68. ;;
  69. # We support the = as a delimiter between option argument and its value.
  70. # Therefore, we expect --speedtest=value, so we watch for --speedtest=*
  71. # For whatever we get, we strip '--speedtest=' using the ${var##--speedtest=} notation
  72. # to get the argument value
  73. --speedtest=*)
  74. _arg_speedtest="${_key##--speedtest=}"
  75. ;;
  76. # We support getopts-style short arguments grouping,
  77. # so as -s accepts value, we allow it to be appended to it, so we watch for -s*
  78. # and we strip the leading -s from the argument string using the ${var##-s} notation.
  79. -s*)
  80. _arg_speedtest="${_key##-s}"
  81. ;;
  82. # The debug argurment doesn't accept a value,
  83. # we expect the --debug or -d, so we watch for them.
  84. -d|--no-debug|--debug)
  85. _arg_debug="on"
  86. test "${1:0:5}" = "--no-" && _arg_debug="off"
  87. ;;
  88. # We support getopts-style short arguments clustering,
  89. # so as -d doesn't accept value, other short options may be appended to it, so we watch for -d*.
  90. # After stripping the leading -d from the argument, we have to make sure
  91. # that the first character that follows coresponds to a short option.
  92. -d*)
  93. _arg_debug="on"
  94. _next="${_key##-d}"
  95. if test -n "$_next" -a "$_next" != "$_key"
  96. then
  97. { begins_with_short_option "$_next" && shift && set -- "-d" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
  98. fi
  99. ;;
  100. # See the comment of option '--debug' to see what's going on here - principle is the same.
  101. -h|--help)
  102. print_help
  103. exit 0
  104. ;;
  105. # See the comment of option '-d' to see what's going on here - principle is the same.
  106. -h*)
  107. print_help
  108. exit 0
  109. ;;
  110. *)
  111. _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
  112. ;;
  113. esac
  114. shift
  115. done
  116. }
  117. # Now call all the functions defined above that are needed to get the job done
  118. parse_commandline "$@"
  119. # OTHER STUFF GENERATED BY Argbash
  120. ### END OF CODE GENERATED BY Argbash (sortof) ### ])
  121. # [ <-- needed because of Argbash
  122. [ "${_arg_debug}" == 'on' ] && _arg_debug=1 || _arg_debug=0
  123. [ ${_arg_debug} -eq 1 ] && set -x
  124. # InfluxDB variables
  125. influxdb_proto=${INFLUXDB_PROTO:-http}
  126. influxdb_host=${INFLUXDB_HOST:-influxdb}
  127. influxdb_port=${INFLUXDB_PORT:-8086}
  128. influxdb_db=${INFLUXDB_DB:-speedtest}
  129. influxdb_url="${influxdb_proto}://${influxdb_host}:${influxdb_port}"
  130. # run speedtest & store result
  131. json_result=$(speedtest -f json --accept-license --accept-gdpr ${_arg_speedtest})
  132. declare -A results
  133. # Extract data from speedtest result
  134. result_id=$(echo "${json_result}" | jq -r '.result.id')
  135. results['ping_latency']=$(echo "${json_result}" | jq -r '.ping.latency')
  136. results['download_bandwidth']=$(echo "${json_result}" | jq -r '.download.bandwidth')
  137. results['upload_bandwidth']=$(echo "${json_result}" | jq -r '.upload.bandwidth')
  138. results['packet_loss']=$(echo "${json_result}" | jq -r '.packetLoss')
  139. # Write metric to InfluxDB
  140. INFLUXDB_APPEND=''
  141. for key in "${!results[@]}"
  142. do
  143. value="${results[${key}]}"
  144. if [ "${value}" != 'null' ]; then
  145. if [ -z ${INFLUXDB_APPEND} ]; then
  146. INFLUXDB_APPEND="${key}=${value}"
  147. else
  148. INFLUXDB_APPEND="${INFLUXDB_APPEND},${key}=${value}"
  149. fi
  150. fi
  151. done
  152. INFLUXDB_APPEND="speedtest,result_id=${result_id} ${INFLUXDB_APPEND}"
  153. [ ${_arg_debug} -eq 1 ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}"
  154. # Ensure InfluxDB database exists
  155. curl \
  156. -d "q=CREATE DATABASE ${influxdb_db}" \
  157. "${influxdb_url}/query"
  158. curl \
  159. -d "${INFLUXDB_APPEND}" \
  160. "${influxdb_url}/write?db=${influxdb_db}"
  161. # ] <-- needed because of Argbash