Internet Speedtest: Unterschied zwischen den Versionen

Aus wiki.frank-wulf.de
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 5: Zeile 5:
#
#
# Author:  Frank Wulf
# Author:  Frank Wulf
# Version: 1.1 (2020-04-18)
# Version: 1.2 (2020-04-24)
#
# Version history:
# 1.0  2020-04-11  Initial release
# 1.1  2020-04-18  Changed to best-of-three mode
#
#


runs=2
tarif="Freenet FUNK"
exclude=0
comment=""
comment=""
out=/tmp/fwspeedtest.out
out=/tmp/fwspeedtest.out
Zeile 19: Zeile 18:
fi
fi


# Run the speedtest three times
# Run the speedtest <runs> times without VPN connection
for i in {1..3}
for i in `eval echo {1..$runs}`
do
do
   # Check if VPN connection is active
   # Check if VPN connection is active
Zeile 28: Zeile 27:
     # Stop VPN connection
     # Stop VPN connection
     systemctl stop openvpn
     systemctl stop openvpn
    sleep 5
   else
   else
     vpn_active=0
     vpn_active=0
   fi
   fi
  # Fill timestamp field
  timestamp[$i]=`date +'%Y-%m-%d %H:%M:%S'`


   # Run speedtest
   # Run speedtest
Zeile 45: Zeile 48:
   fi
   fi


   if [ $i -lt 3 ]; then
   if [ $i -lt $runs ]; then
     # Wait 15 seconds between two speedtests
     # Wait 15 seconds between two speedtests
     sleep 15
     sleep 15
Zeile 51: Zeile 54:
done
done


download=0
download_max=0
i=0


while read line; do
while read line; do
  i=$((i+1))
   download_new=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $7 }'`
   download_new=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $7 }'`
   if [ $download_new -lt $download ]; then
   if [ $download_new -gt $download_max ]; then
     # Download rate is slower than previous ones
     # Download rate is faster than previous ones
     continue
     sw_max=1
  else
    sw_max=0
  fi
 
  if [ $sw_max -eq 1 ]; then
    download=$download_new
    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 }'`
    download_max=$download
    row=$i
   fi
   fi
  download=$download_new
  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 $out`


rm $out 1>/dev/null 2>&1
  if [ $i -eq $runs ]; then
    # 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`


# Round values for latency. jitter and packet_loss
    # Convert used bytes to MBytes and round values
latency=`echo "scale=2; ($latency + 0.005) / 1" | bc`
    download_used=`echo "scale=8; $download_used / 1024 / 1024" | bc`
jitter=`echo "scale=2; ($jitter + 0.005) / 1" | bc`
    download_used=`echo "scale=1; ($download_used + 0.05) / 1" | bc`
packet_loss=`echo "scale=1; ($packet_loss + 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`


# Convert download/upload speed from bytes per second to Mbps and round values
    vpn_active=0
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
    # Fill timestamp field
download_used=`echo "scale=8; $download_used / 1024 / 1024" | bc`
    timestamp=${timestamp[$row]}
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`


# Fill timestamp field
    # Insert data into database
timestamp=`date +'%Y-%m-%d %H:%M:%S'`
    sql="INSERT INTO fw_speedtest (timestamp,server_id,server_name,latency,"
    sql+="jitter,packet_loss,download,upload,download_used,upload_used,"
    sql+="vpn_active,tarif,exclude,comment) VALUES ("
    sql+="\"$timestamp\", \"$server_id\", \"$server_name\", \"$latency\", "
    sql+="\"$jitter\", \"$packet_loss\", \"$download\", \"$upload\", "
    sql+="\"$download_used\", \"$upload_used\", \"$vpn_active\", "
    sql+="\"$tarif\", \"$exclude\", \"$comment\")"


# Insert data into database
    mysql --login-path=fwsysmon -D fwsysmon -e "$sql"
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"
  fi
 
done <<<`cat $out`


exit 0
exit 0

Version vom 3. Mai 2020, 11:30 Uhr

Shell script /usr/bin/fwspeedtest

#!/bin/bash
#
# Author:  Frank Wulf
# Version: 1.2 (2020-04-24)
#

runs=2
tarif="Freenet FUNK"
exclude=0
comment=""
out=/tmp/fwspeedtest.out

if [ -f "$out" ]; then
  rm $out 1>/dev/null 2>&1
fi

# Run the speedtest <runs> times without VPN connection
for i in `eval echo {1..$runs}`
do
  # Check if VPN connection is active
  systemctl status openvpn 1>/dev/null 2>&1
  if [ $? -eq 0 ]; then
    vpn_active=1
    # Stop VPN connection
    systemctl stop openvpn
    sleep 5
  else
    vpn_active=0
  fi

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

  # Run speedtest
  /usr/bin/speedtest --format=csv >>$out
  retcode=$?

  if [ $vpn_active -eq 1 ]; then
    # Start VPN connection
    systemctl start openvpn
  fi

  if [ "$retcode" -ne 0 ]; then
    exit 1
  fi

  if [ $i -lt $runs ]; then
    # Wait 15 seconds between two speedtests
    sleep 15
  fi
done

download_max=0
i=0

while read line; do
  i=$((i+1))
  download_new=`echo "$line"|awk -F '^ *"|" *, *"|" *$' '{ print $7 }'`
  if [ $download_new -gt $download_max ]; then
    # Download rate is faster than previous ones
    sw_max=1
  else
    sw_max=0
  fi

  if [ $sw_max -eq 1 ]; then
    download=$download_new
    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 }'`
    download_max=$download
    row=$i
  fi

  if [ $i -eq $runs ]; then
    # 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`

    vpn_active=0

    # Fill timestamp field
    timestamp=${timestamp[$row]}

    # 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+="vpn_active,tarif,exclude,comment) VALUES ("
    sql+="\"$timestamp\", \"$server_id\", \"$server_name\", \"$latency\", "
    sql+="\"$jitter\", \"$packet_loss\", \"$download\", \"$upload\", "
    sql+="\"$download_used\", \"$upload_used\", \"$vpn_active\", "
    sql+="\"$tarif\", \"$exclude\", \"$comment\")"

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

  fi

done <<<`cat $out`

exit 0

Scheduling Speedtest with systemd

  • Service Unit file /lib/systemd/system/fwspeedtest.service
[Unit]
Description=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