diff --git a/speedtest b/speedtest old mode 100644 new mode 100755 index be3736f..97e7d4b --- a/speedtest +++ b/speedtest @@ -9,23 +9,47 @@ # # ----------------------------------------------------------------------- -# ----------------------------------------------------------------------- +# Modified by: Mike Hanby +# Email: mhanby@uab.edu +# +# The 'speedtest' command comes from https://www.speedtest.net/apps/cli +# Modifications from the original are as follows: +# I didn't want to run the 'speedtest-cli' Python script as root, so I +# modified it to run as a user named 'cmkplugin' (which has to be +# manually created on the host). The user doesn't need any special privileges. +# +# If the script is run as a user other than root, it will continue to run +# as that user (for testing from the command line) +# ----------------------------------------------------------------------- -if type speedtest-cli > /dev/null 2>&1 ; then +if type speedtest > /dev/null 2>&1 ; then LOGFILE="$(mktemp "/tmp/speedtest.XXXXXXXX")" - speedtest-cli --csv > "$LOGFILE" - CSV=$(cat "$LOGFILE") - IFS=, VALUES=($CSV) - PING=${VALUES[5]} - DOWNLOAD=${VALUES[6]} - UPLOAD=${VALUES[7]} - - DOWNLOAD="$(numfmt --to=iec-i --suffix=B <<< $DOWNLOAD)" - UPLOAD="$(numfmt --to=iec-i --suffix=B <<< $UPLOAD)" + + if [ "$EUID" -ne 0 ]; then + if [ ! -f "~/.config/ookla/speedtest-cli.json" ]; then + /usr/bin/speedtest --format=tsv --accept-license > /dev/null 2>&1 + fi + /usr/bin/speedtest --format=tsv -a --progress=no > $LOGFILE + else + if [ ! -f "~cmkplugin/.config/ookla/speedtest-cli.json" ]; then + su - cmkplugin -c "/usr/bin/speedtest --format=tsv --accept-license" > /dev/null 2>&1 + fi + chown cmkplugin $LOGFILE + chmod 664 $LOGFILE + #su - cmkplugin -c "/usr/bin/speedtest-cli --csv --csv-delimiter '|' > $LOGFILE" + su - cmkplugin -c "/usr/bin/speedtest --format=tsv --progress=no > $LOGFILE" + fi + + TSV=$(cat "$LOGFILE") + IFS=$'\t' VALUES=($TSV) + PING=${VALUES[2]} + + DOWNLOAD=$(echo "scale=2; ${VALUES[5]}/1000/1000*8" | bc -l) + UPLOAD=$(echo "scale=2; ${VALUES[6]}/1000/1000*8" | bc -l) echo "<<>>" - echo "0 SPEEDTEST ping=$PING;;|upload=$UPLOAD;;|download=$DOWNLOAD;; ping $PING upload $UPLOAD download $DOWNLOAD " + echo "0 speedtest ping=$PING;;|upload=${UPLOAD}Mbps;;|download=${DOWNLOAD}Mbps;; ping $PING upload ${UPLOAD}Mbps download ${DOWNLOAD}Mbps " fi -rm -rf "$LOGFILE" \ No newline at end of file +rm -rf "$LOGFILE"