From 7c273c3ab3510718e36386a897d8f0076c49b0e8 Mon Sep 17 00:00:00 2001 From: Mike Hanby Date: Wed, 27 Nov 2019 21:29:20 -0500 Subject: [PATCH 1/2] Updated speedtest to allow it to run as non-root user when run by cmk 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) Changes to be committed: - modified: speedtest --- speedtest | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/speedtest b/speedtest index be3736f..0b58a5f 100644 --- a/speedtest +++ b/speedtest @@ -9,12 +9,32 @@ # # ----------------------------------------------------------------------- +# Modified by: Mike Hanby +# Email: mhanby@uab.edu +# +# The 'speedtest-cli' command comes from https://github.com/sivel/speedtest-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 LOGFILE="$(mktemp "/tmp/speedtest.XXXXXXXX")" - speedtest-cli --csv > "$LOGFILE" + + if [ "$EUID" -ne 0 ]; then + speedtest-cli --csv > "$LOGFILE" + else + chown cmkplugin $LOGFILE + chmod 660 $LOGFILE + su - cmkplugin -c "/usr/bin/speedtest-cli --csv > $LOGFILE" + fi + CSV=$(cat "$LOGFILE") IFS=, VALUES=($CSV) PING=${VALUES[5]} @@ -25,7 +45,7 @@ if type speedtest-cli > /dev/null 2>&1 ; then UPLOAD="$(numfmt --to=iec-i --suffix=B <<< $UPLOAD)" echo "<<>>" - echo "0 SPEEDTEST ping=$PING;;|upload=$UPLOAD;;|download=$DOWNLOAD;; ping $PING upload $UPLOAD download $DOWNLOAD " + echo "0 speedtest ping=$PING;;|upload=$UPLOAD;;|download=$DOWNLOAD;; ping $PING upload $UPLOAD download $DOWNLOAD " fi -rm -rf "$LOGFILE" \ No newline at end of file +rm -rf "$LOGFILE" From d23d5ca4eeed51108169a7170e4475deee178859 Mon Sep 17 00:00:00 2001 From: Mike Hanby Date: Thu, 28 Nov 2019 00:46:13 -0500 Subject: [PATCH 2/2] Changed speedtest to use speedtest.net CLI Changes to be committed: - modified: speedtest --- speedtest | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) mode change 100644 => 100755 speedtest diff --git a/speedtest b/speedtest old mode 100644 new mode 100755 index 0b58a5f..97e7d4b --- a/speedtest +++ b/speedtest @@ -12,8 +12,8 @@ # Modified by: Mike Hanby # Email: mhanby@uab.edu # -# The 'speedtest-cli' command comes from https://github.com/sivel/speedtest-cli -# +# 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 @@ -23,29 +23,33 @@ # 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")" if [ "$EUID" -ne 0 ]; then - speedtest-cli --csv > "$LOGFILE" + 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 660 $LOGFILE - su - cmkplugin -c "/usr/bin/speedtest-cli --csv > $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 - 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)" + 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"