#!/usr/bin/env bash # # This is a rather minimal example Argbash potential # Example taken from http://argbash.readthedocs.io/en/stable/example.html # # ARG_OPTIONAL_SINGLE([speedtest],[s],[optional argument extra arguments to speedtest]) # ARG_OPTIONAL_BOOLEAN([debug],[d],[boolean optional argument activate debug mode]) # ARG_HELP([The general script's help msg]) # ARGBASH_GO() # needed because of Argbash --> m4_ignore([ ### START OF CODE GENERATED BY Argbash v2.9.0 one line above ### # Argbash is a bash code generator used to get arguments parsing right. # Argbash is FREE SOFTWARE, see https://argbash.io for more info # Generated online by https://argbash.io/generate # # When called, the process ends. # Args: # $1: The exit message (print to stderr) # $2: The exit code (default is 1) # if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1) # Example: # test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4 die() { local _ret="${2:-1}" test "${_PRINT_HELP:-no}" = yes && print_help >&2 echo "$1" >&2 exit "${_ret}" } # Function that evaluates whether a value passed to it begins by a character # that is a short option of an argument the script knows about. # This is required in order to support getopts-like short options grouping. begins_with_short_option() { local first_option all_short_options='sdh' first_option="${1:0:1}" test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0 } # THE DEFAULTS INITIALIZATION - OPTIONALS _arg_speedtest= _arg_debug="off" # Function that prints general usage of the script. # This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments) # and it makes sense to remind the user how the script is supposed to be called. print_help() { printf '%s\n' "The general script's help msg" printf 'Usage: %s [-s|--speedtest ] [-d|--(no-)debug] [-h|--help]\n' "$0" printf '\t%s\n' "-s, --speedtest: optional argument extra arguments to speedtest (no default)" printf '\t%s\n' "-d, --debug, --no-debug: boolean optional argument activate debug mode (off by default)" printf '\t%s\n' "-h, --help: Prints help" } # The parsing of the command-line parse_commandline() { while test $# -gt 0 do _key="$1" case "$_key" in # We support whitespace as a delimiter between option argument and its value. # Therefore, we expect the --speedtest or -s value. # so we watch for --speedtest and -s. # Since we know that we got the long or short option, # we just reach out for the next argument to get the value. -s|--speedtest) test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_speedtest="$2" shift ;; # We support the = as a delimiter between option argument and its value. # Therefore, we expect --speedtest=value, so we watch for --speedtest=* # For whatever we get, we strip '--speedtest=' using the ${var##--speedtest=} notation # to get the argument value --speedtest=*) _arg_speedtest="${_key##--speedtest=}" ;; # We support getopts-style short arguments grouping, # so as -s accepts value, we allow it to be appended to it, so we watch for -s* # and we strip the leading -s from the argument string using the ${var##-s} notation. -s*) _arg_speedtest="${_key##-s}" ;; # The debug argurment doesn't accept a value, # we expect the --debug or -d, so we watch for them. -d|--no-debug|--debug) _arg_debug="on" test "${1:0:5}" = "--no-" && _arg_debug="off" ;; # We support getopts-style short arguments clustering, # so as -d doesn't accept value, other short options may be appended to it, so we watch for -d*. # After stripping the leading -d from the argument, we have to make sure # that the first character that follows coresponds to a short option. -d*) _arg_debug="on" _next="${_key##-d}" if test -n "$_next" -a "$_next" != "$_key" then { 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." fi ;; # See the comment of option '--debug' to see what's going on here - principle is the same. -h|--help) print_help exit 0 ;; # See the comment of option '-d' to see what's going on here - principle is the same. -h*) print_help exit 0 ;; *) _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1 ;; esac shift done } # Now call all the functions defined above that are needed to get the job done parse_commandline "$@" # OTHER STUFF GENERATED BY Argbash ### END OF CODE GENERATED BY Argbash (sortof) ### ]) # [ <-- needed because of Argbash [ "${_arg_debug}" == 'on' ] && _arg_debug=1 || _arg_debug=0 [ ${_arg_debug} -eq 1 ] && set -x # InfluxDB variables influxdb_proto=${INFLUXDB_PROTO:-http} influxdb_host=${INFLUXDB_HOST:-influxdb} influxdb_port=${INFLUXDB_PORT:-8086} influxdb_db=${INFLUXDB_DB:-speedtest} influxdb_url="${influxdb_proto}://${influxdb_host}:${influxdb_port}" # run speedtest & store result json_result=$(speedtest -f json --accept-license --accept-gdpr ${_arg_speedtest}) declare -A results # Extract data from speedtest result result_id=$(echo "${json_result}" | jq -r '.result.id') results['ping_latency']=$(echo "${json_result}" | jq -r '.ping.latency') results['download_bandwidth']=$(echo "${json_result}" | jq -r '.download.bandwidth') results['upload_bandwidth']=$(echo "${json_result}" | jq -r '.upload.bandwidth') results['packet_loss']=$(echo "${json_result}" | jq -r '.packetLoss') # Write metric to InfluxDB INFLUXDB_APPEND='' for key in "${!results[@]}" do value="${results[${key}]}" if [ "${value}" != 'null' ]; then if [ -z ${INFLUXDB_APPEND} ]; then INFLUXDB_APPEND="${key}=${value}" else INFLUXDB_APPEND="${INFLUXDB_APPEND},${key}=${value}" fi fi done INFLUXDB_APPEND="speedtest,result_id=${result_id} ${INFLUXDB_APPEND}" [ ${_arg_debug} -eq 1 ] && echo "[DEBUG] INFLUXDB: ${INFLUXDB_APPEND}" # Ensure InfluxDB database exists curl \ -d "q=CREATE DATABASE ${influxdb_db}" \ "${influxdb_url}/query" curl \ -d "${INFLUXDB_APPEND}" \ "${influxdb_url}/write?db=${influxdb_db}" # ] <-- needed because of Argbash