From 398d95cbfa80e31a58837ab9e65d02517d938362 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Tue, 10 Aug 2021 21:01:22 -0500 Subject: [PATCH 1/2] removed reliance on alias; wasn't intended to be left in --- http_check_telegram | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/http_check_telegram b/http_check_telegram index 6c1ffd3..2fd49a5 100755 --- a/http_check_telegram +++ b/http_check_telegram @@ -5,7 +5,6 @@ CONFIG=$1 #Alias DB command for easy future adjustments SQL='sqlite3' -alias sql="$SQL" # Check for dependencies if ! which $SQL >/dev/null 2>&1; then @@ -60,7 +59,7 @@ fi #DB functions db_create() { - sql $DB "CREATE TABLE problem ( + $SQL $DB "CREATE TABLE problem ( site TEXT UNIQUE NOT NULL, count INT NOT NULL DEFAULT 0, alert INT NOT NULL DEFAULT 0, @@ -68,18 +67,18 @@ db_create() { );" } db_addsite() { - sql $DB "INSERT OR IGNORE INTO problem (site) VALUES('$1');" + $SQL $DB "INSERT OR IGNORE INTO problem (site) VALUES('$1');" } db_inc() { - sql $DB "UPDATE problem SET count = count + 1 WHERE site = '$1' AND count < $MAX_COUNT;" - if [ $(sql $DB "SELECT count FROM problem WHERE site = '$1';") -ge $MAX_COUNT ]; then - sql $DB "UPDATE problem SET alert = 1 WHERE site = '$1';" + $SQL $DB "UPDATE problem SET count = count + 1 WHERE site = '$1' AND count < $MAX_COUNT;" + if [ $($SQL $DB "SELECT count FROM problem WHERE site = '$1';") -ge $MAX_COUNT ]; then + $SQL $DB "UPDATE problem SET alert = 1 WHERE site = '$1';" fi } db_dec() { - sql $DB "UPDATE problem SET count = count - 1 WHERE site = '$1' AND count > 0;" - if [ $(sql $DB "SELECT count FROM problem WHERE site = '$1';") -le 0 ]; then - sql $DB "UPDATE problem SET alert = 0 WHERE site = '$1';" + $SQL $DB "UPDATE problem SET count = count - 1 WHERE site = '$1' AND count > 0;" + if [ $($SQL $DB "SELECT count FROM problem WHERE site = '$1';") -le 0 ]; then + $SQL $DB "UPDATE problem SET alert = 0 WHERE site = '$1';" fi } @@ -156,27 +155,27 @@ done || exit 2 ### fi # Find down sites without sent alerts -unsent=$(sql $DB "SELECT count(*) FROM problem WHERE alert = 1 AND sent = 0;") -cleared=$(sql $DB "SELECT count(*) FROM problem WHERE alert = 0 AND sent = 1;") -down=$(sql $DB "SELECT count(*) FROM problem WHERE alert = 1;") -downlist=$(sql $DB "SELECT site FROM problem WHERE alert = 1;" | perl -p -e 's/\n/%0A/g') +unsent=$($SQL $DB "SELECT count(*) FROM problem WHERE alert = 1 AND sent = 0;") +cleared=$($SQL $DB "SELECT count(*) FROM problem WHERE alert = 0 AND sent = 1;") +down=$($SQL $DB "SELECT count(*) FROM problem WHERE alert = 1;") +downlist=$($SQL $DB "SELECT site FROM problem WHERE alert = 1;" | perl -p -e 's/\n/%0A/g') # If ANY sites have unsent alerts, send a list of ALL sites in 'alert' state if [ $unsent -gt 0 ]; then ALERT="${HEADER}%0ASites Down:%0A${downlist}" send_message "$ALERT" && printf '%s\n' "Problems found. Message sent via Telegram bot" >&2 || printf '%s\n' "Problems found. Message sending has failed" - sql $DB "UPDATE problem SET sent = 1 WHERE sent = 0 AND alert = 1;" #Prevents resending messages + $SQL $DB "UPDATE problem SET sent = 1 WHERE sent = 0 AND alert = 1;" #Prevents resending messages fi # If ALL sites have cleared alerts, send an all-clear if [ $cleared -gt 0 ]; then - clearlist=$(sql $DB "SELECT site FROM problem WHERE alert = 0 AND sent = 1;" | perl -p -e 's/\n/%0A/g') + clearlist=$($SQL $DB "SELECT site FROM problem WHERE alert = 0 AND sent = 1;" | perl -p -e 's/\n/%0A/g') CLEARED="${HEADER}%0AThese HTTP alerts have cleared:%0A${clearlist}" if [ $down -gt 0 ]; then CLEARED="${CLEARED}%0ASites still down:%0A${downlist}" fi send_message "$CLEARED" && printf '%s\n' "A site's alert status has cleared. Message sent via Telegram bot" >&2 || printf '%s\n' "A site's alert status has cleared. Message sending has failed" - sql $DB "UPDATE problem SET sent = 0 WHERE sent = 1 AND alert = 0;" #Prevents resending messages + $SQL $DB "UPDATE problem SET sent = 0 WHERE sent = 1 AND alert = 0;" #Prevents resending messages fi exit 0 From 52a408a513ccc50872f126303ed6d0ad821fa6f7 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Tue, 10 Aug 2021 23:10:15 -0500 Subject: [PATCH 2/2] Added code to prune DB --- http_check_telegram | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/http_check_telegram b/http_check_telegram index 2fd49a5..ec861c3 100755 --- a/http_check_telegram +++ b/http_check_telegram @@ -64,11 +64,14 @@ db_create() { count INT NOT NULL DEFAULT 0, alert INT NOT NULL DEFAULT 0, sent INT NOT NULL DEFAULT 0 - );" + );" || return 1 } db_addsite() { $SQL $DB "INSERT OR IGNORE INTO problem (site) VALUES('$1');" } +db_rmsite() { + $SQL $DB "DELETE FROM problem WHERE site = '$1';" +} db_inc() { $SQL $DB "UPDATE problem SET count = count + 1 WHERE site = '$1' AND count < $MAX_COUNT;" if [ $($SQL $DB "SELECT count FROM problem WHERE site = '$1';") -ge $MAX_COUNT ]; then @@ -99,6 +102,10 @@ get_http_code() { fi } +read_checklist() { + grep -v "^#\|^$" $CHKLIST | sort | uniq || return 1 +} + print_time() { date '+%Y-%m-%d %H:%M:%S' } @@ -110,6 +117,14 @@ FAILURE=false # Create database if it doesn't exist if [ -f $DB ]; then printf '%s\n' "Database found: $DB" + # Remove stale entries in DB no longer found in check list + printf '%s\n' "Pruning database..." + for row in $($SQL $DB "SELECT site FROM problem;"); do + if ! read_checklist | grep $row >/dev/null 2>&1; then + db_rmsite "$row" && printf '%s\n' "Removing old record: $row" + fi + done + printf '%s\n' "Database pruning complete." else printf '%s' "Database not found at '$DB'\nCreating DB ... " if db_create; then @@ -122,7 +137,7 @@ else fi # Check HTTP codes -for site in $(grep -v "^#\|^$" $CHKLIST | sort | uniq); do +for site in $(read_checklist); do db_addsite $site respcode=$(get_http_code $site) case $respcode in @@ -149,11 +164,6 @@ for site in $(grep -v "^#\|^$" $CHKLIST | sort | uniq); do esac done || exit 2 -### # Send compiled message if any problems were found -### if [ "$FAILURE" = "true" ]; then -### send_message "$MSG" && printf '%s\n' "Problems found. Message sent via Telegram bot" >&2 || printf '%s\n' "Problems found. Message sending has failed" -### fi - # Find down sites without sent alerts unsent=$($SQL $DB "SELECT count(*) FROM problem WHERE alert = 1 AND sent = 0;") cleared=$($SQL $DB "SELECT count(*) FROM problem WHERE alert = 0 AND sent = 1;")