From 7f50d30822daff79ce5b6d631d363e93b85f99ee Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:27:23 +0200 Subject: [PATCH] Clean and add new monitoring service --- .../rootfs/custom-services.d/30-monitoring.sh | 113 +++++++++-------- birdnet-pi/rootfs/helpers/convert_list.php | 116 ------------------ birdnet-pi/rootfs/helpers/views.add | 27 ---- 3 files changed, 56 insertions(+), 200 deletions(-) delete mode 100644 birdnet-pi/rootfs/helpers/convert_list.php delete mode 100644 birdnet-pi/rootfs/helpers/views.add diff --git a/birdnet-pi/rootfs/custom-services.d/30-monitoring.sh b/birdnet-pi/rootfs/custom-services.d/30-monitoring.sh index 4364481fb..54f8df921 100755 --- a/birdnet-pi/rootfs/custom-services.d/30-monitoring.sh +++ b/birdnet-pi/rootfs/custom-services.d/30-monitoring.sh @@ -2,90 +2,89 @@ # shellcheck shell=bash echo "Starting service: throttlerecording" -touch "$HOME"/BirdSongs/StreamData/analyzing_now.txt +touch "$HOME/BirdSongs/StreamData/analyzing_now.txt" -# variables for readability -srv="birdnet_recording" -analyzing_now="." -counter=10 -set +u -# shellcheck disable=SC1091 +# Read configuration source /config/birdnet.conf 2>/dev/null -# Ensure folder exists +# Set constants +srv="birdnet_recording" +srv2="birdnet_analysis" ingest_dir="$RECS_DIR/StreamData" +counter=10 -# Check permissions -mkdir -p "$ingest_dir" -chown -R pi:pi "$ingest_dir" -chmod -R 755 "$ingest_dir" -ingest_dir="$(readlink -f "$ingest_dir")" || true +# Ensure directories and permissions mkdir -p "$ingest_dir" chown -R pi:pi "$ingest_dir" chmod -R 755 "$ingest_dir" -function apprisealert() { - # Set failed check so it only runs once - touch "$HOME"/BirdNET-Pi/failed_servicescheck - NOTIFICATION="" - STOPPEDSERVICE="
Stopped services: " - services=(birdnet_analysis - chart_viewer - spectrogram_viewer - icecast2 - birdnet_recording - birdnet_log - birdnet_stats) - for i in "${services[@]}"; do - if [[ "$(sudo systemctl is-active "${i}".service)" == "inactive" ]]; then - STOPPEDSERVICE+="${i}; " +# Function to send notifications using Apprise +apprisealert() { + local notification="" + local stopped_service="
Stopped services: " + + # Check for stopped services + services=(birdnet_analysis chart_viewer spectrogram_viewer icecast2 birdnet_recording birdnet_log birdnet_stats) + for service in "${services[@]}"; do + if [[ "$(systemctl is-active "$service")" == "inactive" ]]; then + stopped_service+="$service; " fi done - NOTIFICATION+="$STOPPEDSERVICE" - NOTIFICATION+="
Additional informations: " - NOTIFICATION+="
Since: ${LASTCHECK:-unknown}" - NOTIFICATION+="
System: ${SITE_NAME:-$(hostname)}" - NOTIFICATION+="
Available disk space: $(df -h "$(readlink -f "$HOME/BirdSongs")" | awk 'NR==2 {print $4}')" - if [ -n "$BIRDNETPI_URL" ]; then - NOTIFICATION+="
Access your BirdNET-Pi" - fi + + # Build notification message + notification+="$stopped_service" + notification+="
Additional information: " + notification+="
Since: ${LASTCHECK:-unknown}" + notification+="
System: ${SITE_NAME:-$(hostname)}" + notification+="
Available disk space: $(df -h "$HOME/BirdSongs" | awk 'NR==2 {print $4}')" + [[ -n "$BIRDNETPI_URL" ]] && notification+="
Access your BirdNET-Pi" + + # Send notification TITLE="BirdNET-Analyzer stopped" - "$HOME"/BirdNET-Pi/birdnet/bin/apprise -vv -t "$TITLE" -b "${NOTIFICATION}" --input-format=html --config="$HOME/BirdNET-Pi/apprise.txt" + "$HOME/BirdNET-Pi/birdnet/bin/apprise" -vv -t "$TITLE" -b "$notification" --input-format=html --config="$HOME/BirdNET-Pi/apprise.txt" } +# Main loop while true; do sleep 61 # Restart analysis if clogged - ############################ - if ((counter <= 0)); then - latest="$(cat "$ingest_dir"/analyzing_now.txt)" - if [[ "$latest" == "$analyzing_now" ]]; then + current_file="$(cat "$ingest_dir/analyzing_now.txt")" + if [[ "$current_file" == "$analyzing_now" ]]; then echo "$(date) WARNING no change in analyzing_now for 10 iterations, restarting services" - /."$HOME"/BirdNET-Pi/scripts/restart_services.sh + "$HOME/BirdNET-Pi/scripts/restart_services.sh" fi counter=10 - analyzing_now=$(cat "$ingest_dir"/analyzing_now.txt) + analyzing_now="$current_file" fi - # Pause recorder to catch-up - ############################ + # Check recorder state and queue length + wav_count=$(find "$ingest_dir" -maxdepth 1 -name '*.wav' | wc -l) + service_state=$(systemctl is-active "$srv") + analysis_state=$(systemctl is-active "$srv2") - wavs="$(find "$ingest_dir" -maxdepth 1 -name '*.wav' | wc -l)" - state="$(systemctl is-active "$srv")" + bashio::log.green "$(date) INFO: $wav_count wav files waiting in $ingest_dir, $srv state is $service_state, $srv2 state is $analysis_state" - bashio::log.green "$(date) INFO ${wavs} wav files waiting in $ingest_dir, $srv state is $state" - - if ((wavs > 100)); then - bashio::log.red "$(date) WARNING too many files in queue, pausing $srv" + # Pause recorder if queue is too large + if ((wav_count > 50)); then + bashio::log.red "$(date) WARNING: Too many files in queue, pausing $srv and restarting $srv2" sudo systemctl stop "$srv" - sudo systemctl restart birdnet_analysis - if [ -s "$HOME/BirdNET-Pi/apprise.txt" ]; then apprisealert; fi - elif [[ "$state" != "active" ]]; then - bashio::log.yellow "$(date) INFO started $srv service" - sudo systemctl start $srv - sudo systemctl restart birdnet_analysis + sudo systemctl restart "$srv2" + [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]] && apprisealert + elif ((wav_count > 30)); then + bashio::log.red "$(date) WARNING: Too many files in queue, restarting $srv2" + sudo systemctl restart "$srv2" + [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]] && apprisealert + else + if [[ "$service_state" != "active" ]]; then + bashio::log.yellow "$(date) INFO: Restarting $srv service" + sudo systemctl restart "$srv" + fi + if [[ "$analysis_state" != "active" ]]; then + bashio::log.yellow "$(date) INFO: Restarting $srv2 service" + sudo systemctl restart "$srv2" + fi fi ((counter--)) diff --git a/birdnet-pi/rootfs/helpers/convert_list.php b/birdnet-pi/rootfs/helpers/convert_list.php deleted file mode 100644 index f4259d341..000000000 --- a/birdnet-pi/rootfs/helpers/convert_list.php +++ /dev/null @@ -1,116 +0,0 @@ - - - -

