Internet Speedtest: Unterschied zwischen den Versionen

Aus wiki.frank-wulf.de
Zur Navigation springen Zur Suche springen
Zeile 5: Zeile 5:
#
#
# Author:  Frank Wulf
# Author:  Frank Wulf
# Version: 1.0 (2020-04-09)
# Version: 1.0 (2020-04-10)
#
#
# Version history:
# Version history:
# 1.0  2020-04-09   Initial release
# 1.0  2020-04-10   Initial release
#
#


Zeile 25: Zeile 25:
   download=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $7 }'`
   download=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $7 }'`
   upload=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $8 }'`
   upload=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $8 }'`
  download_used=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $9 }'`
  upload_used=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $10 }'`
done <<<`cat /tmp/fwspeedtest.out`
done <<<`cat /tmp/fwspeedtest.out`


Zeile 42: Zeile 44:
upload=`echo "scale=8; $upload * 8 / 1024 / 1024" | bc`
upload=`echo "scale=8; $upload * 8 / 1024 / 1024" | bc`
upload=`echo "scale=2; ($upload + 0.005) / 1" | bc`
upload=`echo "scale=2; ($upload + 0.005) / 1" | bc`
# Convert used bytes to MBytes and round values
download_used=`echo "scale=8; $download_used / 1024 / 1024" | bc`
download_used=`echo "scale=1; ($download_used + 0.05) / 1" | bc`
upload_used=`echo "scale=8; $upload_used / 1024 / 1024" | bc`
upload_used=`echo "scale=1; ($upload_used + 0.05) / 1" | bc`


# Insert data into database
# Insert data into database
sql="INSERT INTO fw_speedtest (timestamp,server_id,server_name,latency,"
sql="INSERT INTO fw_speedtest (timestamp,server_id,server_name,latency,"
sql+="jitter,packet_loss,download,upload,comment) VALUES ("
sql+="jitter,packet_loss,download,upload,download_used,upload_used,"
sql+="comment) VALUES ("
sql+="\"$timestamp\", \"$server_id\", \"$server_name\", \"$latency\", "
sql+="\"$timestamp\", \"$server_id\", \"$server_name\", \"$latency\", "
sql+="\"$jitter\", \"$packet_loss\", \"$download\", \"$upload\", "
sql+="\"$jitter\", \"$packet_loss\", \"$download\", \"$upload\", "
sql+="\"$comment\")"
sql+="\"$download_used\", \"$upload_used\", \"$comment\")"


mysql --login-path=fwsysmon -D fwsysmon -e "$sql"
mysql --login-path=fwsysmon -D fwsysmon -e "$sql"

Version vom 10. April 2020, 18:28 Uhr

Shell script /usr/bin/fwspeedtest

#!/bin/bash
#
# Author:  Frank Wulf
# Version: 1.0 (2020-04-10)
#
# Version history:
# 1.0   2020-04-10   Initial release
#

# Run speedtest
/usr/bin/speedtest --format=csv >/tmp/fwspeedtest.out
if [ $? -ne 0 ]; then
  exit 1
fi

while read line; do
  server_id=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $3 }'`
  server_name=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $2 }'`
  latency=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $4 }'`
  jitter=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $5 }'`
  packet_loss=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $6 }'`
  download=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $7 }'`
  upload=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $8 }'`
  download_used=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $9 }'`
  upload_used=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $10 }'`
done <<<`cat /tmp/fwspeedtest.out`

rm /tmp/fwspeedtest.out 1>/dev/null 2>&1

# Fill timestamp field
timestamp=`date +'%Y-%m-%d %H:%M:%S'`

# Round values for latency. jitter and packet_loss
latency=`echo "scale=2; ($latency + 0.005) / 1" | bc`
jitter=`echo "scale=2; ($jitter + 0.005) / 1" | bc`
packet_loss=`echo "scale=1; ($packet_loss + 0.05) / 1" | bc`

# Convert download/upload speed from bytes per second to Mbps and round values
download=`echo "scale=8; $download * 8 / 1024 / 1024" | bc`
download=`echo "scale=2; ($download + 0.005) / 1" | bc`
upload=`echo "scale=8; $upload * 8 / 1024 / 1024" | bc`
upload=`echo "scale=2; ($upload + 0.005) / 1" | bc`

# Convert used bytes to MBytes and round values
download_used=`echo "scale=8; $download_used / 1024 / 1024" | bc`
download_used=`echo "scale=1; ($download_used + 0.05) / 1" | bc`
upload_used=`echo "scale=8; $upload_used / 1024 / 1024" | bc`
upload_used=`echo "scale=1; ($upload_used + 0.05) / 1" | bc`

# Insert data into database
sql="INSERT INTO fw_speedtest (timestamp,server_id,server_name,latency,"
sql+="jitter,packet_loss,download,upload,download_used,upload_used,"
sql+="comment) VALUES ("
sql+="\"$timestamp\", \"$server_id\", \"$server_name\", \"$latency\", "
sql+="\"$jitter\", \"$packet_loss\", \"$download\", \"$upload\", "
sql+="\"$download_used\", \"$upload_used\", \"$comment\")"

mysql --login-path=fwsysmon -D fwsysmon -e "$sql"

exit 0

Scheduling Speedtest with systemd

  • Service Unit file /lib/systemd/system/fwspeedtest.service
[Unit]
Description=Run LTE speedtest

[Service]
Type=oneshot
ExecStart=/usr/bin/fwspeedtest
User=root
Group=root
  • Timer Unit file /lib/systemd/system/fwspeedtest.timer
[Unit]
Description=Run LTE speedtest four times a day

[Timer]
OnCalendar=*-*-* 0,6,12,18:00
RandomizedDelaySec=6h
Persistent=true

[Install]
WantedBy=timers.target