Clean and add new monitoring service

This commit is contained in:
Alexandre
2024-10-07 15:27:23 +02:00
committed by GitHub
parent 03f30ad353
commit 7f50d30822
3 changed files with 56 additions and 200 deletions

View File

@@ -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="<br><b>Stopped services:</b> "
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="<br><b>Stopped services:</b> "
# 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+="<br><b>Additional informations</b>: "
NOTIFICATION+="<br><b>Since:</b> ${LASTCHECK:-unknown}"
NOTIFICATION+="<br><b>System:</b> ${SITE_NAME:-$(hostname)}"
NOTIFICATION+="<br>Available disk space: $(df -h "$(readlink -f "$HOME/BirdSongs")" | awk 'NR==2 {print $4}')"
if [ -n "$BIRDNETPI_URL" ]; then
NOTIFICATION+="<br> <a href=\"$BIRDNETPI_URL\">Access your BirdNET-Pi</a>"
fi
# Build notification message
notification+="$stopped_service"
notification+="<br><b>Additional information</b>: "
notification+="<br><b>Since:</b> ${LASTCHECK:-unknown}"
notification+="<br><b>System:</b> ${SITE_NAME:-$(hostname)}"
notification+="<br>Available disk space: $(df -h "$HOME/BirdSongs" | awk 'NR==2 {print $4}')"
[[ -n "$BIRDNETPI_URL" ]] && notification+="<br><a href=\"$BIRDNETPI_URL\">Access your BirdNET-Pi</a>"
# 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--))