This tool will allow to convert on-the-fly species to compensate for model errors. It SHOULD NOT BE USED except if you know what you are doing, instead the model errors should be reported to the owner. However, it is still convenient for systematic biases that are confirmed through careful listening of samples, while waiting for the models to be updated.

- -
-
- -

Specie to convert from :

- - - -

-

Specie to convert to :

- - - - -
-
- -
-
- -
-
- -

- -
-
- -
-
-

Converted Species List

- - -
-
- -
-
- - - - diff --git a/birdnet-pi/rootfs/helpers/views.add b/birdnet-pi/rootfs/helpers/views.add deleted file mode 100644 index 8514078a0..000000000 --- a/birdnet-pi/rootfs/helpers/views.add +++ /dev/null @@ -1,27 +0,0 @@ - if($_GET['view'] == "Converted"){ - ensure_authenticated(); - if(isset($_GET['species']) && isset($_GET['add'])){ - $file = './scripts/convert_species_list.txt'; - $str = file_get_contents("$file"); - $str = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str); - file_put_contents("$file", "$str"); - // Write $_GET['species'] to the file - file_put_contents("./scripts/convert_species_list.txt", htmlspecialchars_decode($_GET['species'], ENT_QUOTES)."\n", FILE_APPEND); - } elseif (isset($_GET['species']) && isset($_GET['del'])){ - $file = './scripts/convert_species_list.txt'; - $str = file_get_contents("$file"); - $str = preg_replace('/^\h*\v+/m', '', $str); - file_put_contents("$file", "$str"); - foreach($_GET['species'] as $selectedOption) { - $content = file_get_contents("./scripts/convert_species_list.txt"); - $newcontent = str_replace($selectedOption, "", "$content"); - $newcontent = str_replace(htmlspecialchars_decode($selectedOption, ENT_QUOTES), "", "$content"); - file_put_contents("./scripts/convert_species_list.txt", "$newcontent"); - } - $file = './scripts/convert_species_list.txt'; - $str = file_get_contents("$file"); - $str = preg_replace('/^\h*\v+/m', '', $str); - file_put_contents("$file", "$str"); - } - include('./scripts/convert_list.php'); - }