Speedtest-rrd

Iš Žinynas.
Jump to navigation Jump to search

Panaudojimas: Tinklo greičio pasiremiant speedtest.net matavimas kas 30min ir loginimas/atvaizdavimas.

Screenshot 2025-01-13 at 00.24.34.png


Sistemoje turėtų būti jau sudiegtas rrdtool ir speedtest-cli.

Padarome RRD duombazę:

rrdtool create speedtest.rrd \
  --step 1800 \
  DS:download:GAUGE:3600:0:1000 \
  DS:upload:GAUGE:3600:0:1000 \
  DS:ping:GAUGE:3600:0:500 \
  RRA:AVERAGE:0.5:1:48 \
  RRA:AVERAGE:0.5:6:336 \
  RRA:AVERAGE:0.5:48:365

Duombazės atnaujinimui panaudojame šį skriptą:

#!/bin/bash
RRD=speedtest.rrd
MAX_RETRIES=3
RETRY_DELAY=10
timestamp=$(date +"%Y-%m-%d %H:%M:%S")

run_speedtest() {
    local attempt=1
    while [ $attempt -le $MAX_RETRIES ]; do
        result=$(speedtest --secure --server 1437 --json 2>/dev/null)
        exit_code=$?
        if [ $exit_code -eq 0 ] && [ -n "$result" ]; then
            echo "$result"
            return 0
        fi
        sleep $RETRY_DELAY
        attempt=$((attempt + 1))
    done
    return 1
}

result=$(run_speedtest)
if [ $? -ne 0 ]; then
    echo "[$timestamp] Data retrieval !!!failed!!! -> $result"
    download="NaN"
    upload="NaN"
    ping="NaN"
else
download=$(echo "$result" | jq '.download' | awk '{print int($1 / 1250000) + 0.0 }')
upload=$(echo "$result" | jq '.upload' | awk '{print int($1 / 1250000) + 0.0 }')
ping=$(echo "$result" | jq '.ping' | awk '{print int($1 + 0.5) }')
echo "[$timestamp] Data retrieval !!!ok!!! -> Download: $download Upload: $upload Latency: $ping"
fi

rrdtool update $RRD N:$download:$upload:$ping

Grafiko piešimui panaudojame šį skriptą:

#!/bin/bash

RRD=speedtest.rrd
GRAPH_PATH=/var/www/html/speed_test

rrdtool graph ${GRAPH_PATH}_24h.png \
--width 800 --height 400 \
--start end-24h \
--end now \
--title "Internet speed test (Last 24h)" \
--vertical-label "Speed Mbps" \
--font DEFAULT:12:Courier \
--font TITLE:18:Courier \
--color CANVAS#121212 \
--color BACK#0a0a0a \
--color SHADEA#0a0a0a \
--color SHADEB#0a0a0a \
--color FONT#e0e0e0 \
--color AXIS#aaaaaa \
--color GRID#333333 \
DEF:download=${RRD}:download:AVERAGE \
DEF:upload=${RRD}:upload:AVERAGE \
DEF:ping=${RRD}:ping:AVERAGE \
AREA:download#00FF0080: \
LINE2:download#00FF00:"Download" \
AREA:upload#FF450080: \
LINE2:upload#FF4500:"Upload" \
AREA:ping#B0B0B080: \
LINE2:ping#B0B0B0:"Ping" \
COMMENT:"\\n" \
GPRINT:download:LAST:"Last Download\: %6.0lf (Mbps)" \
GPRINT:upload:LAST:"Last Upload\: %6.0lf (Mbps)" \
GPRINT:ping:LAST:"Last Ping\: %6.0lf (ms)\\n"

Uždedam ant cron

echo "*/30 *  * * *   root    /path_to/update >> /path_to/cron.log 2>&1" >> /etc/crontab
echo "0    *  * * *   root    /path_to/graph > /dev/null 2>&1" >> /etc/crontab