Update 30-monitoring.sh

This commit is contained in:
Alexandre
2024-10-12 08:39:56 +02:00
committed by GitHub
parent d51f12199d
commit 7e8a47c4a3

View File

@@ -1,28 +1,30 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck shell=bash # shellcheck shell=bash
# Adapted from https://github.com/mcguirepr89/BirdNET-Pi/issues/393#issuecomment-1166445710
# Define logging functions
log_green() { echo -e "\033[32m$1\033[0m" }
log_red() { echo -e "\033[31m$1\033[0m" }
log_yellow() { echo -e "\033[33m$1\033[0m" }
log_info() { echo -e "\033[34m$1\033[0m" }
echo "$(log_green "Starting service: throttlerecording")"
touch "$HOME/BirdSongs/StreamData/analyzing_now.txt"
# Read configuration # Read configuration
set -u set -u
source /config/birdnet.conf 2>/dev/null source /config/birdnet.conf 2>/dev/null
set +u set +u
# Define logging functions
log_green() { echo -e "\033[32m$1\033[0m"; }
log_red() { echo -e "\033[31m$1\033[0m"; }
log_yellow() { echo -e "\033[33m$1\033[0m"; }
log_info() { echo -e "\033[34m$1\033[0m"; }
log_green "Starting service: throttlerecording"
touch "$HOME/BirdSongs/StreamData/analyzing_now.txt"
# Set constants # Set constants
srv="birdnet_recording" srv="birdnet_recording"
srv2="birdnet_analysis" srv2="birdnet_analysis"
ingest_dir="$RECS_DIR/StreamData" ingest_dir="$RECS_DIR/StreamData"
counter=10 counter=10
# Ensure directories and permissions # Ensure directories and permissions
mkdir -p "$ingest_dir" && chown -R pi:pi "$ingest_dir" && chmod -R 755 "$ingest_dir" mkdir -p "$ingest_dir"
chown -R pi:pi "$ingest_dir"
chmod -R 755 "$ingest_dir"
# Function to send notifications using Apprise # Function to send notifications using Apprise
apprisealert() { apprisealert() {
@@ -77,20 +79,25 @@ while true; do
log_red "$(date) WARNING: Too many files in queue, pausing $srv and restarting $srv2" log_red "$(date) WARNING: Too many files in queue, pausing $srv and restarting $srv2"
sudo systemctl stop "$srv" sudo systemctl stop "$srv"
sudo systemctl restart "$srv2" sudo systemctl restart "$srv2"
[[ -s "$HOME/BirdNET-Pi/apprise.txt" ]] && apprisealert sleep 30
elif ((wav_count > 30)); then elif ((wav_count > 30)); then
log_red "$(date) WARNING: Too many files in queue, restarting $srv2" log_red "$(date) WARNING: Too many files in queue, restarting $srv2"
sudo systemctl restart "$srv2" sudo systemctl restart "$srv2"
[[ -s "$HOME/BirdNET-Pi/apprise.txt" ]] && apprisealert sleep 30
else fi
if [[ "$service_state" != "active" ]]; then
log_yellow "$(date) INFO: Restarting $srv service" # Check service states
sudo systemctl restart "$srv" for service in "$srv" "$srv2"; do
fi local state_var="${service}_state"
if [[ "$analysis_state" != "active" ]]; then if [[ "${!state_var}" != "active" ]]; then
log_yellow "$(date) INFO: Restarting $srv2 service" log_yellow "$(date) INFO: Restarting $service service"
sudo systemctl restart "$srv2" sudo systemctl restart "$service"
fi fi
done
# Send alert if needed
if ((wav_count > 30)) && [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]]; then
apprisealert
fi fi
((counter--)) ((counter--))