Skip to content

Commit

Permalink
copy speedtest log to volume
Browse files Browse the repository at this point in the history
  • Loading branch information
ipitio committed Feb 13, 2024
1 parent 151d4db commit 9e2ffd0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
2 changes: 2 additions & 0 deletions advanced/Scripts/speedtestmod/mod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ manageHistory() {
fi
if [ -f /var/log/pihole/speedtest.log ]; then
mv -f /var/log/pihole/speedtest.log /var/log/pihole/speedtest.log.old
rm -f /etc/pihole/speedtest.log
fi
elif [ -f $last_db ]; then
echo "Restoring Database..."
Expand All @@ -105,6 +106,7 @@ manageHistory() {
fi
if [ -f /var/log/pihole/speedtest.log.old ]; then
mv -f /var/log/pihole/speedtest.log.old /var/log/pihole/speedtest.log
cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log
fi
fi
fi
Expand Down
72 changes: 42 additions & 30 deletions advanced/Scripts/speedtestmod/speedtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,64 @@ speedtest() {
fi
}

savetest() {
local start_time=$1
local stop_time=$2
local isp=${3-"No Internet"}
local from_ip=${4-"-"}
local server=${5-"-"}
local server_dist=${6-0}
local server_ping=${7-0}
local download=${8-0}
local upload=${9-0}
local share_url=${10-"#"}
sqlite3 /etc/pihole/speedtest.db "$create_table"
sqlite3 /etc/pihole/speedtest.db "insert into speedtest values (NULL, '${start_time}', '${stop_time}', '${isp}', '${from_ip}', '${server}', ${server_dist}, ${server_ping}, ${download}, ${upload}, '${share_url}');"
mv -f "$out" /var/log/pihole/speedtest.log
cp -af /var/log/pihole/speedtest.log /etc/pihole/speedtest.log
rm -f /tmp/speedtest_results
[ "$isp" == "No Internet" ] && exit 1 || exit 0
}

internet() {
stop=$(date -u --rfc-3339='seconds')
res="$(</tmp/speedtest_results)"
server_id=$(jq -r '.server.id' <<< "$res")
servers="$(curl 'https://www.speedtest.net/api/js/servers' --compressed -H 'Upgrade-Insecure-Requests: 1' -H 'DNT: 1' -H 'Sec-GPC: 1')"
server_dist=$(jq --arg id "$server_id" '.[] | select(.id == $id) | .distance' <<< "$servers")
local stop=$(date -u --rfc-3339='seconds')
local res="$(</tmp/speedtest_results)"
local server_id=$(jq -r '.server.id' <<< "$res")
local servers="$(curl 'https://www.speedtest.net/api/js/servers' --compressed -H 'Upgrade-Insecure-Requests: 1' -H 'DNT: 1' -H 'Sec-GPC: 1')"
local server_dist=$(jq --arg id "$server_id" '.[] | select(.id == $id) | .distance' <<< "$servers")

if grep -q official <<< "$(/usr/bin/speedtest --version)"; then
server_name=$(jq -r '.server.name' <<< "$res")
download=$(jq -r '.download.bandwidth' <<< "$res" | awk '{$1=$1*8/1000/1000; print $1;}' | sed 's/,/./g')
upload=$(jq -r '.upload.bandwidth' <<< "$res" | awk '{$1=$1*8/1000/1000; print $1;}' | sed 's/,/./g')
isp=$(jq -r '.isp' <<< "$res")
from_ip=$(jq -r '.interface.externalIp' <<< "$res")
server_ping=$(jq -r '.ping.latency' <<< "$res")
share_url=$(jq -r '.result.url' <<< "$res")
local server_name=$(jq -r '.server.name' <<< "$res")
local download=$(jq -r '.download.bandwidth' <<< "$res" | awk '{$1=$1*8/1000/1000; print $1;}' | sed 's/,/./g')
local upload=$(jq -r '.upload.bandwidth' <<< "$res" | awk '{$1=$1*8/1000/1000; print $1;}' | sed 's/,/./g')
local isp=$(jq -r '.isp' <<< "$res")
local from_ip=$(jq -r '.interface.externalIp' <<< "$res")
local server_ping=$(jq -r '.ping.latency' <<< "$res")
local share_url=$(jq -r '.result.url' <<< "$res")
if [ -z "$server_dist" ]; then
server_dist="-1"
fi
else
server_name=$(jq -r '.server.sponsor' <<< "$res")
download=$(jq -r '.download' <<< "$res" | awk '{$1=$1/1000/1000; print $1;}' | sed 's/,/./g')
upload=$(jq -r '.upload' <<< "$res" | awk '{$1=$1/1000/1000; print $1;}' | sed 's/,/./g')
isp=$(jq -r '.client.isp' <<< "$res")
from_ip=$(jq -r '.client.ip' <<< "$res")
server_ping=$(jq -r '.ping' <<< "$res")
share_url=$(jq -r '.share' <<< "$res")
local server_name=$(jq -r '.server.sponsor' <<< "$res")
local download=$(jq -r '.download' <<< "$res" | awk '{$1=$1/1000/1000; print $1;}' | sed 's/,/./g')
local upload=$(jq -r '.upload' <<< "$res" | awk '{$1=$1/1000/1000; print $1;}' | sed 's/,/./g')
local isp=$(jq -r '.client.isp' <<< "$res")
local from_ip=$(jq -r '.client.ip' <<< "$res")
local server_ping=$(jq -r '.ping' <<< "$res")
local share_url=$(jq -r '.share' <<< "$res")
if [ -z "$server_dist" ]; then
server_dist=$(jq -r '.server.d' <<< "$res")
fi
fi

jq . /tmp/speedtest_results
sqlite3 /etc/pihole/speedtest.db "$create_table"
sqlite3 /etc/pihole/speedtest.db "insert into speedtest values (NULL, '${start}', '${stop}', '${isp}', '${from_ip}', '${server_name}', ${server_dist}, ${server_ping}, ${download}, ${upload}, '${share_url}');"
mv -f "$out" /var/log/pihole/speedtest.log
exit 0
savetest "$start" "$stop" "$isp" "$from_ip" "$server_name" "$server_dist" "$server_ping" "$download" "$upload" "$share_url"
}

nointernet(){
stop=$(date -u --rfc-3339='seconds')
rm -f /tmp/speedtest_results
echo "No Internet"
sqlite3 /etc/pihole/speedtest.db "$create_table"
sqlite3 /etc/pihole/speedtest.db "insert into speedtest values (NULL, '${start}', '${stop}', 'No Internet', '-', '-', 0, 0, 0, 0, '#');"
mv -f "$out" /var/log/pihole/speedtest.log
exit 1
local stop=$(date -u --rfc-3339='seconds')
echo "No Internet" > /tmp/speedtest_results
savetest "$start" "$stop"
}

notInstalled() {
Expand Down
1 change: 0 additions & 1 deletion advanced/Scripts/webpage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ EOF
if [[ "$total_seconds" == "nan" ]] || (( $(echo "$total_seconds > 0" | bc -l) )); then
crontab -l &> /dev/null || crontab -l 2>/dev/null | { cat; echo ""; } | crontab -
(crontab -l; echo "* * * * * /bin/bash $schedule_script") | crontab -
sudo bash -c "$schedule_script"
fi
}

Expand Down

0 comments on commit 9e2ffd0

Please sign in to comment.