Skip to content

Commit

Permalink
create db
Browse files Browse the repository at this point in the history
  • Loading branch information
ipitio committed Jan 26, 2024
1 parent b6ab4f3 commit 8b540fa
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 123 deletions.
37 changes: 15 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,29 @@ Test your connection speed directly in the Pi-hole web interface!

![Dashboard](assets/dashboard.png)

## Features

This Speedtest Mod is, as the name suggests, a speedtest mod for Pi-hole. It runs speedtests (usually*) using [Ookla's `speedtest`](https://www.speedtest.net/apps/cli) and logs the results in a database. You can:

* View the results in the web interface (Speedtest Log),
* Flush the Database, or
* Restore it until a new speedtest is run.
This Speedtest Mod is, as the name suggests, a speedtest mod for Pi-hole. We recommend running speedtests using [Ookla's `speedtest`](https://www.speedtest.net/apps/cli), but will respect your choice to use the potentially less accurate `speedtest-cli` if you already have it installed. Should one of these fail, the other will be tried.

This limitation is of the script/GUI; you can always manipulate the database directly or export it as a CSV (Speedtest Log > Export As CSV); however, the Mod does also allow you to:
> **Notes**
>
> * The more tests you run, the more data will be used.
> * Any issues about inconsistent or inaccurate results should be directed to the maintainers of whichever speedtest package is installed on your system, not here.
* Install, update, and uninstall it,
* Set a custom speedtest server,
* Run tests ad-hoc and/or at set intervals, and
* Display a pretty line or bar chart on the dashboard of the last 1/2/4/7/30 days of tests.

![Settings](assets/settings.png)
## Features

Pull requests and suggestions are welcome!

Please note that the more tests you run, the more data will be used. Also note that any issues relating to inconsistent or inaccurate results should be directed to the speedtest package maintainers, not here.

<sup>

\* While `speedtest` is preferred, by way of being reinstalled every time the Mod is updated, `speedtest-cli` will be tried should it fail. Being Python-based, `speedtest-cli` results may be less consistent or accurate; nevertheless, having it as a fallback is better than nothing.
* Fast and safe un/re/install script (Mod the Mod)
* Customizable speedtest server
* Test ad-hoc and/or on a schedule
* A pretty line or bar chart on the dashboard
* View the results and export them as a CSV in the Speedtest Log
* Flush and restore the database

</sup>
![Settings](assets/settings.png)

## Usage

The provided script by @ipitio can un/re/install and update the mod, and manage its history. It accepts up to three arguments: any, all, or none of `up`, `un`, and `db`. They must be in that order; check usage for details. Its functionality is available via the web interface as well (Settings > Speedtest).
The provided script by @ipitio can un/re/install and update the mod, and manage its history, for you. It accepts up to three arguments: any, all, or none of `up`, `un`, and `db`. They must be in that order; check usage for details. Its functionality is available via the web interface as well (Settings > Speedtest).

### Install

Expand Down Expand Up @@ -74,8 +67,8 @@ curl -sSLN https://github.com/arevindh/pihole-speedtest/raw/master/mod.sh | sudo
Web 5.21

<details>

<summary>Older Notes</summary>
</br>

**June 8 2023**

Expand Down
220 changes: 119 additions & 101 deletions mod.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
#!/bin/bash
LOG_FILE="/var/log/pimod.log"

admin_dir=/var/www/html
curr_wp=/opt/pihole/webpage.sh
last_wp=$curr_wp.old
org_wp=$curr_wp.org

curr_db=/etc/pihole/speedtest.db
last_db=$curr_db.old
db_table="speedtest"
create_table="create table if not exists $db_table (
id integer primary key autoincrement,
start_time integer,
stop_time text,
from_server text,
from_ip text,
server text,
server_dist real,
server_ping real,
download real,
upload real,
share_url text
);"

help() {
echo "(Re)install Latest Speedtest Mod."
echo "Usage: sudo $0 [up] [un] [db]"
Expand Down Expand Up @@ -46,17 +68,12 @@ download() {
if [ ! -z "$src" ]; then
if [ "$url" != "old" ]; then
git config --global --add safe.directory "$dest"
if ! git remote -v | grep -q "old"; then
git remote rename origin old
fi
if git remote -v | grep -q "origin"; then
git remote remove origin
fi
git remote -v | grep -q "old" || git remote rename origin old
git remote -v | grep -q "origin" && git remote remove origin
git remote add origin $url
else
git remote rename origin new
git remote remove origin
git remote rename old origin
git remote remove new
fi
git fetch origin -q
fi
Expand All @@ -67,6 +84,32 @@ download() {
cd ..
}

isEmpty() {
db=$1
if [ -f $db ]; then
if ! sqlite3 "$db" "select * from $db_table limit 1;" >/dev/null 2>&1 || [ -z "$(sqlite3 "$db" "select * from $db_table limit 1;")" ]; then
return 0
fi
fi
return 1
}

manageHistory() {
if [ "${1-}" == "db" ]; then
if [ -f $curr_db ] && ! isEmpty $curr_db; then
if [ -z "${2-}" ]; then
echo "$(date) - Flushing Database..."
mv -f $curr_db $last_db
fi
elif [ -f $last_db ]; then
echo "$(date) - Restoring Database..."
mv -f $last_db $curr_db
fi
echo "$(date) - Configuring Database..."
sqlite3 "$curr_db" "$create_table"
fi
}

install() {
echo "$(date) - Installing any missing dependencies..."

Expand Down Expand Up @@ -97,8 +140,21 @@ install() {
curl -sSLN https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | sudo bash
fi
fi
local PHP_VERSION=$(php -v | head -n 1 | awk '{print $2}' | cut -d "." -f 1,2)
apt-get install -y sqlite3 "${PHP_VERSION}-sqlite3" jq speedtest-cli- speedtest

local PHP_VERSION=$(php -v | head -n 1 | awk '{print $2}' | cut -d "." -f 1,2)
local packages="sqlite3 php${PHP_VERSION}-sqlite3 jq"
for package in $packages; do
if dpkg -s $package >/dev/null 2>&1; then
packages=$(echo $packages | sed "s/$package//")
fi
done
if ! dpkg -s speedtest >/dev/null 2>&1 && ! dpkg -s speedtest-cli >/dev/null 2>&1; then
packages="$packages speedtest"
fi
if [ ! -z "$(echo $packages | sed 's/ //g')" ]; then
apt-get install -y $packages
fi

if [ -f /usr/local/bin/speedtest ]; then
rm -f /usr/local/bin/speedtest
ln -s /usr/bin/speedtest /usr/local/bin/speedtest
Expand All @@ -107,54 +163,63 @@ install() {
echo "$(date) - Installing Latest Speedtest Mod..."

download /opt mod_pihole https://github.com/arevindh/pi-hole
cp pihole/webpage.sh pihole/webpage.sh.org
cp mod_pihole/advanced/Scripts/webpage.sh pihole/webpage.sh.mod

download /var/www/html admin https://github.com/arevindh/AdminLTE web
cd /opt
if [ ! -f pihole/webpage.sh.bak ] && [ -f pihole/webpage.sh ]; then
cp pihole/webpage.sh pihole/webpage.sh.bak
fi
cp pihole/webpage.sh.mod pihole/webpage.sh
chmod +x pihole/webpage.sh

if [ ! -f /etc/pihole/speedtest.db ]; then
local last_db=/etc/pihole/speedtest.db.old
if [ -f $last_db ]; then
echo "$(date) - Restoring Database..."
mv $last_db /etc/pihole/
else
echo "$(date) - Creating Database..."
cp /var/www/html/admin/scripts/pi-hole/speedtest/speedtest.db /etc/pihole/
if [ -f $curr_wp ]; then
if ! cat $curr_wp | grep -q SpeedTest; then
cp $curr_wp $org_wp
fi
if [ ! -f $last_wp ]; then
cp $curr_wp $last_wp
fi
fi

cp /opt/mod_pihole/advanced/Scripts/webpage.sh $curr_wp
chmod +x $curr_wp
manageHistory db .
pihole -a -s
pihole updatechecker local
}

hashFile() {
md5sum $1 | cut -d ' ' -f 1
uninstall() {
if [ -f $curr_wp ] && cat $curr_wp | grep -q SpeedTest; then
echo "$(date) - Uninstalling Current Speedtest Mod..."

if [ ! -f $org_wp ]; then
if [ ! -d /opt/org_pihole ]; then
download /opt org_pihole https://github.com/pi-hole/pi-hole Pi-hole
fi
cd /opt
cp org_pihole/advanced/Scripts/webpage.sh $org_wp
rm -rf org_pihole
fi

pihole -a -su
download /var/www/html admin https://github.com/pi-hole/AdminLTE web
if [ ! -f $last_wp ]; then
cp $curr_wp $last_wp
fi
cp $org_wp $curr_wp
chmod +x $curr_wp
fi

manageHistory ${1-}
}

purge() {
echo "$(date) - Cleaning up..."
rm -rf /opt/pihole/webpage.sh.*
rm -rf /var/www/html/*_admin
rm -rf /etc/pihole/speedtest.db.*
rm -rf /etc/pihole/speedtest.db_*
rm -rf "$curr_wp".*
rm -rf "$admin_dir"*_admin
rm -rf "$curr_db".*
rm -rf "$curr_db"_*
rm -rf /opt/mod_pihole

local init_db=/var/www/html/admin/scripts/pi-hole/speedtest/speedtest.db
local curr_db=/etc/pihole/speedtest.db
if [ -f $init_db ] && [ -f $curr_db ] && [ "$(hashFile $init_db)" == "$(hashFile /etc/pihole/speedtest.db)" ]; then
rm -f /etc/pihole/speedtest.db
if isEmpty $curr_db; then
rm -f $curr_db
fi
}

update() {
echo "$(date) - Updating Pi-hole..."
cd /var/www/html/admin
cd $admin_dir/admin
git reset --hard origin/master
git checkout master
PIHOLE_SKIP_OS_CHECK=true sudo -E pihole -up
Expand All @@ -164,75 +229,28 @@ update() {
fi
}

manageHistory() {
local init_db=/var/www/html/admin/scripts/pi-hole/speedtest/speedtest.db
local curr_db=/etc/pihole/speedtest.db
local last_db=/etc/pihole/speedtest.db.old
if [ "${1-}" == "db" ]; then
if [ -f $curr_db ] && [ -f $init_db ] && [ "$(hashFile $curr_db)" != "$(hashFile $init_db)" ]; then
echo "$(date) - Flushing Database..."
mv -f $curr_db $last_db
elif [ -f $last_db ]; then
echo "$(date) - Restoring Database..."
mv -f $last_db $curr_db
fi
fi
}

uninstall() {
if [ -f /opt/pihole/webpage.sh ] && cat /opt/pihole/webpage.sh | grep -q SpeedTest; then
echo "$(date) - Uninstalling Current Speedtest Mod..."

cd /opt
cp pihole/webpage.sh pihole/webpage.sh.mod
if [ ! -f /opt/pihole/webpage.sh.bak ]; then
cp pihole/webpage.sh pihole/webpage.sh.bak
fi

if [ ! -f /opt/pihole/webpage.sh.org ]; then
if [ ! -d /opt/org_pihole ]; then
download /opt org_pihole https://github.com/pi-hole/pi-hole Pi-hole
fi
cd /opt
cp org_pihole/advanced/Scripts/webpage.sh pihole/webpage.sh.org
rm -rf org_pihole
fi

pihole -a -su
download /var/www/html admin https://github.com/pi-hole/AdminLTE web
cd /opt/pihole/
cp webpage.sh.org webpage.sh
chmod +x webpage.sh
fi

manageHistory ${1-}
}

abort() {
if (( $aborted == 1 )); then
if (($aborted == 1)); then
exit 1
fi
aborted=1

echo "$(date) - Process Aborting..." | sudo tee -a /var/log/pimod.log
echo "$(date) - Process Aborting..."

if [ -f /opt/pihole/webpage.sh.bak ]; then
cp /opt/pihole/webpage.sh.bak /opt/pihole/webpage.sh
chmod +x /opt/pihole/webpage.sh
rm -f /opt/pihole/webpage.sh.bak
if [ -f $last_wp ]; then
cp $last_wp $curr_wp
chmod +x $curr_wp
rm -f $last_wp
fi

if [ -d /var/www/html/admin/.git/refs/remotes/old ]; then
download /var/www/html admin old web
if [ -f $last_db ] && [ ! -f $curr_db ]; then
mv $last_db $curr_db
fi

if [ -f /etc/pihole/speedtest.db.old ] && [ ! -f /etc/pihole/speedtest.db ]; then
mv /etc/pihole/speedtest.db.old /etc/pihole/speedtest.db
fi

if [ ! -f /opt/pihole/webpage.sh ] || ! cat /opt/pihole/webpage.sh | grep -q SpeedTest; then
if [ ! -f $curr_wp ] || ! cat $curr_wp | grep -q SpeedTest; then
purge
fi
if [ -d $admin_dir/admin/.git/refs/remotes/old ]; then
download $admin_dir admin old web
fi

pihole restartdns
echo "$(date) - Please try again or try manually."
Expand All @@ -241,9 +259,9 @@ abort() {

commit() {
echo "$(date) - Almost Done..."
cd /var/www/html/admin
cd $admin_dir/admin
git remote -v | grep -q "old" && git remote remove old
rm -f /opt/pihole/webpage.sh.bak
rm -f $last_wp
pihole restartdns
echo "$(date) - Done!"
exit 0
Expand Down

0 comments on commit 8b540fa

Please sign in to comment.