-
Notifications
You must be signed in to change notification settings - Fork 2
/
speedtest
51 lines (42 loc) · 1.52 KB
/
speedtest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /bin/bash
# Speedtest for Check_mk or nagios
#
# Install speedtest-cli pkg
#
# (c) 2018, Edoardo Marchetti <marchetti.edoardo@gmail.com>
# https://github.com/jas31085/CheckMK-plugin
#
# -----------------------------------------------------------------------
# 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")"
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]}
DOWNLOAD=${VALUES[6]}
UPLOAD=${VALUES[7]}
DOWNLOAD="$(numfmt --to=iec-i --suffix=B <<< $DOWNLOAD)"
UPLOAD="$(numfmt --to=iec-i --suffix=B <<< $UPLOAD)"
echo "<<<local>>>"
echo "0 speedtest ping=$PING;;|upload=$UPLOAD;;|download=$DOWNLOAD;; ping $PING upload $UPLOAD download $DOWNLOAD "
fi
rm -rf "$LOGFILE"