Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions speedtest
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<<<local>>>"
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"
rm -rf "$LOGFILE"