From 30ecee6ea03e0d21199f48366fb5bf91f1fca561 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Sat, 6 Jan 2024 22:19:09 +0000 Subject: [PATCH 1/7] Watchdog and multiple channels support --- CHANGELOG.md | 32 ++++++ LICENSE | 2 +- install.sh | 214 ++++++++++++++++++++++++++++++++--- package.json | 2 +- src/lib.sh | 209 +++++++++++++++++++++++++++++++--- src/setup.sh | 5 +- templates/docker-compose.yml | 6 +- updated | 2 +- 8 files changed, 435 insertions(+), 37 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b46fc43 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,32 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.8.0] - 2024-01-06 + +### Added + +- Support for command line switches +- Support for development and production update channels +- Watchdog called from cron +- Status of watchdog and its logs + +### Changed + +- Main menu status show overal calculated status instead of Nightscout container + +### Fixed + +- More memory and memory limits config (in template) for MongoDB + +## [1.7.0] - 2023-10-20 + +### Added + +- UI shows verison number +- split betwen conatainer update and restart diff --git a/LICENSE b/LICENSE index 7e23f2c..66cba3b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2023 Dominik Dzienia +Copyright 2023-2004 Dominik Dzienia Attribution-NonCommercial-NoDerivatives 4.0 International diff --git a/install.sh b/install.sh index 4a7627b..0f4e91a 100644 --- a/install.sh +++ b/install.sh @@ -1,6 +1,6 @@ #!/bin/bash -### version: 1.7.0 +### version: 1.8.0 # ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.# # Nightscout Mikr.us setup script # @@ -31,12 +31,18 @@ ENV_FILE_DEP=/srv/nightscout/config/deployment.env DOCKER_COMPOSE_FILE=/srv/nightscout/config/docker-compose.yml PROFANITY_DB_FILE=/srv/nightscout/data/profanity.db RESERVED_DB_FILE=/srv/nightscout/data/reserved.db +WATCHDOG_STATUS_FILE=/srv/nightscout/data/watchdog_status +WATCHDOG_TIME_FILE=/srv/nightscout/data/watchdog_time +WATCHDOG_LOG_FILE=/srv/nightscout/data/watchdog.log +WATCHDOG_CRON_LOG=/srv/nightscout/data/watchdog-cron.log +UPDATE_CHANNEL_FILE=/srv/nightscout/data/update_channel MONGO_DB_DIR=/srv/nightscout/data/mongodb TOOL_FILE=/srv/nightscout/tools/nightscout-tool TOOL_LINK=/usr/bin/nightscout-tool UPDATES_DIR=/srv/nightscout/updates -SCRIPT_VERSION="1.7.0" #auto-update -SCRIPT_BUILD_TIME="2023.10.20" #auto-update +UPDATE_CHANNEL=master +SCRIPT_VERSION="1.8.0" #auto-update +SCRIPT_BUILD_TIME="2024.01.06" #auto-update #======================================= # SETUP @@ -423,6 +429,11 @@ check_nano() { add_if_not_ok "Text Editor" "nano" } +check_dateutils() { + dateutils.ddiff --version >/dev/null 2>&1 + add_if_not_ok "Date Utils" "dateutils" +} + setup_packages() { # shellcheck disable=SC2145 # shellcheck disable=SC2068 @@ -533,6 +544,18 @@ setup_firewall_for_ns() { fi } +install_cron() { + local croncmd="$TOOL_LINK -w > $WATCHDOG_CRON_LOG 2>&1" + local cronjob="*/5 * * * * $croncmd" + msgok "Configuring watchdog..." + ( crontab -l | grep -v -F "$croncmd" || : ; echo "$cronjob" ) | crontab - +} + +uninstall_cron() { + local croncmd="nightscout-tool" + ( crontab -l | grep -v -F "$croncmd" ) | crontab - +} + get_docker_status() { local ID=$(docker ps -a --no-trunc --filter name="^$1" --format '{{ .ID }}') if [[ "$ID" =~ [0-9a-fA-F]{12,} ]]; then @@ -582,18 +605,18 @@ source_admin() { } download_conf() { - download_if_not_exists "deployment config" $ENV_FILE_DEP https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/deployment.env - download_if_not_exists "nightscout config" $ENV_FILE_NS https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/nightscout.env - download_if_not_exists "docker compose file" $DOCKER_COMPOSE_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/docker-compose.yml - download_if_not_exists "profanity database" $PROFANITY_DB_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/profanity.db - download_if_not_exists "reservation database" $RESERVED_DB_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/reserved.db + download_if_not_exists "deployment config" $ENV_FILE_DEP "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/deployment.env" + download_if_not_exists "nightscout config" $ENV_FILE_NS "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/nightscout.env" + download_if_not_exists "docker compose file" $DOCKER_COMPOSE_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/docker-compose.yml" + download_if_not_exists "profanity database" $PROFANITY_DB_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/profanity.db" + download_if_not_exists "reservation database" $RESERVED_DB_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/reserved.db" } download_tools() { - download_if_not_exists "update stamp" "$UPDATES_DIR/updated" https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/updated + download_if_not_exists "update stamp" "$UPDATES_DIR/updated" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/updated" if ! [[ -f $TOOL_FILE ]]; then - download_if_not_exists "nightscout-tool file" $TOOL_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/install.sh + download_if_not_exists "nightscout-tool file" $TOOL_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/install.sh" local timestamp=$(date +%s) echo "$timestamp" >"$UPDATES_DIR/timestamp" else @@ -624,7 +647,7 @@ update_if_needed() { if [ $((timestamp - lastUpdate)) -gt $((60 * 60 * 24)) ] || [ $# -eq 1 ]; then echo "$timestamp" >"$UPDATES_DIR/timestamp" - local onlineUpdated="$(curl -fsSL "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/updated")" + local onlineUpdated="$(curl -fsSL "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/updated")" local lastUpdate=$(cat "$UPDATES_DIR/updated") if [ "$onlineUpdated" == "$lastUpdate" ] || [ $# -eq 0 ]; then msgok "Scripts and config files are up to date" @@ -633,10 +656,10 @@ update_if_needed() { fi else ohai "Updating scripts and config files" - curl -fsSL -o "$UPDATES_DIR/install.sh" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/install.sh" - curl -fsSL -o "$UPDATES_DIR/deployment.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/deployment.env" - curl -fsSL -o "$UPDATES_DIR/nightscout.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/nightscout.env" - curl -fsSL -o "$UPDATES_DIR/docker-compose.yml" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/docker-compose.yml" + curl -fsSL -o "$UPDATES_DIR/install.sh" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/install.sh" + curl -fsSL -o "$UPDATES_DIR/deployment.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/deployment.env" + curl -fsSL -o "$UPDATES_DIR/nightscout.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/nightscout.env" + curl -fsSL -o "$UPDATES_DIR/docker-compose.yml" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/docker-compose.yml" curl -fsSL -o "$PROFANITY_DB_FILE" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/profanity.db" curl -fsSL -o "$RESERVED_DB_FILE" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/reserved.db" @@ -1022,6 +1045,16 @@ get_container_status() { fi } +get_container_status_code() { + local ID=$(docker ps -a --no-trunc --filter name="^$1$" --format '{{ .ID }}') + if [[ "$ID" =~ [0-9a-fA-F]{12,} ]]; then + local status=$(docker inspect "$ID" | jq -r ".[0].State.Status") + echo "$status" + else + echo "unknown" + fi +} + show_logs() { local col=$((COLUMNS - 10)) local rws=$((LINES - 3)) @@ -1207,6 +1240,7 @@ uninstall_menu() { if ! [ $? -eq 1 ]; then docker_compose_down dialog --title " Odinstalowanie" --infobox "\n Usuwanie plików\n ... Proszę czekać ..." 6 32 + uninstall_cron rm -r "${MONGO_DB_DIR:?}/data" rm -r "${CONFIG_ROOT_DIR:?}" rm "$TOOL_LINK" @@ -1248,7 +1282,7 @@ main_menu() { local quickStatus=$(center_text "Nightscout: $(get_container_status 'ns-server')" 55) local quickVersion=$(center_text "Wersja: $ns_tag" 55) local quickDomain=$(center_text "Domena: $(get_domain_status 'ns-server')" 55) - local CHOICE=$(whiptail --title "Zarządzanie Nightscoutem :: $SCRIPT_VERSION" --menu "\n$quickStatus\n$quickVersion\n$quickDomain\n" 20 60 9 \ + local CHOICE=$(whiptail --title "Zarządzanie Nightscoutem :: $SCRIPT_VERSION" --menu "\n$quickStatus\n$quickVersion\n$quickDomain\n" 20 60 9 \ "1)" "Status kontenerów i logi" \ "2)" "Pokaż port i API SECRET" \ "S)" "Aktualizuj system" \ @@ -1338,11 +1372,157 @@ install_or_menu() { fi } +watchdog_check() { + echo "Nightscout Watchdog mode" + + WATCHDOG_LAST_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + WATCHDOG_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + WATCHDOG_LAST_STATUS="unknown" + WATCHDOG_STATUS="unknown" + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + echo "Found $WATCHDOG_TIME_FILE" + WATCHDOG_LAST_TIME=$(cat $WATCHDOG_TIME_FILE) + else + echo "First watchdog run" + fi + + if [[ -f $WATCHDOG_STATUS_FILE ]]; then + echo "Found $WATCHDOG_STATUS_FILE" + WATCHDOG_LAST_STATUS=$(cat $WATCHDOG_STATUS_FILE) + fi + + local STATUS_AGO=$(dateutils.ddiff "$WATCHDOG_TIME" "$WATCHDOG_LAST_TIME" -f '%S') + + if [ "$STATUS_AGO" -gt 900 ]; then + echo "Watchdog last status is $STATUS_AGO seconds old, ignoring" + WATCHDOG_LAST_STATUS="unknown" + fi + + local NS_STATUS=$(get_container_status_code 'ns-server') + local DB_STATUS=$(get_container_status_code 'ns-database') + local COMBINED_STATUS="$NS_STATUS $DB_STATUS" + + echo "Server container: $NS_STATUS" + echo "Database container: $DB_STATUS" + + if [ "$COMBINED_STATUS" = "running running" ]; then + echo "Will check page contents" + local ns_external_port=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_PORT") + local html=$(curl -s "127.0.0.1:$ns_external_port") + + WATCHDOG_STATUS="detection_failed" + + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + echo "Nightscout is running" + WATCHDOG_STATUS="ok" + fi + + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + echo "Nightscout is crashed, restarting..." + WATCHDOG_STATUS="restart" + if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then + docker restart 'ns-server' + echo "...done" + fi + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + echo "Nightscout is still restarting..." + WATCHDOG_STATUS="restarting" + fi + + else + if [ "$NS_STATUS" = "restarting" ] || [ "$DB_STATUS" = "restarting" ]; then + WATCHDOG_STATUS="restarting" + else + WATCHDOG_STATUS="not_running" + fi + fi + + echo "Watchdog observation: $WATCHDOG_STATUS" + + # if [ "$WATCHDOG_LAST_STATUS" != "$WATCHDOG_STATUS" ]; then + echo "$WATCHDOG_TIME [$WATCHDOG_STATUS]" >>$WATCHDOG_LOG_FILE + LOGSIZE=$(wc -l <$WATCHDOG_LOG_FILE) + if [ "$LOGSIZE" -gt 1000 ]; then + tail -1000 $WATCHDOG_LOG_FILE >"$WATCHDOG_LOG_FILE.tmp" + mv -f "$WATCHDOG_LOG_FILE.tmp" "$WATCHDOG_LOG_FILE" + fi + # fi + + echo "$WATCHDOG_TIME" >$WATCHDOG_TIME_FILE + echo "$WATCHDOG_STATUS" >$WATCHDOG_STATUS_FILE + + exit 0 +} + +load_update_channel() { + if [[ -f $UPDATE_CHANNEL_FILE ]]; then + UPDATE_CHANNEL=$(cat $UPDATE_CHANNEL_FILE) + msgok "Loaded update channel: $UPDATE_CHANNEL" + fi +} + +parse_commandline_args() { + + load_update_channel + + CMDARGS=$(getopt --quiet -o wvdp --long watchdog,version,develop,production -n 'nightscout-tool' -- "$@") + + # shellcheck disable=SC2181 + if [ $? != 0 ]; then + echo "Invalid arguments: " "$@" >&2 + exit 1 + fi + + # Note the quotes around '$TEMP': they are essential! + eval set -- "$CMDARGS" + + WATCHDOGMODE=false + while true; do + case "$1" in + -w | --watchdog) + WATCHDOGMODE=true + shift + ;; + -v | --version) + echo "$SCRIPT_VERSION" + exit 0 + ;; + -d | --develop) + warn "Switching to DEVELOP update channel" + UPDATE_CHANNEL=develop + echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE + shift + ;; + -p | --production) + warn "Switching to PRODUCTION update channel" + UPDATE_CHANNEL=master + echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE + shift + ;; + --) + shift + break + ;; + *) break ;; + esac + done + + if [ "$WATCHDOGMODE" = "true" ]; then + watchdog_check + fi + +} + #======================================= # MAIN SCRIPT #======================================= +parse_commandline_args "$@" # check_interactive check_git check_docker @@ -1350,6 +1530,7 @@ check_docker_compose check_jq check_ufw check_nano +check_dateutils setup_packages setup_node check_dotenv @@ -1360,6 +1541,7 @@ download_tools update_if_needed setup_firewall +install_cron source_admin diff --git a/package.json b/package.json index 57f87f7..778e5d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dlvoy/ns-installer-mikrus", - "version": "1.7.0", + "version": "1.8.0", "description": "Nightscout installer for mikr.us VPS", "main": "index.js", "scripts": { diff --git a/src/lib.sh b/src/lib.sh index df8a3a7..02dd810 100644 --- a/src/lib.sh +++ b/src/lib.sh @@ -15,12 +15,18 @@ ENV_FILE_DEP=/srv/nightscout/config/deployment.env DOCKER_COMPOSE_FILE=/srv/nightscout/config/docker-compose.yml PROFANITY_DB_FILE=/srv/nightscout/data/profanity.db RESERVED_DB_FILE=/srv/nightscout/data/reserved.db +WATCHDOG_STATUS_FILE=/srv/nightscout/data/watchdog_status +WATCHDOG_TIME_FILE=/srv/nightscout/data/watchdog_time +WATCHDOG_LOG_FILE=/srv/nightscout/data/watchdog.log +WATCHDOG_CRON_LOG=/srv/nightscout/data/watchdog-cron.log +UPDATE_CHANNEL_FILE=/srv/nightscout/data/update_channel MONGO_DB_DIR=/srv/nightscout/data/mongodb TOOL_FILE=/srv/nightscout/tools/nightscout-tool TOOL_LINK=/usr/bin/nightscout-tool UPDATES_DIR=/srv/nightscout/updates -SCRIPT_VERSION="1.7.0" #auto-update -SCRIPT_BUILD_TIME="2023.10.20" #auto-update +UPDATE_CHANNEL=master +SCRIPT_VERSION="1.8.0" #auto-update +SCRIPT_BUILD_TIME="2024.01.06" #auto-update #======================================= # SETUP @@ -407,6 +413,11 @@ check_nano() { add_if_not_ok "Text Editor" "nano" } +check_dateutils() { + dateutils.ddiff --version >/dev/null 2>&1 + add_if_not_ok "Date Utils" "dateutils" +} + setup_packages() { # shellcheck disable=SC2145 # shellcheck disable=SC2068 @@ -517,6 +528,18 @@ setup_firewall_for_ns() { fi } +install_cron() { + local croncmd="$TOOL_LINK -w > $WATCHDOG_CRON_LOG 2>&1" + local cronjob="*/5 * * * * $croncmd" + msgok "Configuring watchdog..." + ( crontab -l | grep -v -F "$croncmd" || : ; echo "$cronjob" ) | crontab - +} + +uninstall_cron() { + local croncmd="nightscout-tool" + ( crontab -l | grep -v -F "$croncmd" ) | crontab - +} + get_docker_status() { local ID=$(docker ps -a --no-trunc --filter name="^$1" --format '{{ .ID }}') if [[ "$ID" =~ [0-9a-fA-F]{12,} ]]; then @@ -566,18 +589,18 @@ source_admin() { } download_conf() { - download_if_not_exists "deployment config" $ENV_FILE_DEP https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/deployment.env - download_if_not_exists "nightscout config" $ENV_FILE_NS https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/nightscout.env - download_if_not_exists "docker compose file" $DOCKER_COMPOSE_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/docker-compose.yml - download_if_not_exists "profanity database" $PROFANITY_DB_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/profanity.db - download_if_not_exists "reservation database" $RESERVED_DB_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/reserved.db + download_if_not_exists "deployment config" $ENV_FILE_DEP "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/deployment.env" + download_if_not_exists "nightscout config" $ENV_FILE_NS "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/nightscout.env" + download_if_not_exists "docker compose file" $DOCKER_COMPOSE_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/docker-compose.yml" + download_if_not_exists "profanity database" $PROFANITY_DB_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/profanity.db" + download_if_not_exists "reservation database" $RESERVED_DB_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/reserved.db" } download_tools() { - download_if_not_exists "update stamp" "$UPDATES_DIR/updated" https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/updated + download_if_not_exists "update stamp" "$UPDATES_DIR/updated" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/updated" if ! [[ -f $TOOL_FILE ]]; then - download_if_not_exists "nightscout-tool file" $TOOL_FILE https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/install.sh + download_if_not_exists "nightscout-tool file" $TOOL_FILE "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/install.sh" local timestamp=$(date +%s) echo "$timestamp" >"$UPDATES_DIR/timestamp" else @@ -608,7 +631,7 @@ update_if_needed() { if [ $((timestamp - lastUpdate)) -gt $((60 * 60 * 24)) ] || [ $# -eq 1 ]; then echo "$timestamp" >"$UPDATES_DIR/timestamp" - local onlineUpdated="$(curl -fsSL "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/updated")" + local onlineUpdated="$(curl -fsSL "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/updated")" local lastUpdate=$(cat "$UPDATES_DIR/updated") if [ "$onlineUpdated" == "$lastUpdate" ] || [ $# -eq 0 ]; then msgok "Scripts and config files are up to date" @@ -617,10 +640,10 @@ update_if_needed() { fi else ohai "Updating scripts and config files" - curl -fsSL -o "$UPDATES_DIR/install.sh" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/install.sh" - curl -fsSL -o "$UPDATES_DIR/deployment.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/deployment.env" - curl -fsSL -o "$UPDATES_DIR/nightscout.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/nightscout.env" - curl -fsSL -o "$UPDATES_DIR/docker-compose.yml" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/master/templates/docker-compose.yml" + curl -fsSL -o "$UPDATES_DIR/install.sh" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/install.sh" + curl -fsSL -o "$UPDATES_DIR/deployment.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/deployment.env" + curl -fsSL -o "$UPDATES_DIR/nightscout.env" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/nightscout.env" + curl -fsSL -o "$UPDATES_DIR/docker-compose.yml" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/$UPDATE_CHANNEL/templates/docker-compose.yml" curl -fsSL -o "$PROFANITY_DB_FILE" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/profanity.db" curl -fsSL -o "$RESERVED_DB_FILE" "https://gitea.dzienia.pl/shared/mikrus-installer/raw/branch/profanity/templates/reserved.db" @@ -1006,6 +1029,16 @@ get_container_status() { fi } +get_container_status_code() { + local ID=$(docker ps -a --no-trunc --filter name="^$1$" --format '{{ .ID }}') + if [[ "$ID" =~ [0-9a-fA-F]{12,} ]]; then + local status=$(docker inspect "$ID" | jq -r ".[0].State.Status") + echo "$status" + else + echo "unknown" + fi +} + show_logs() { local col=$((COLUMNS - 10)) local rws=$((LINES - 3)) @@ -1191,6 +1224,7 @@ uninstall_menu() { if ! [ $? -eq 1 ]; then docker_compose_down dialog --title " Odinstalowanie" --infobox "\n Usuwanie plików\n ... Proszę czekać ..." 6 32 + uninstall_cron rm -r "${MONGO_DB_DIR:?}/data" rm -r "${CONFIG_ROOT_DIR:?}" rm "$TOOL_LINK" @@ -1232,7 +1266,7 @@ main_menu() { local quickStatus=$(center_text "Nightscout: $(get_container_status 'ns-server')" 55) local quickVersion=$(center_text "Wersja: $ns_tag" 55) local quickDomain=$(center_text "Domena: $(get_domain_status 'ns-server')" 55) - local CHOICE=$(whiptail --title "Zarządzanie Nightscoutem :: $SCRIPT_VERSION" --menu "\n$quickStatus\n$quickVersion\n$quickDomain\n" 20 60 9 \ + local CHOICE=$(whiptail --title "Zarządzanie Nightscoutem :: $SCRIPT_VERSION" --menu "\n$quickStatus\n$quickVersion\n$quickDomain\n" 20 60 9 \ "1)" "Status kontenerów i logi" \ "2)" "Pokaż port i API SECRET" \ "S)" "Aktualizuj system" \ @@ -1321,3 +1355,148 @@ install_or_menu() { main_menu fi } + +watchdog_check() { + echo "Nightscout Watchdog mode" + + WATCHDOG_LAST_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + WATCHDOG_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + WATCHDOG_LAST_STATUS="unknown" + WATCHDOG_STATUS="unknown" + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + echo "Found $WATCHDOG_TIME_FILE" + WATCHDOG_LAST_TIME=$(cat $WATCHDOG_TIME_FILE) + else + echo "First watchdog run" + fi + + if [[ -f $WATCHDOG_STATUS_FILE ]]; then + echo "Found $WATCHDOG_STATUS_FILE" + WATCHDOG_LAST_STATUS=$(cat $WATCHDOG_STATUS_FILE) + fi + + local STATUS_AGO=$(dateutils.ddiff "$WATCHDOG_TIME" "$WATCHDOG_LAST_TIME" -f '%S') + + if [ "$STATUS_AGO" -gt 900 ]; then + echo "Watchdog last status is $STATUS_AGO seconds old, ignoring" + WATCHDOG_LAST_STATUS="unknown" + fi + + local NS_STATUS=$(get_container_status_code 'ns-server') + local DB_STATUS=$(get_container_status_code 'ns-database') + local COMBINED_STATUS="$NS_STATUS $DB_STATUS" + + echo "Server container: $NS_STATUS" + echo "Database container: $DB_STATUS" + + if [ "$COMBINED_STATUS" = "running running" ]; then + echo "Will check page contents" + local ns_external_port=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_PORT") + local html=$(curl -s "127.0.0.1:$ns_external_port") + + WATCHDOG_STATUS="detection_failed" + + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + echo "Nightscout is running" + WATCHDOG_STATUS="ok" + fi + + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + echo "Nightscout is crashed, restarting..." + WATCHDOG_STATUS="restart" + if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then + docker restart 'ns-server' + echo "...done" + fi + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + echo "Nightscout is still restarting..." + WATCHDOG_STATUS="restarting" + fi + + else + if [ "$NS_STATUS" = "restarting" ] || [ "$DB_STATUS" = "restarting" ]; then + WATCHDOG_STATUS="restarting" + else + WATCHDOG_STATUS="not_running" + fi + fi + + echo "Watchdog observation: $WATCHDOG_STATUS" + + # if [ "$WATCHDOG_LAST_STATUS" != "$WATCHDOG_STATUS" ]; then + echo "$WATCHDOG_TIME [$WATCHDOG_STATUS]" >>$WATCHDOG_LOG_FILE + LOGSIZE=$(wc -l <$WATCHDOG_LOG_FILE) + if [ "$LOGSIZE" -gt 1000 ]; then + tail -1000 $WATCHDOG_LOG_FILE >"$WATCHDOG_LOG_FILE.tmp" + mv -f "$WATCHDOG_LOG_FILE.tmp" "$WATCHDOG_LOG_FILE" + fi + # fi + + echo "$WATCHDOG_TIME" >$WATCHDOG_TIME_FILE + echo "$WATCHDOG_STATUS" >$WATCHDOG_STATUS_FILE + + exit 0 +} + +load_update_channel() { + if [[ -f $UPDATE_CHANNEL_FILE ]]; then + UPDATE_CHANNEL=$(cat $UPDATE_CHANNEL_FILE) + msgok "Loaded update channel: $UPDATE_CHANNEL" + fi +} + +parse_commandline_args() { + + load_update_channel + + CMDARGS=$(getopt --quiet -o wvdp --long watchdog,version,develop,production -n 'nightscout-tool' -- "$@") + + # shellcheck disable=SC2181 + if [ $? != 0 ]; then + echo "Invalid arguments: " "$@" >&2 + exit 1 + fi + + # Note the quotes around '$TEMP': they are essential! + eval set -- "$CMDARGS" + + WATCHDOGMODE=false + while true; do + case "$1" in + -w | --watchdog) + WATCHDOGMODE=true + shift + ;; + -v | --version) + echo "$SCRIPT_VERSION" + exit 0 + ;; + -d | --develop) + warn "Switching to DEVELOP update channel" + UPDATE_CHANNEL=develop + echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE + shift + ;; + -p | --production) + warn "Switching to PRODUCTION update channel" + UPDATE_CHANNEL=master + echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE + shift + ;; + --) + shift + break + ;; + *) break ;; + esac + done + + if [ "$WATCHDOGMODE" = "true" ]; then + watchdog_check + fi + +} diff --git a/src/setup.sh b/src/setup.sh index 716dae0..ba22697 100755 --- a/src/setup.sh +++ b/src/setup.sh @@ -1,6 +1,6 @@ #!/bin/bash -### version: 1.7.0 +### version: 1.8.0 # ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.# # Nightscout Mikr.us setup script # @@ -26,6 +26,7 @@ if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi # MAIN SCRIPT #======================================= +parse_commandline_args "$@" # check_interactive check_git check_docker @@ -33,6 +34,7 @@ check_docker_compose check_jq check_ufw check_nano +check_dateutils setup_packages setup_node check_dotenv @@ -43,6 +45,7 @@ download_tools update_if_needed setup_firewall +install_cron source_admin diff --git a/templates/docker-compose.yml b/templates/docker-compose.yml index 5a04ee5..4a2a152 100644 --- a/templates/docker-compose.yml +++ b/templates/docker-compose.yml @@ -1,4 +1,4 @@ -### version: 1.0.1 +### version: 1.8.0 version: "2" @@ -9,10 +9,12 @@ networks: services: mongodb: container_name: ns-database - mem_limit: 200m + mem_limit: 400m mem_reservation: 100M restart: always image: "bitnami/mongodb:${NS_MONGODB_TAG}" + environment: + - MONGODB_EXTRA_FLAGS=--wiredTigerCacheSizeGB=0.4 volumes: - "${NS_DATA_DIR}/mongodb:/bitnami/mongodb" ports: diff --git a/updated b/updated index 68a2f7f..12b7ee5 100644 --- a/updated +++ b/updated @@ -1 +1 @@ -2023-10-20T19:45:50.827Z \ No newline at end of file +2024-01-06T21:57:56.813Z \ No newline at end of file -- 2.38.5 From c691d05f5b104e83780b7de098c95472b1628542 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Fri, 27 Oct 2023 07:27:00 +0000 Subject: [PATCH 2/7] Updated status page URL --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e53e602..876222c 100644 --- a/README.md +++ b/README.md @@ -20,5 +20,5 @@ Nie opieraj decyzji terapeutycznych na podstawie wskazań tego narzędzia! **Twórcy tego narzędzia NIE SĄ administratorami Mikr.us-ów ani Hetznera!** - W razie problemów z dostępnością serwera najpierw sprawdź [status Mikr.us-a](https://status.mikr.us/status/mikrus). Gdyby strona status nie działała - sprawdź czy przypadkiem [Hetzner nie ma problemów](https://status.hetzner.com) -- Instalator także posiada swoją [stronę statusu](https://dzieniaalive.bieda.it/status/td) - ale dotyczy ona tylko **skryptów instalacyjnych** a NIE dostępności TWOJEGO Nightscouta po instalacji. +- Instalator także posiada swoją [stronę statusu](https://status.cukrzycowy.pl/) - ale dotyczy ona tylko **skryptów instalacyjnych** a NIE dostępności TWOJEGO Nightscouta po instalacji. -- 2.38.5 From 261d23c50ca19f461ea1a52e57eba05100a84480 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Sun, 7 Jan 2024 13:34:06 +0000 Subject: [PATCH 3/7] Improved watchdog statuses --- CHANGELOG.md | 4 +- install.sh | 225 ++++++++++++++++++++++++++++++++++++++++++++------- src/lib.sh | 224 +++++++++++++++++++++++++++++++++++++++++++------- src/setup.sh | 1 + updated | 2 +- 5 files changed, 396 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b46fc43..1883bc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,6 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] - ## [1.8.0] - 2024-01-06 ### Added @@ -18,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Main menu status show overal calculated status instead of Nightscout container +- Main menu status show live calculated status instead of Nightscout container status ### Fixed diff --git a/install.sh b/install.sh index 0f4e91a..01a6c21 100644 --- a/install.sh +++ b/install.sh @@ -42,7 +42,7 @@ TOOL_LINK=/usr/bin/nightscout-tool UPDATES_DIR=/srv/nightscout/updates UPDATE_CHANNEL=master SCRIPT_VERSION="1.8.0" #auto-update -SCRIPT_BUILD_TIME="2024.01.06" #auto-update +SCRIPT_BUILD_TIME="2024.01.07" #auto-update #======================================= # SETUP @@ -132,8 +132,10 @@ tty_reset="$(tty_escape 0)" emoji_check="\U2705" emoji_ok="\U1F197" emoji_err="\U274C" +emoji_note="\U1F4A1" uni_bullet=" $(printf '\u2022') " +uni_copyright="$(printf '\uA9\uFE0F')" uni_bullet_pad=" " uni_exit=" $(printf '\U274C') Wyjdź " @@ -152,6 +154,9 @@ uni_confirm_ed=" $(printf '\U1F4DD') Edytuj " uni_install=" $(printf '\U1F680') Instaluj " uni_resign=" $(printf '\U1F6AB') Rezygnuję " +uni_ns_ok="$(printf '\U1F7E2') działa" +uni_watchdog_ok="$(printf '\U1F415') Nightscout działa" + #======================================= # UTILS #======================================= @@ -179,6 +184,11 @@ msgok() { printf "$emoji_ok $1\n" } +msgnote() { + # shellcheck disable=SC2059 + printf "$emoji_note $1\n" +} + msgcheck() { # shellcheck disable=SC2059 printf "$emoji_check $1\n" @@ -545,15 +555,18 @@ setup_firewall_for_ns() { } install_cron() { - local croncmd="$TOOL_LINK -w > $WATCHDOG_CRON_LOG 2>&1" - local cronjob="*/5 * * * * $croncmd" - msgok "Configuring watchdog..." - ( crontab -l | grep -v -F "$croncmd" || : ; echo "$cronjob" ) | crontab - + local croncmd="$TOOL_LINK -w > $WATCHDOG_CRON_LOG 2>&1" + local cronjob="*/5 * * * * $croncmd" + msgok "Configuring watchdog..." + ( + crontab -l | grep -v -F "$croncmd" || : + echo "$cronjob" + ) | crontab - } uninstall_cron() { - local croncmd="nightscout-tool" - ( crontab -l | grep -v -F "$croncmd" ) | crontab - + local croncmd="nightscout-tool" + (crontab -l | grep -v -F "$croncmd") | crontab - } get_docker_status() { @@ -758,7 +771,7 @@ update_if_needed() { about_dialog() { okdlg "O tym narzędziu..." \ - "$(printf '\U1F9D1') (c) 2023 Dominik Dzienia\n$(printf '\U1F4E7') dominik.dzienia@gmail.com\n\n$(printf '\U1F3DB') To narzędzie jest dystrybuowane na licencji CC BY-NC-ND 4.0\nhttps://creativecommons.org/licenses/by-nc-nd/4.0/deed.pl\n\nwersja: $SCRIPT_VERSION ($SCRIPT_BUILD_TIME)" + "$(printf '\U1F9D1') (c) 2023 Dominik Dzienia\n$(printf '\U1F4E7') dominik.dzienia@gmail.com\n\n$(printf '\U1F3DB') To narzędzie jest dystrybuowane na licencji CC BY-NC-ND 4.0\nhttps://creativecommons.org/licenses/by-nc-nd/4.0/deed.pl\n\nwersja: $SCRIPT_VERSION ($SCRIPT_BUILD_TIME) $UPDATE_CHANNEL" } prompt_welcome() { @@ -1015,6 +1028,151 @@ admin_panel_promo() { whiptail --title "Panel zarządzania Mikr.us-em" --msgbox "$(center_multiline "Ta instalacja Nightscout dodaje dodatkowy panel administracyjny\ndo zarządzania serwerem i konfiguracją - online.\n\nZnajdziesz go klikając na ikonkę serwera w menu strony Nightscout\nlub dodając /mikrus na końcu swojego adresu Nightscout" 70)" 12 75 } +get_watchdog_age_string() { + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + last_time=$(cat $WATCHDOG_TIME_FILE) + local status_ago=$(dateutils.ddiff "$last_time" "$curr_time" -f '%Mmin. %Ssek.') + echo "$last_time ($status_ago temu)" + else + echo "jescze nie uruchomiony" + fi +} + +get_watchdog_status_code() { + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local status="unknown" + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + last_time=$(cat $WATCHDOG_TIME_FILE) + fi + + if [[ -f $WATCHDOG_STATUS_FILE ]]; then + status=$(cat $WATCHDOG_STATUS_FILE) + fi + + local status_ago=$(dateutils.ddiff "$curr_time" "$last_time" -f '%S') + + if [ "$status_ago" -gt 900 ]; then + status="unknown" + fi + + echo "$status" +} + +get_watchdog_status_code_live() { + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local status="unknown" + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + last_time=$(cat $WATCHDOG_TIME_FILE) + fi + + if [[ -f $WATCHDOG_STATUS_FILE ]]; then + status=$(cat $WATCHDOG_STATUS_FILE) + fi + + local status_ago=$(dateutils.ddiff "$curr_time" "$last_time" -f '%S') + + if [ "$status_ago" -gt 900 ]; then + status="unknown" + fi + + local NS_STATUS=$(get_container_status_code 'ns-server') + local DB_STATUS=$(get_container_status_code 'ns-database') + local COMBINED_STATUS="$NS_STATUS $DB_STATUS" + + if [ "$COMBINED_STATUS" = "running running" ]; then + local domain=$(get_td_domain) + local html=$(curl -Lks "$domain") + + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + status="ok" + fi + + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + status="crashed" + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + status="restarting" + fi + + else + if [ "$NS_STATUS" = "restarting" ] || [ "$DB_STATUS" = "restarting" ]; then + status="restarting" + else + status="not_running" + fi + fi + + echo "$status" +} + +get_watchdog_status() { + local status="$1" + case "$status" in + "ok") + echo "$2" + ;; + "restart") + printf "\U1F680 wymuszono restart NS" + ;; + "restarting") + printf "\U23F3 uruchamia się" + ;; + "unknown") + printf "\U1F4A4 brak statusu" + ;; + "not_running") + printf "\U1F534 serwer nie działa" + ;; + "detection_failed") + printf "\U2753 nieznany stan" + ;; + "crashed") + printf "\U1F4A5 awaria NS" + ;; + esac + +} + +show_watchdog_logs() { + local col=$((COLUMNS - 10)) + local rws=$((LINES - 3)) + if [ $col -gt 120 ]; then + col=160 + fi + if [ $col -lt 60 ]; then + col=60 + fi + if [ $rws -lt 12 ]; then + rws=12 + fi + + local tmpfile=$(mktemp) + { + echo "Ostatnie uruchomienie watchdoga:" + get_watchdog_age_string + echo "-------------------------------------------------------" + + echo "Statusy ostatnich przebiegów watchdoga:" + tail -5 "$WATCHDOG_LOG_FILE" + echo "-------------------------------------------------------" + + echo "Log ostatniego przebiegu watchdoga:" + cat "$WATCHDOG_CRON_LOG" + } >"$tmpfile" + + whiptail --title "Logi Watchdoga" --scrolltext --textbox "$tmpfile" $rws $col + rm "$tmpfile" +} + get_container_status() { local ID=$(docker ps -a --no-trunc --filter name="^$1$" --format '{{ .ID }}') if [[ "$ID" =~ [0-9a-fA-F]{12,} ]]; then @@ -1079,10 +1237,11 @@ show_logs() { status_menu() { while :; do - local CHOICE=$(whiptail --title "Status kontenerów" --menu "\nWybierz pozycję aby zobaczyć logi:\n" 15 60 5 \ + local CHOICE=$(whiptail --title "Status kontenerów" --menu "\n Aktualizacja: kontenery na żywo, watchdog co 5 minut\n\n Wybierz pozycję aby zobaczyć logi:\n" 17 60 5 \ "1)" " Nightscout: $(get_container_status 'ns-server')" \ "2)" " Baza danych: $(get_container_status 'ns-database')" \ "3)" " Backup: $(get_container_status 'ns-backup')" \ + "4)" " Watchdog: $(get_watchdog_status "$(get_watchdog_status_code)" "$uni_watchdog_ok")" \ "M)" "Powrót do menu" \ --ok-button="Zobacz logi" --cancel-button="$uni_back" \ 3>&2 2>&1 1>&3) @@ -1097,6 +1256,9 @@ status_menu() { "3)") show_logs 'ns-backup' 'usługi kopii zapasowych' ;; + "4)") + show_watchdog_logs + ;; "M)") break ;; @@ -1240,7 +1402,7 @@ uninstall_menu() { if ! [ $? -eq 1 ]; then docker_compose_down dialog --title " Odinstalowanie" --infobox "\n Usuwanie plików\n ... Proszę czekać ..." 6 32 - uninstall_cron + uninstall_cron rm -r "${MONGO_DB_DIR:?}/data" rm -r "${CONFIG_ROOT_DIR:?}" rm "$TOOL_LINK" @@ -1279,7 +1441,7 @@ get_domain_status() { main_menu() { while :; do local ns_tag=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_NIGHTSCOUT_TAG") - local quickStatus=$(center_text "Nightscout: $(get_container_status 'ns-server')" 55) + local quickStatus=$(center_text "Strona Nightscout: $(get_watchdog_status "$(get_watchdog_status_code_live)" "$uni_ns_ok")" 55) local quickVersion=$(center_text "Wersja: $ns_tag" 55) local quickDomain=$(center_text "Domena: $(get_domain_status 'ns-server')" 55) local CHOICE=$(whiptail --title "Zarządzanie Nightscoutem :: $SCRIPT_VERSION" --menu "\n$quickStatus\n$quickVersion\n$quickDomain\n" 20 60 9 \ @@ -1408,8 +1570,8 @@ watchdog_check() { if [ "$COMBINED_STATUS" = "running running" ]; then echo "Will check page contents" - local ns_external_port=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_PORT") - local html=$(curl -s "127.0.0.1:$ns_external_port") + local domain=$(get_td_domain) + local html=$(curl -Lks "$domain") WATCHDOG_STATUS="detection_failed" @@ -1421,13 +1583,13 @@ watchdog_check() { if [[ "$html" =~ 'MongoDB connection failed' ]]; then echo "Nightscout is crashed, restarting..." WATCHDOG_STATUS="restart" - if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then - docker restart 'ns-server' - echo "...done" - fi + if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then + docker restart 'ns-server' + echo "...done" + fi fi - regex3='MIKR.US - coś poszło nie tak' + regex3='MIKR.US - coś poszło nie tak' if [[ "$html" =~ $regex3 ]]; then echo "Nightscout is still restarting..." WATCHDOG_STATUS="restarting" @@ -1461,13 +1623,19 @@ watchdog_check() { load_update_channel() { if [[ -f $UPDATE_CHANNEL_FILE ]]; then UPDATE_CHANNEL=$(cat $UPDATE_CHANNEL_FILE) - msgok "Loaded update channel: $UPDATE_CHANNEL" + msgok "Loaded update channel: $UPDATE_CHANNEL" fi } +startup_version() { + msgnote "nightscout-tool version $SCRIPT_VERSION ($SCRIPT_BUILD_TIME)" + msgnote "$uni_copyright 2023-2024 Dominik Dzienia" + msgnote "Licensed under CC BY-NC-ND 4.0" +} + parse_commandline_args() { - load_update_channel + load_update_channel CMDARGS=$(getopt --quiet -o wvdp --long watchdog,version,develop,production -n 'nightscout-tool' -- "$@") @@ -1492,16 +1660,16 @@ parse_commandline_args() { exit 0 ;; -d | --develop) - warn "Switching to DEVELOP update channel" - UPDATE_CHANNEL=develop - echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE - shift + warn "Switching to DEVELOP update channel" + UPDATE_CHANNEL=develop + echo "$UPDATE_CHANNEL" >$UPDATE_CHANNEL_FILE + shift ;; -p | --production) - warn "Switching to PRODUCTION update channel" - UPDATE_CHANNEL=master - echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE - shift + warn "Switching to PRODUCTION update channel" + UPDATE_CHANNEL=master + echo "$UPDATE_CHANNEL" >$UPDATE_CHANNEL_FILE + shift ;; --) shift @@ -1522,6 +1690,7 @@ parse_commandline_args() { # MAIN SCRIPT #======================================= +startup_version parse_commandline_args "$@" # check_interactive check_git diff --git a/src/lib.sh b/src/lib.sh index 02dd810..dbf54c6 100644 --- a/src/lib.sh +++ b/src/lib.sh @@ -26,7 +26,7 @@ TOOL_LINK=/usr/bin/nightscout-tool UPDATES_DIR=/srv/nightscout/updates UPDATE_CHANNEL=master SCRIPT_VERSION="1.8.0" #auto-update -SCRIPT_BUILD_TIME="2024.01.06" #auto-update +SCRIPT_BUILD_TIME="2024.01.07" #auto-update #======================================= # SETUP @@ -116,8 +116,10 @@ tty_reset="$(tty_escape 0)" emoji_check="\U2705" emoji_ok="\U1F197" emoji_err="\U274C" +emoji_note="\U1F4A1" uni_bullet=" $(printf '\u2022') " +uni_copyright="$(printf '\uA9\uFE0F')" uni_bullet_pad=" " uni_exit=" $(printf '\U274C') Wyjdź " @@ -136,6 +138,9 @@ uni_confirm_ed=" $(printf '\U1F4DD') Edytuj " uni_install=" $(printf '\U1F680') Instaluj " uni_resign=" $(printf '\U1F6AB') Rezygnuję " +uni_ns_ok="$(printf '\U1F7E2') działa" +uni_watchdog_ok="$(printf '\U1F415') Nightscout działa" + #======================================= # UTILS #======================================= @@ -163,6 +168,11 @@ msgok() { printf "$emoji_ok $1\n" } +msgnote() { + # shellcheck disable=SC2059 + printf "$emoji_note $1\n" +} + msgcheck() { # shellcheck disable=SC2059 printf "$emoji_check $1\n" @@ -529,15 +539,18 @@ setup_firewall_for_ns() { } install_cron() { - local croncmd="$TOOL_LINK -w > $WATCHDOG_CRON_LOG 2>&1" - local cronjob="*/5 * * * * $croncmd" - msgok "Configuring watchdog..." - ( crontab -l | grep -v -F "$croncmd" || : ; echo "$cronjob" ) | crontab - + local croncmd="$TOOL_LINK -w > $WATCHDOG_CRON_LOG 2>&1" + local cronjob="*/5 * * * * $croncmd" + msgok "Configuring watchdog..." + ( + crontab -l | grep -v -F "$croncmd" || : + echo "$cronjob" + ) | crontab - } uninstall_cron() { - local croncmd="nightscout-tool" - ( crontab -l | grep -v -F "$croncmd" ) | crontab - + local croncmd="nightscout-tool" + (crontab -l | grep -v -F "$croncmd") | crontab - } get_docker_status() { @@ -742,7 +755,7 @@ update_if_needed() { about_dialog() { okdlg "O tym narzędziu..." \ - "$(printf '\U1F9D1') (c) 2023 Dominik Dzienia\n$(printf '\U1F4E7') dominik.dzienia@gmail.com\n\n$(printf '\U1F3DB') To narzędzie jest dystrybuowane na licencji CC BY-NC-ND 4.0\nhttps://creativecommons.org/licenses/by-nc-nd/4.0/deed.pl\n\nwersja: $SCRIPT_VERSION ($SCRIPT_BUILD_TIME)" + "$(printf '\U1F9D1') (c) 2023 Dominik Dzienia\n$(printf '\U1F4E7') dominik.dzienia@gmail.com\n\n$(printf '\U1F3DB') To narzędzie jest dystrybuowane na licencji CC BY-NC-ND 4.0\nhttps://creativecommons.org/licenses/by-nc-nd/4.0/deed.pl\n\nwersja: $SCRIPT_VERSION ($SCRIPT_BUILD_TIME) $UPDATE_CHANNEL" } prompt_welcome() { @@ -999,6 +1012,151 @@ admin_panel_promo() { whiptail --title "Panel zarządzania Mikr.us-em" --msgbox "$(center_multiline "Ta instalacja Nightscout dodaje dodatkowy panel administracyjny\ndo zarządzania serwerem i konfiguracją - online.\n\nZnajdziesz go klikając na ikonkę serwera w menu strony Nightscout\nlub dodając /mikrus na końcu swojego adresu Nightscout" 70)" 12 75 } +get_watchdog_age_string() { + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + last_time=$(cat $WATCHDOG_TIME_FILE) + local status_ago=$(dateutils.ddiff "$last_time" "$curr_time" -f '%Mmin. %Ssek.') + echo "$last_time ($status_ago temu)" + else + echo "jescze nie uruchomiony" + fi +} + +get_watchdog_status_code() { + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local status="unknown" + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + last_time=$(cat $WATCHDOG_TIME_FILE) + fi + + if [[ -f $WATCHDOG_STATUS_FILE ]]; then + status=$(cat $WATCHDOG_STATUS_FILE) + fi + + local status_ago=$(dateutils.ddiff "$curr_time" "$last_time" -f '%S') + + if [ "$status_ago" -gt 900 ]; then + status="unknown" + fi + + echo "$status" +} + +get_watchdog_status_code_live() { + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local status="unknown" + + if [[ -f $WATCHDOG_TIME_FILE ]]; then + last_time=$(cat $WATCHDOG_TIME_FILE) + fi + + if [[ -f $WATCHDOG_STATUS_FILE ]]; then + status=$(cat $WATCHDOG_STATUS_FILE) + fi + + local status_ago=$(dateutils.ddiff "$curr_time" "$last_time" -f '%S') + + if [ "$status_ago" -gt 900 ]; then + status="unknown" + fi + + local NS_STATUS=$(get_container_status_code 'ns-server') + local DB_STATUS=$(get_container_status_code 'ns-database') + local COMBINED_STATUS="$NS_STATUS $DB_STATUS" + + if [ "$COMBINED_STATUS" = "running running" ]; then + local domain=$(get_td_domain) + local html=$(curl -Lks "$domain") + + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + status="ok" + fi + + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + status="crashed" + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + status="restarting" + fi + + else + if [ "$NS_STATUS" = "restarting" ] || [ "$DB_STATUS" = "restarting" ]; then + status="restarting" + else + status="not_running" + fi + fi + + echo "$status" +} + +get_watchdog_status() { + local status="$1" + case "$status" in + "ok") + echo "$2" + ;; + "restart") + printf "\U1F680 wymuszono restart NS" + ;; + "restarting") + printf "\U23F3 uruchamia się" + ;; + "unknown") + printf "\U1F4A4 brak statusu" + ;; + "not_running") + printf "\U1F534 serwer nie działa" + ;; + "detection_failed") + printf "\U2753 nieznany stan" + ;; + "crashed") + printf "\U1F4A5 awaria NS" + ;; + esac + +} + +show_watchdog_logs() { + local col=$((COLUMNS - 10)) + local rws=$((LINES - 3)) + if [ $col -gt 120 ]; then + col=160 + fi + if [ $col -lt 60 ]; then + col=60 + fi + if [ $rws -lt 12 ]; then + rws=12 + fi + + local tmpfile=$(mktemp) + { + echo "Ostatnie uruchomienie watchdoga:" + get_watchdog_age_string + echo "-------------------------------------------------------" + + echo "Statusy ostatnich przebiegów watchdoga:" + tail -5 "$WATCHDOG_LOG_FILE" + echo "-------------------------------------------------------" + + echo "Log ostatniego przebiegu watchdoga:" + cat "$WATCHDOG_CRON_LOG" + } >"$tmpfile" + + whiptail --title "Logi Watchdoga" --scrolltext --textbox "$tmpfile" $rws $col + rm "$tmpfile" +} + get_container_status() { local ID=$(docker ps -a --no-trunc --filter name="^$1$" --format '{{ .ID }}') if [[ "$ID" =~ [0-9a-fA-F]{12,} ]]; then @@ -1063,10 +1221,11 @@ show_logs() { status_menu() { while :; do - local CHOICE=$(whiptail --title "Status kontenerów" --menu "\nWybierz pozycję aby zobaczyć logi:\n" 15 60 5 \ + local CHOICE=$(whiptail --title "Status kontenerów" --menu "\n Aktualizacja: kontenery na żywo, watchdog co 5 minut\n\n Wybierz pozycję aby zobaczyć logi:\n" 17 60 5 \ "1)" " Nightscout: $(get_container_status 'ns-server')" \ "2)" " Baza danych: $(get_container_status 'ns-database')" \ "3)" " Backup: $(get_container_status 'ns-backup')" \ + "4)" " Watchdog: $(get_watchdog_status "$(get_watchdog_status_code)" "$uni_watchdog_ok")" \ "M)" "Powrót do menu" \ --ok-button="Zobacz logi" --cancel-button="$uni_back" \ 3>&2 2>&1 1>&3) @@ -1081,6 +1240,9 @@ status_menu() { "3)") show_logs 'ns-backup' 'usługi kopii zapasowych' ;; + "4)") + show_watchdog_logs + ;; "M)") break ;; @@ -1224,7 +1386,7 @@ uninstall_menu() { if ! [ $? -eq 1 ]; then docker_compose_down dialog --title " Odinstalowanie" --infobox "\n Usuwanie plików\n ... Proszę czekać ..." 6 32 - uninstall_cron + uninstall_cron rm -r "${MONGO_DB_DIR:?}/data" rm -r "${CONFIG_ROOT_DIR:?}" rm "$TOOL_LINK" @@ -1263,7 +1425,7 @@ get_domain_status() { main_menu() { while :; do local ns_tag=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_NIGHTSCOUT_TAG") - local quickStatus=$(center_text "Nightscout: $(get_container_status 'ns-server')" 55) + local quickStatus=$(center_text "Strona Nightscout: $(get_watchdog_status "$(get_watchdog_status_code_live)" "$uni_ns_ok")" 55) local quickVersion=$(center_text "Wersja: $ns_tag" 55) local quickDomain=$(center_text "Domena: $(get_domain_status 'ns-server')" 55) local CHOICE=$(whiptail --title "Zarządzanie Nightscoutem :: $SCRIPT_VERSION" --menu "\n$quickStatus\n$quickVersion\n$quickDomain\n" 20 60 9 \ @@ -1392,8 +1554,8 @@ watchdog_check() { if [ "$COMBINED_STATUS" = "running running" ]; then echo "Will check page contents" - local ns_external_port=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_PORT") - local html=$(curl -s "127.0.0.1:$ns_external_port") + local domain=$(get_td_domain) + local html=$(curl -Lks "$domain") WATCHDOG_STATUS="detection_failed" @@ -1405,13 +1567,13 @@ watchdog_check() { if [[ "$html" =~ 'MongoDB connection failed' ]]; then echo "Nightscout is crashed, restarting..." WATCHDOG_STATUS="restart" - if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then - docker restart 'ns-server' - echo "...done" - fi + if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then + docker restart 'ns-server' + echo "...done" + fi fi - regex3='MIKR.US - coś poszło nie tak' + regex3='MIKR.US - coś poszło nie tak' if [[ "$html" =~ $regex3 ]]; then echo "Nightscout is still restarting..." WATCHDOG_STATUS="restarting" @@ -1445,13 +1607,19 @@ watchdog_check() { load_update_channel() { if [[ -f $UPDATE_CHANNEL_FILE ]]; then UPDATE_CHANNEL=$(cat $UPDATE_CHANNEL_FILE) - msgok "Loaded update channel: $UPDATE_CHANNEL" + msgok "Loaded update channel: $UPDATE_CHANNEL" fi } +startup_version() { + msgnote "nightscout-tool version $SCRIPT_VERSION ($SCRIPT_BUILD_TIME)" + msgnote "$uni_copyright 2023-2024 Dominik Dzienia" + msgnote "Licensed under CC BY-NC-ND 4.0" +} + parse_commandline_args() { - load_update_channel + load_update_channel CMDARGS=$(getopt --quiet -o wvdp --long watchdog,version,develop,production -n 'nightscout-tool' -- "$@") @@ -1476,16 +1644,16 @@ parse_commandline_args() { exit 0 ;; -d | --develop) - warn "Switching to DEVELOP update channel" - UPDATE_CHANNEL=develop - echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE - shift + warn "Switching to DEVELOP update channel" + UPDATE_CHANNEL=develop + echo "$UPDATE_CHANNEL" >$UPDATE_CHANNEL_FILE + shift ;; -p | --production) - warn "Switching to PRODUCTION update channel" - UPDATE_CHANNEL=master - echo "$UPDATE_CHANNEL" > $UPDATE_CHANNEL_FILE - shift + warn "Switching to PRODUCTION update channel" + UPDATE_CHANNEL=master + echo "$UPDATE_CHANNEL" >$UPDATE_CHANNEL_FILE + shift ;; --) shift diff --git a/src/setup.sh b/src/setup.sh index ba22697..26783a9 100755 --- a/src/setup.sh +++ b/src/setup.sh @@ -26,6 +26,7 @@ if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi # MAIN SCRIPT #======================================= +startup_version parse_commandline_args "$@" # check_interactive check_git diff --git a/updated b/updated index 12b7ee5..1a23781 100644 --- a/updated +++ b/updated @@ -1 +1 @@ -2024-01-06T21:57:56.813Z \ No newline at end of file +2024-01-07T13:33:40.615Z \ No newline at end of file -- 2.38.5 From 2c60195d4f61b9c8297e560f9b2df2d083bf4700 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Sun, 7 Jan 2024 14:14:02 +0000 Subject: [PATCH 4/7] Watchdog error handling --- install.sh | 74 ++++++++++++++++++++++++++++++++---------------------- src/lib.sh | 74 ++++++++++++++++++++++++++++++++---------------------- updated | 2 +- 3 files changed, 89 insertions(+), 61 deletions(-) diff --git a/install.sh b/install.sh index 01a6c21..095d308 100644 --- a/install.sh +++ b/install.sh @@ -1029,13 +1029,13 @@ admin_panel_promo() { } get_watchdog_age_string() { - local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") if [[ -f $WATCHDOG_TIME_FILE ]]; then last_time=$(cat $WATCHDOG_TIME_FILE) local status_ago=$(dateutils.ddiff "$last_time" "$curr_time" -f '%Mmin. %Ssek.') - echo "$last_time ($status_ago temu)" + echo "$last_time ($status_ago temu)" else echo "jescze nie uruchomiony" fi @@ -1088,19 +1088,26 @@ get_watchdog_status_code_live() { if [ "$COMBINED_STATUS" = "running running" ]; then local domain=$(get_td_domain) - local html=$(curl -Lks "$domain") + local domainLen=${#domain} + if ((domainLen > 15)); then - if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then - status="ok" - fi + local html=$(curl -Lks "$domain") - if [[ "$html" =~ 'MongoDB connection failed' ]]; then - status="crashed" - fi + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + status="ok" + fi - regex3='MIKR.US - coś poszło nie tak' - if [[ "$html" =~ $regex3 ]]; then - status="restarting" + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + status="crashed" + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + status="restarting" + fi + + else + status="detection_failed" fi else @@ -1166,7 +1173,7 @@ show_watchdog_logs() { echo "-------------------------------------------------------" echo "Log ostatniego przebiegu watchdoga:" - cat "$WATCHDOG_CRON_LOG" + cat "$WATCHDOG_CRON_LOG" } >"$tmpfile" whiptail --title "Logi Watchdoga" --scrolltext --textbox "$tmpfile" $rws $col @@ -1571,28 +1578,35 @@ watchdog_check() { if [ "$COMBINED_STATUS" = "running running" ]; then echo "Will check page contents" local domain=$(get_td_domain) - local html=$(curl -Lks "$domain") - WATCHDOG_STATUS="detection_failed" + local domainLen=${#domain} + if ((domainLen > 15)); then + local html=$(curl -Lks "$domain") - if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then - echo "Nightscout is running" - WATCHDOG_STATUS="ok" - fi + WATCHDOG_STATUS="detection_failed" - if [[ "$html" =~ 'MongoDB connection failed' ]]; then - echo "Nightscout is crashed, restarting..." - WATCHDOG_STATUS="restart" - if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then - docker restart 'ns-server' - echo "...done" + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + echo "Nightscout is running" + WATCHDOG_STATUS="ok" fi - fi - regex3='MIKR.US - coś poszło nie tak' - if [[ "$html" =~ $regex3 ]]; then - echo "Nightscout is still restarting..." - WATCHDOG_STATUS="restarting" + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + echo "Nightscout is crashed, restarting..." + WATCHDOG_STATUS="restart" + if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then + docker restart 'ns-server' + echo "...done" + fi + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + echo "Nightscout is still restarting..." + WATCHDOG_STATUS="restarting" + fi + + else + WATCHDOG_STATUS="detection_failed" fi else diff --git a/src/lib.sh b/src/lib.sh index dbf54c6..3170e7e 100644 --- a/src/lib.sh +++ b/src/lib.sh @@ -1013,13 +1013,13 @@ admin_panel_promo() { } get_watchdog_age_string() { - local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") local last_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + local curr_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ") if [[ -f $WATCHDOG_TIME_FILE ]]; then last_time=$(cat $WATCHDOG_TIME_FILE) local status_ago=$(dateutils.ddiff "$last_time" "$curr_time" -f '%Mmin. %Ssek.') - echo "$last_time ($status_ago temu)" + echo "$last_time ($status_ago temu)" else echo "jescze nie uruchomiony" fi @@ -1072,19 +1072,26 @@ get_watchdog_status_code_live() { if [ "$COMBINED_STATUS" = "running running" ]; then local domain=$(get_td_domain) - local html=$(curl -Lks "$domain") + local domainLen=${#domain} + if ((domainLen > 15)); then - if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then - status="ok" - fi + local html=$(curl -Lks "$domain") - if [[ "$html" =~ 'MongoDB connection failed' ]]; then - status="crashed" - fi + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + status="ok" + fi - regex3='MIKR.US - coś poszło nie tak' - if [[ "$html" =~ $regex3 ]]; then - status="restarting" + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + status="crashed" + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + status="restarting" + fi + + else + status="detection_failed" fi else @@ -1150,7 +1157,7 @@ show_watchdog_logs() { echo "-------------------------------------------------------" echo "Log ostatniego przebiegu watchdoga:" - cat "$WATCHDOG_CRON_LOG" + cat "$WATCHDOG_CRON_LOG" } >"$tmpfile" whiptail --title "Logi Watchdoga" --scrolltext --textbox "$tmpfile" $rws $col @@ -1555,28 +1562,35 @@ watchdog_check() { if [ "$COMBINED_STATUS" = "running running" ]; then echo "Will check page contents" local domain=$(get_td_domain) - local html=$(curl -Lks "$domain") - WATCHDOG_STATUS="detection_failed" + local domainLen=${#domain} + if ((domainLen > 15)); then + local html=$(curl -Lks "$domain") - if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then - echo "Nightscout is running" - WATCHDOG_STATUS="ok" - fi + WATCHDOG_STATUS="detection_failed" - if [[ "$html" =~ 'MongoDB connection failed' ]]; then - echo "Nightscout is crashed, restarting..." - WATCHDOG_STATUS="restart" - if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then - docker restart 'ns-server' - echo "...done" + if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then + echo "Nightscout is running" + WATCHDOG_STATUS="ok" fi - fi - regex3='MIKR.US - coś poszło nie tak' - if [[ "$html" =~ $regex3 ]]; then - echo "Nightscout is still restarting..." - WATCHDOG_STATUS="restarting" + if [[ "$html" =~ 'MongoDB connection failed' ]]; then + echo "Nightscout is crashed, restarting..." + WATCHDOG_STATUS="restart" + if [ "$WATCHDOG_LAST_STATUS" != "restart" ]; then + docker restart 'ns-server' + echo "...done" + fi + fi + + regex3='MIKR.US - coś poszło nie tak' + if [[ "$html" =~ $regex3 ]]; then + echo "Nightscout is still restarting..." + WATCHDOG_STATUS="restarting" + fi + + else + WATCHDOG_STATUS="detection_failed" fi else diff --git a/updated b/updated index 1a23781..4a4c9f4 100644 --- a/updated +++ b/updated @@ -1 +1 @@ -2024-01-07T13:33:40.615Z \ No newline at end of file +2024-01-07T14:13:41.909Z \ No newline at end of file -- 2.38.5 From f49084390612e1ca461e527082e7a0f0e53015d2 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Sun, 7 Jan 2024 14:41:40 +0000 Subject: [PATCH 5/7] Added domain requesting cache --- install.sh | 20 ++++++++++++++++---- src/lib.sh | 20 ++++++++++++++++---- updated | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/install.sh b/install.sh index 095d308..44d9e7c 100644 --- a/install.sh +++ b/install.sh @@ -350,6 +350,7 @@ confirmdlg() { packages=() aptGetWasUpdated=0 freshInstall=0 +cachedMenuDomain='' MIKRUS_APIKEY='' MIKRUS_HOST='' @@ -1087,10 +1088,18 @@ get_watchdog_status_code_live() { local COMBINED_STATUS="$NS_STATUS $DB_STATUS" if [ "$COMBINED_STATUS" = "running running" ]; then - local domain=$(get_td_domain) + + status="detection_failed" + + local domain=$cachedMenuDomain + local cachedDomainLen=${#cachedMenuDomain} + if ((cachedDomainLen < 16)); then + domain=$(get_td_domain) + fi + local domainLen=${#domain} if ((domainLen > 15)); then - + cachedMenuDomain=$domain local html=$(curl -Lks "$domain") if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then @@ -1107,7 +1116,7 @@ get_watchdog_status_code_live() { fi else - status="detection_failed" + status="domain_failed" fi else @@ -1142,6 +1151,9 @@ get_watchdog_status() { "detection_failed") printf "\U2753 nieznany stan" ;; + "domain_failed") + printf "\U2753 problem z domeną" + ;; "crashed") printf "\U1F4A5 awaria NS" ;; @@ -1606,7 +1618,7 @@ watchdog_check() { fi else - WATCHDOG_STATUS="detection_failed" + WATCHDOG_STATUS="domain_failed" fi else diff --git a/src/lib.sh b/src/lib.sh index 3170e7e..c919996 100644 --- a/src/lib.sh +++ b/src/lib.sh @@ -334,6 +334,7 @@ confirmdlg() { packages=() aptGetWasUpdated=0 freshInstall=0 +cachedMenuDomain='' MIKRUS_APIKEY='' MIKRUS_HOST='' @@ -1071,10 +1072,18 @@ get_watchdog_status_code_live() { local COMBINED_STATUS="$NS_STATUS $DB_STATUS" if [ "$COMBINED_STATUS" = "running running" ]; then - local domain=$(get_td_domain) + + status="detection_failed" + + local domain=$cachedMenuDomain + local cachedDomainLen=${#cachedMenuDomain} + if ((cachedDomainLen < 16)); then + domain=$(get_td_domain) + fi + local domainLen=${#domain} if ((domainLen > 15)); then - + cachedMenuDomain=$domain local html=$(curl -Lks "$domain") if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then @@ -1091,7 +1100,7 @@ get_watchdog_status_code_live() { fi else - status="detection_failed" + status="domain_failed" fi else @@ -1126,6 +1135,9 @@ get_watchdog_status() { "detection_failed") printf "\U2753 nieznany stan" ;; + "domain_failed") + printf "\U2753 problem z domeną" + ;; "crashed") printf "\U1F4A5 awaria NS" ;; @@ -1590,7 +1602,7 @@ watchdog_check() { fi else - WATCHDOG_STATUS="detection_failed" + WATCHDOG_STATUS="domain_failed" fi else diff --git a/updated b/updated index 4a4c9f4..7dda6c1 100644 --- a/updated +++ b/updated @@ -1 +1 @@ -2024-01-07T14:13:41.909Z \ No newline at end of file +2024-01-07T14:41:12.444Z \ No newline at end of file -- 2.38.5 From 4b78d9117a5df7bce7089b3fc302d0cd16f321bb Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Sun, 7 Jan 2024 14:51:43 +0000 Subject: [PATCH 6/7] Fixed log on new install --- install.sh | 24 +++++++++++++++--------- src/lib.sh | 24 +++++++++++++++--------- updated | 2 +- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/install.sh b/install.sh index 44d9e7c..9d5d7df 100644 --- a/install.sh +++ b/install.sh @@ -1088,18 +1088,18 @@ get_watchdog_status_code_live() { local COMBINED_STATUS="$NS_STATUS $DB_STATUS" if [ "$COMBINED_STATUS" = "running running" ]; then - + status="detection_failed" - local domain=$cachedMenuDomain + local domain=$cachedMenuDomain local cachedDomainLen=${#cachedMenuDomain} if ((cachedDomainLen < 16)); then - domain=$(get_td_domain) - fi + domain=$(get_td_domain) + fi local domainLen=${#domain} if ((domainLen > 15)); then - cachedMenuDomain=$domain + cachedMenuDomain=$domain local html=$(curl -Lks "$domain") if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then @@ -1180,12 +1180,18 @@ show_watchdog_logs() { get_watchdog_age_string echo "-------------------------------------------------------" - echo "Statusy ostatnich przebiegów watchdoga:" - tail -5 "$WATCHDOG_LOG_FILE" + if [[ -f $WATCHDOG_LOG_FILE ]]; then + echo "Statusy ostatnich przebiegów watchdoga:" + tail -5 "$WATCHDOG_LOG_FILE" + else + echo "Brak logów z ostatnich przebiegów watchdoga" + fi echo "-------------------------------------------------------" - echo "Log ostatniego przebiegu watchdoga:" - cat "$WATCHDOG_CRON_LOG" + if [[ -f $WATCHDOG_CRON_LOG ]]; then + echo "Log ostatniego przebiegu watchdoga:" + cat "$WATCHDOG_CRON_LOG" + fi } >"$tmpfile" whiptail --title "Logi Watchdoga" --scrolltext --textbox "$tmpfile" $rws $col diff --git a/src/lib.sh b/src/lib.sh index c919996..7160d75 100644 --- a/src/lib.sh +++ b/src/lib.sh @@ -1072,18 +1072,18 @@ get_watchdog_status_code_live() { local COMBINED_STATUS="$NS_STATUS $DB_STATUS" if [ "$COMBINED_STATUS" = "running running" ]; then - + status="detection_failed" - local domain=$cachedMenuDomain + local domain=$cachedMenuDomain local cachedDomainLen=${#cachedMenuDomain} if ((cachedDomainLen < 16)); then - domain=$(get_td_domain) - fi + domain=$(get_td_domain) + fi local domainLen=${#domain} if ((domainLen > 15)); then - cachedMenuDomain=$domain + cachedMenuDomain=$domain local html=$(curl -Lks "$domain") if [[ "$html" =~ github.com/nightscout/cgm-remote-monitor ]]; then @@ -1164,12 +1164,18 @@ show_watchdog_logs() { get_watchdog_age_string echo "-------------------------------------------------------" - echo "Statusy ostatnich przebiegów watchdoga:" - tail -5 "$WATCHDOG_LOG_FILE" + if [[ -f $WATCHDOG_LOG_FILE ]]; then + echo "Statusy ostatnich przebiegów watchdoga:" + tail -5 "$WATCHDOG_LOG_FILE" + else + echo "Brak logów z ostatnich przebiegów watchdoga" + fi echo "-------------------------------------------------------" - echo "Log ostatniego przebiegu watchdoga:" - cat "$WATCHDOG_CRON_LOG" + if [[ -f $WATCHDOG_CRON_LOG ]]; then + echo "Log ostatniego przebiegu watchdoga:" + cat "$WATCHDOG_CRON_LOG" + fi } >"$tmpfile" whiptail --title "Logi Watchdoga" --scrolltext --textbox "$tmpfile" $rws $col diff --git a/updated b/updated index 7dda6c1..603ddcb 100644 --- a/updated +++ b/updated @@ -1 +1 @@ -2024-01-07T14:41:12.444Z \ No newline at end of file +2024-01-07T14:51:22.135Z \ No newline at end of file -- 2.38.5 From 743b6a49e22e5544216570eaa01935924f85e6c2 Mon Sep 17 00:00:00 2001 From: Dominik Dzienia Date: Sun, 7 Jan 2024 18:30:25 +0000 Subject: [PATCH 7/7] Fixed domain names hint --- CHANGELOG.md | 6 +++++- install.sh | 6 +++--- src/lib.sh | 6 +++--- updated | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1883bc2..fb9cbb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [1.8.0] - 2024-01-06 +## [1.8.0] - 2024-01-07 ### Added @@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Removed underscore from domain name hint, as xDrip and NS have issues with domains containing it + +### Fixed + - More memory and memory limits config (in template) for MongoDB ## [1.7.0] - 2023-10-20 diff --git a/install.sh b/install.sh index 9d5d7df..8c688f3 100644 --- a/install.sh +++ b/install.sh @@ -954,18 +954,18 @@ domain_setup() { fi ns_external_port=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_PORT") - whiptail --title "Ustaw subdomenę" --msgbox "Aby Nightscout był widoczny z internetu ustaw adres - subdomenę:\n\n [wybierz].ns.techdiab.pl\n\nWybrany początek subdomeny powinien:\n${uni_bullet}mieć długość od 4 do 12 znaków\n${uni_bullet}zaczynać się z małej litery,\n${uni_bullet}może składać się z małych liter, cyfr i podkreślenia _\n${uni_bullet}być unikalny, charakterystyczny i łatwa do zapamiętania" 16 75 + whiptail --title "Ustaw subdomenę" --msgbox "Aby Nightscout był widoczny z internetu ustaw adres - subdomenę:\n\n [wybierz].ns.techdiab.pl\n\nWybrany początek subdomeny powinien:\n${uni_bullet}mieć długość od 4 do 12 znaków\n${uni_bullet}zaczynać się z małej litery,\n${uni_bullet}może składać się z małych liter i cyfr\n${uni_bullet}być unikalny, charakterystyczny i łatwa do zapamiętania" 16 75 while :; do SUBDOMAIN='' while :; do - SUBDOMAIN=$(whiptail --title "Podaj początek subdomeny" --inputbox "\n(4-12 znaków, tylko: małe litery, cyfry oraz _)\n\n" --cancel-button "Anuluj" 12 60 3>&1 1>&2 2>&3) + SUBDOMAIN=$(whiptail --title "Podaj początek subdomeny" --inputbox "\n(4-12 znaków, tylko: małe litery i cyfry)\n\n" --cancel-button "Anuluj" 12 60 3>&1 1>&2 2>&3) if [ $? -eq 1 ]; then break fi - if [[ "$SUBDOMAIN" =~ ^[a-z][a-zA-Z0-9_]{3,11}$ ]]; then + if [[ "$SUBDOMAIN" =~ ^[a-z][a-z0-9]{3,11}$ ]]; then if printf "%s" "$SUBDOMAIN" | grep -f "$PROFANITY_DB_FILE" >>$LOGTO 2>&1; then okdlg "$uni_excl Nieprawidłowa subdomena $uni_excl" \ diff --git a/src/lib.sh b/src/lib.sh index 7160d75..dca70f5 100644 --- a/src/lib.sh +++ b/src/lib.sh @@ -938,18 +938,18 @@ domain_setup() { fi ns_external_port=$(dotenv-tool -r get -f $ENV_FILE_DEP "NS_PORT") - whiptail --title "Ustaw subdomenę" --msgbox "Aby Nightscout był widoczny z internetu ustaw adres - subdomenę:\n\n [wybierz].ns.techdiab.pl\n\nWybrany początek subdomeny powinien:\n${uni_bullet}mieć długość od 4 do 12 znaków\n${uni_bullet}zaczynać się z małej litery,\n${uni_bullet}może składać się z małych liter, cyfr i podkreślenia _\n${uni_bullet}być unikalny, charakterystyczny i łatwa do zapamiętania" 16 75 + whiptail --title "Ustaw subdomenę" --msgbox "Aby Nightscout był widoczny z internetu ustaw adres - subdomenę:\n\n [wybierz].ns.techdiab.pl\n\nWybrany początek subdomeny powinien:\n${uni_bullet}mieć długość od 4 do 12 znaków\n${uni_bullet}zaczynać się z małej litery,\n${uni_bullet}może składać się z małych liter i cyfr\n${uni_bullet}być unikalny, charakterystyczny i łatwa do zapamiętania" 16 75 while :; do SUBDOMAIN='' while :; do - SUBDOMAIN=$(whiptail --title "Podaj początek subdomeny" --inputbox "\n(4-12 znaków, tylko: małe litery, cyfry oraz _)\n\n" --cancel-button "Anuluj" 12 60 3>&1 1>&2 2>&3) + SUBDOMAIN=$(whiptail --title "Podaj początek subdomeny" --inputbox "\n(4-12 znaków, tylko: małe litery i cyfry)\n\n" --cancel-button "Anuluj" 12 60 3>&1 1>&2 2>&3) if [ $? -eq 1 ]; then break fi - if [[ "$SUBDOMAIN" =~ ^[a-z][a-zA-Z0-9_]{3,11}$ ]]; then + if [[ "$SUBDOMAIN" =~ ^[a-z][a-z0-9]{3,11}$ ]]; then if printf "%s" "$SUBDOMAIN" | grep -f "$PROFANITY_DB_FILE" >>$LOGTO 2>&1; then okdlg "$uni_excl Nieprawidłowa subdomena $uni_excl" \ diff --git a/updated b/updated index 603ddcb..ff41ff3 100644 --- a/updated +++ b/updated @@ -1 +1 @@ -2024-01-07T14:51:22.135Z \ No newline at end of file +2024-01-07T18:29:57.941Z \ No newline at end of file -- 2.38.5