From adcdad701b4e906eba29195017d8264465abbe16 Mon Sep 17 00:00:00 2001 From: Martinski <119833648+Martinski4GitHub@users.noreply.github.com> Date: Thu, 4 Jul 2024 01:59:15 -0700 Subject: [PATCH 1/5] Fixed code to get space available in USB drive Ported code over from "connmon" script that calculates correctly the free space available in a USB-attached drive that has hundreds of GBytes free. The code also compares correctly the "Available space" vs the "Required space." --- spdmerlin.sh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/spdmerlin.sh b/spdmerlin.sh index 354005c..56159d1 100644 --- a/spdmerlin.sh +++ b/spdmerlin.sh @@ -13,7 +13,7 @@ ## https://github.com/jackyaz/spdMerlin ## ## ## ############################################################## -# Last Modified: 2024-Jul-01 +# Last Modified: 2024-Jul-04 #------------------------------------------------------------- ############## Shellcheck directives ############# @@ -2029,10 +2029,18 @@ Generate_CSVs(){ renice 0 $$ } -Reset_DB(){ +##----------------------------------------## +## Modified by Martinski W. [2024-Jul-04] ## +##----------------------------------------## +# [Ported code over from "connmon" script] # +Reset_DB() +{ SIZEAVAIL="$(df -P -k "$SCRIPT_STORAGE_DIR" | awk '{print $4}' | tail -n 1)" SIZEDB="$(ls -l "$SCRIPT_STORAGE_DIR/spdstats.db" | awk '{print $5}')" - if [ "$SIZEDB" -gt "$((SIZEAVAIL*1024))" ]; then + SIZEAVAIL="$(echo "$SIZEAVAIL" | awk '{printf("%s", $1 * 1024);}')" + + if [ "$(echo "$SIZEAVAIL $SIZEDB" | awk -F ' ' '{print ($1 < $2)}')" -eq 1 ] + then Print_Output true "Database size exceeds available space. $(ls -lh "$SCRIPT_STORAGE_DIR/spdstats.db" | awk '{print $5}')B is required to create backup." "$ERR" return 1 else From 9c81869598ff691ebd52239368e44682514f9d0c Mon Sep 17 00:00:00 2001 From: Martinski <119833648+Martinski4GitHub@users.noreply.github.com> Date: Thu, 4 Jul 2024 02:02:06 -0700 Subject: [PATCH 2/5] Update README.md Updated date string. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7692a06..a4e139f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ![Shellcheck](https://github.com/jackyaz/spdMerlin/actions/workflows/shellcheck.yml/badge.svg) ## v4.4.6 -### Updated on 2024-July-01 +### Updated on 2024-July-04 ## About spdMerlin is an internet speedtest and monitoring tool for AsusWRT Merlin with charts for daily, weekly and monthly summaries. It tracks download/upload bandwidth as well as latency, jitter and packet loss. From c899e5476d927de11a6dc950ef6dcb816b2113df Mon Sep 17 00:00:00 2001 From: Martinski <119833648+Martinski4GitHub@users.noreply.github.com> Date: Mon, 15 Jul 2024 00:59:31 -0700 Subject: [PATCH 3/5] Revert "Update README.md" This reverts commit 9c81869598ff691ebd52239368e44682514f9d0c. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a4e139f..7692a06 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ![Shellcheck](https://github.com/jackyaz/spdMerlin/actions/workflows/shellcheck.yml/badge.svg) ## v4.4.6 -### Updated on 2024-July-04 +### Updated on 2024-July-01 ## About spdMerlin is an internet speedtest and monitoring tool for AsusWRT Merlin with charts for daily, weekly and monthly summaries. It tracks download/upload bandwidth as well as latency, jitter and packet loss. From 033fcc5393745f079f546032b2845e017564cd70 Mon Sep 17 00:00:00 2001 From: Martinski <119833648+Martinski4GitHub@users.noreply.github.com> Date: Mon, 15 Jul 2024 01:03:53 -0700 Subject: [PATCH 4/5] Revert "Fixed code to get space available in USB drive" This reverts commit adcdad701b4e906eba29195017d8264465abbe16. --- spdmerlin.sh | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/spdmerlin.sh b/spdmerlin.sh index 56159d1..354005c 100644 --- a/spdmerlin.sh +++ b/spdmerlin.sh @@ -13,7 +13,7 @@ ## https://github.com/jackyaz/spdMerlin ## ## ## ############################################################## -# Last Modified: 2024-Jul-04 +# Last Modified: 2024-Jul-01 #------------------------------------------------------------- ############## Shellcheck directives ############# @@ -2029,18 +2029,10 @@ Generate_CSVs(){ renice 0 $$ } -##----------------------------------------## -## Modified by Martinski W. [2024-Jul-04] ## -##----------------------------------------## -# [Ported code over from "connmon" script] # -Reset_DB() -{ +Reset_DB(){ SIZEAVAIL="$(df -P -k "$SCRIPT_STORAGE_DIR" | awk '{print $4}' | tail -n 1)" SIZEDB="$(ls -l "$SCRIPT_STORAGE_DIR/spdstats.db" | awk '{print $5}')" - SIZEAVAIL="$(echo "$SIZEAVAIL" | awk '{printf("%s", $1 * 1024);}')" - - if [ "$(echo "$SIZEAVAIL $SIZEDB" | awk -F ' ' '{print ($1 < $2)}')" -eq 1 ] - then + if [ "$SIZEDB" -gt "$((SIZEAVAIL*1024))" ]; then Print_Output true "Database size exceeds available space. $(ls -lh "$SCRIPT_STORAGE_DIR/spdstats.db" | awk '{print $5}')B is required to create backup." "$ERR" return 1 else From 2d25eeb9e1865312fd7e5acb52ef4aa184414603 Mon Sep 17 00:00:00 2001 From: Martinski <119833648+Martinski4GitHub@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:30:16 -0700 Subject: [PATCH 5/5] Fixed "Reset Database" functionality Fixed the "Reset Database" functionality on the CLI menu where the database file was correctly reset but the result was not reflected on the webGUI page where "old" entries were still shown as if the database had not been reset. --- README.md | 2 +- spdmerlin.sh | 88 +++++++++++++++++++++++++++++++++++------------- spdstats_www.asp | 7 ++-- spdstats_www.js | 39 +++++++++++++++++---- 4 files changed, 103 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 0065c96..57881f9 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ![Shellcheck](https://github.com/jackyaz/spdMerlin/actions/workflows/shellcheck.yml/badge.svg) ## v4.4.6 -### Updated on 2024-July-07 +### Updated on 2024-July-15 ## About spdMerlin is an internet speedtest and monitoring tool for AsusWRT Merlin with charts for daily, weekly and monthly summaries. It tracks download/upload bandwidth as well as latency, jitter and packet loss. diff --git a/spdmerlin.sh b/spdmerlin.sh index 15026f5..8f50b4f 100644 --- a/spdmerlin.sh +++ b/spdmerlin.sh @@ -13,7 +13,7 @@ ## https://github.com/jackyaz/spdMerlin ## ## ## ############################################################## -# Last Modified: 2024-Jul-07 +# Last Modified: 2024-Jul-15 #------------------------------------------------------------- ############## Shellcheck directives ############# @@ -48,7 +48,7 @@ readonly OOKLA_DIR="$SCRIPT_DIR/ookla" readonly OOKLA_LICENSE_DIR="$SCRIPT_DIR/ooklalicense" readonly OOKLA_HOME_DIR="$HOME_DIR/.config/ookla" -[ -z "$(nvram get odmpid)" ] && ROUTER_MODEL=$(nvram get productid) || ROUTER_MODEL=$(nvram get odmpid) +[ -z "$(nvram get odmpid)" ] && ROUTER_MODEL="$(nvram get productid)" || ROUTER_MODEL="$(nvram get odmpid)" [ -f /opt/bin/sqlite3 ] && SQLITE3_PATH=/opt/bin/sqlite3 || SQLITE3_PATH=/usr/sbin/sqlite3 if [ "$(uname -m)" = "aarch64" ]; then @@ -936,7 +936,11 @@ TestSchedule(){ esac } -ScriptStorageLocation(){ +##----------------------------------------## +## Modified by Martinski W. [2024-Jul-15] ## +##----------------------------------------## +ScriptStorageLocation() +{ case "$1" in usb) sed -i 's/^STORAGELOCATION=.*$/STORAGELOCATION=usb/' "$SCRIPT_CONF" @@ -944,10 +948,12 @@ ScriptStorageLocation(){ mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/csv" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/.interfaces" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/.interfaces_user" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null + mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/.databaseupgraded" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/config" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/config.bak" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/spdtitletext.js" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/jffs/addons/$SCRIPT_NAME_LOWER.d/spdstats.db" "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null + mv "/jffs/addons/$SCRIPT_NAME_LOWER.d"/lastx_*.csv "/opt/share/$SCRIPT_NAME_LOWER.d/" 2>/dev/null SCRIPT_CONF="/opt/share/$SCRIPT_NAME_LOWER.d/config" ScriptStorageLocation load ;; @@ -957,10 +963,12 @@ ScriptStorageLocation(){ mv "/opt/share/$SCRIPT_NAME_LOWER.d/csv" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/opt/share/$SCRIPT_NAME_LOWER.d/.interfaces" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/opt/share/$SCRIPT_NAME_LOWER.d/.interfaces_user" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null + mv "/opt/share/$SCRIPT_NAME_LOWER.d/.databaseupgraded" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/opt/share/$SCRIPT_NAME_LOWER.d/config" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/opt/share/$SCRIPT_NAME_LOWER.d/config.bak" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/opt/share/$SCRIPT_NAME_LOWER.d/spdtitletext.js" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null mv "/opt/share/$SCRIPT_NAME_LOWER.d/spdstats.db" "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null + mv "/opt/share/$SCRIPT_NAME_LOWER.d"/lastx_*.csv "/jffs/addons/$SCRIPT_NAME_LOWER.d/" 2>/dev/null SCRIPT_CONF="/jffs/addons/$SCRIPT_NAME_LOWER.d/config" ScriptStorageLocation load ;; @@ -1184,14 +1192,25 @@ WriteSql_ToFile(){ fi } -WriteStats_ToJS(){ - echo "function $3(){" >> "$2" +##----------------------------------------## +## Modified by Martinski W. [2024-Jul-15] ## +##----------------------------------------## +WriteStats_ToJS() +{ + if [ $# -lt 4 ] ; then return 1 ; fi + + echo "function $3(){" > "$2" html='document.getElementById("'"$4"'").innerHTML="' - while IFS='' read -r line || [ -n "$line" ]; do - html="${html}${line}\\r\\n" + + while IFS='' read -r line || [ -n "$line" ] + do html="${html}${line}\r\n" done < "$1" html="$html"'"' - printf "%s\\r\\n}\\r\\n" "$html" >> "$2" + + if [ $# -lt 5 ] || [ -z "$5" ] + then printf "%s\r\n}\r\n" "$html" >> "$2" + else printf "%s;\r\n%s\r\n}\r\n" "$html" "$5" >> "$2" + fi } GenerateServerList(){ @@ -1724,11 +1743,10 @@ Run_Speedtest(){ echo 'var spdteststatus = "GenerateCSV";' > /tmp/detect_spdtest.js Print_Output true "Retrieving data for WebUI charts" "$PASS" Generate_CSVs - + echo "Stats last updated: $timenowfriendly" > /tmp/spdstatstitle.txt - rm -f "$SCRIPT_STORAGE_DIR/spdtitletext.js" WriteStats_ToJS /tmp/spdstatstitle.txt "$SCRIPT_STORAGE_DIR/spdtitletext.js" SetSPDStatsTitle statstitle - + if [ "$applyautobw" = "true" ]; then Menu_AutoBW_Update fi @@ -1787,7 +1805,11 @@ Run_Speedtest_WebUI(){ cp -a "$SCRIPT_CONF.bak" "$SCRIPT_CONF" } -Process_Upgrade(){ +##----------------------------------------## +## Modified by Martinski W. [2024-Jul-15] ## +##----------------------------------------## +Process_Upgrade() +{ if [ ! -f "$OOKLA_DIR/speedtest" ]; then Download_File "$SCRIPT_REPO/files/$ARCH.tar.gz" "$OOKLA_DIR/$ARCH.tar.gz" tar -xzf "$OOKLA_DIR/$ARCH.tar.gz" -C "$OOKLA_DIR" @@ -1796,12 +1818,10 @@ Process_Upgrade(){ fi rm -f "$SCRIPT_STORAGE_DIR/spdjs.js" rm -f "$SCRIPT_STORAGE_DIR/.tableupgraded"* + if [ ! -f "$SCRIPT_STORAGE_DIR/spdtitletext.js" ]; then - { - echo 'function SetSPDStatsTitle(){'; - echo 'document.getElementById("statstitle").innerHTML="Stats last updated: Not yet updated\r\n";'; - echo "}"; - } > "$SCRIPT_STORAGE_DIR/spdtitletext.js" + echo "Stats last updated: Not yet updated" > /tmp/spdstatstitle.txt + WriteStats_ToJS /tmp/spdstatstitle.txt "$SCRIPT_STORAGE_DIR/spdtitletext.js" SetSPDStatsTitle statstitle fi if [ ! -f "$SCRIPT_DIR/README.md" ]; then Update_File README.md @@ -1862,7 +1882,8 @@ Process_Upgrade(){ } #$1 iface name -Generate_LastXResults(){ +Generate_LastXResults() +{ FULL_IFACELIST="WAN VPNC1 VPNC2 VPNC3 VPNC4 VPNC5" for IFACE_NAME in $FULL_IFACELIST; do rm -f "$SCRIPT_STORAGE_DIR/lastx_${IFACE_NAME}.htm" @@ -1875,7 +1896,7 @@ Generate_LastXResults(){ "$SQLITE3_PATH" "$SCRIPT_STORAGE_DIR/spdstats.db" < /tmp/spd-lastx.sql rm -f /tmp/spd-lastx.sql sed -i 's/,,/,null,/g;s/"//g;' /tmp/spd-lastx.csv - mv /tmp/spd-lastx.csv "$SCRIPT_STORAGE_DIR/lastx_$1.csv" + mv /tmp/spd-lastx.csv "$SCRIPT_STORAGE_DIR/lastx_${1}.csv" } Generate_CSVs(){ @@ -2030,7 +2051,7 @@ Generate_CSVs(){ } ##----------------------------------------## -## Modified by Martinski W. [2024-Jul-07] ## +## Modified by Martinski W. [2024-Jul-15] ## ##----------------------------------------## # [Ported code over from "connmon" script] # Reset_DB() @@ -2048,15 +2069,34 @@ Reset_DB() if ! cp -a "$SCRIPT_STORAGE_DIR/spdstats.db" "$SCRIPT_STORAGE_DIR/spdstats.db.bak"; then Print_Output true "Database backup failed, please check storage device" "$WARN" fi - + + Print_Output false "Please wait..." "$PASS" tablelist="WAN VPNC1 VPNC2 VPNC3 VPNC4 VPNC5" for dbtable in $tablelist; do echo "DELETE FROM [spdstats_$dbtable];" > /tmp/spd-stats.sql "$SQLITE3_PATH" "$SCRIPT_STORAGE_DIR/spdstats.db" < /tmp/spd-stats.sql rm -f /tmp/spd-stats.sql done - + + ## Clear/Reset all CSV files ## + Generate_CSVs + + ## Show "reset" messages on webGUI ## + timeDateNow="$(/bin/date +"%c")" + extraJScode='databaseResetDone += 1;' + echo "Resetting stats: $timeDateNow" > /tmp/spdstatstitle.txt + WriteStats_ToJS /tmp/spdstatstitle.txt "$SCRIPT_STORAGE_DIR/spdtitletext.js" SetSPDStatsTitle statstitle "$extraJScode" + rm -f /tmp/spdstatstitle.txt + sleep 2 Print_Output true "Database reset complete" "$WARN" + { + sleep 4 + timeDateNow="$(/bin/date +"%c")" + extraJScode='databaseResetDone = 0;' + echo "Stats were reset: $timeDateNow" > /tmp/spdstatstitle.txt + WriteStats_ToJS /tmp/spdstatstitle.txt "$SCRIPT_STORAGE_DIR/spdtitletext.js" SetSPDStatsTitle statstitle "$extraJScode" + rm -f /tmp/spdstatstitle.txt + } & fi } @@ -2155,7 +2195,7 @@ MainMenu(){ printf "a. AutoBW\\n\\n" printf "u. Check for updates\\n" printf "uf. Update %s with latest version (force update)\\n\\n" "$SCRIPT_NAME" - printf "rt. Reset %s database / delete all data\\n\\n" "$SCRIPT_NAME" + printf "rs. Reset %s database / delete all data\\n\\n" "$SCRIPT_NAME" printf "e. Exit %s\\n\\n" "$SCRIPT_NAME" printf "z. Uninstall %s\\n" "$SCRIPT_NAME" printf "\\n" @@ -2294,7 +2334,7 @@ MainMenu(){ PressEnter break ;; - rt) + rs) printf "\\n" if Check_Lock menu; then Menu_ResetDB diff --git a/spdstats_www.asp b/spdstats_www.asp index 5683aa0..6e84ad6 100644 --- a/spdstats_www.asp +++ b/spdstats_www.asp @@ -32,8 +32,9 @@ p{font-weight:bolder}thead.collapsible-jquery{color:#fff;padding:0;width:100%;bo diff --git a/spdstats_www.js b/spdstats_www.js index 7d4f174..28162e7 100644 --- a/spdstats_www.js +++ b/spdstats_www.js @@ -863,7 +863,7 @@ function initial(){ SetCurrentPage(); LoadCustomSettings(); show_menu(); - $j('#Time_Format').val(GetCookie('Time_Format','number')); + $j('#Time_Format').val(GetCookie('Time_Format', 'number')); ScriptUpdateLayout(); get_statstitle_file(); get_interfaces_file(); @@ -1240,7 +1240,11 @@ function update_spdtest(){ }); } -function PostSpeedTest(){ +/**-------------------------------------**/ +/** Added by Martinski W. [2024-Jul-15] **/ +/**------------------------------- -----**/ +function ClearSpdTableRows() +{ $j('#table_allinterfaces').empty(); $j('#rowautomaticspdtest').empty(); $j('#rowautospdprefserver').empty(); @@ -1251,8 +1255,16 @@ function PostSpeedTest(){ $j('#rowautospdprefserver').remove(); $j('#rowautospdprefserverselect').remove(); $j('#rowmanualspdtest').remove(); +} + +/**----------------------------------------**/ +/** Modified by Martinski W. [2024-Jul-15] **/ +/**----------------------------------------**/ +function PostSpeedTest() +{ + ClearSpdTableRows(); currentNoCharts = 0; - $j('#Time_Format').val(GetCookie('Time_Format','number')); + $j('#Time_Format').val(GetCookie('Time_Format', 'number')); get_statstitle_file(); setTimeout(get_interfaces_file,3000); } @@ -1557,15 +1569,30 @@ function get_interfaces_file(){ }); } -function get_statstitle_file(){ +/**----------------------------------------**/ +/** Modified by Martinski W. [2024-Jul-15] **/ +/**----------------------------------------**/ +let databaseResetDone = 0; +function get_statstitle_file() +{ $j.ajax({ url: '/ext/spdmerlin/spdtitletext.js', dataType: 'script', error: function(xhr){ - setTimeout(get_statstitle_file,1000); + setTimeout(get_statstitle_file, 2000); }, - success: function(){ + success: function() + { SetSPDStatsTitle(); + if (databaseResetDone === 1) + { + ClearSpdTableRows(); + currentNoCharts = 0; + $j('#Time_Format').val(GetCookie('Time_Format', 'number')); + get_interfaces_file(); + databaseResetDone += 1; + } + setTimeout(get_statstitle_file, 4000); } }); }