mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-05-31 04:44:05 +02:00
Improved alert system
This commit is contained in:
@@ -23,7 +23,7 @@ srv="birdnet_recording"
|
|||||||
srv2="birdnet_analysis"
|
srv2="birdnet_analysis"
|
||||||
ingest_dir="$RECS_DIR/StreamData"
|
ingest_dir="$RECS_DIR/StreamData"
|
||||||
counter=10
|
counter=10
|
||||||
no_files_counter=0
|
|
||||||
# Ensure directories and permissions
|
# Ensure directories and permissions
|
||||||
mkdir -p "$ingest_dir"
|
mkdir -p "$ingest_dir"
|
||||||
chown -R pi:pi "$ingest_dir"
|
chown -R pi:pi "$ingest_dir"
|
||||||
@@ -31,9 +31,9 @@ chmod -R 755 "$ingest_dir"
|
|||||||
|
|
||||||
# Function to send notifications using Apprise
|
# Function to send notifications using Apprise
|
||||||
apprisealert() {
|
apprisealert() {
|
||||||
|
local issue_message="$1"
|
||||||
local notification=""
|
local notification=""
|
||||||
local stopped_service="<br><b>Stopped services:</b> "
|
local stopped_service="<br><b>Stopped services:</b> "
|
||||||
local no_files_alert="${1:-}"
|
|
||||||
|
|
||||||
# Check for stopped services
|
# Check for stopped services
|
||||||
services=(birdnet_analysis chart_viewer spectrogram_viewer icecast2 birdnet_recording birdnet_log birdnet_stats)
|
services=(birdnet_analysis chart_viewer spectrogram_viewer icecast2 birdnet_recording birdnet_log birdnet_stats)
|
||||||
@@ -44,17 +44,21 @@ apprisealert() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
# Build notification message
|
# Build notification message
|
||||||
|
notification+="<b>Issue:</b> $issue_message"
|
||||||
notification+="$stopped_service"
|
notification+="$stopped_service"
|
||||||
notification+="<br><b>Additional information</b>: "
|
notification+="<br><b>Additional information:</b>"
|
||||||
notification+="<br><b>Since:</b> ${LASTCHECK:-unknown}"
|
notification+="<br><b>Since:</b> ${LASTCHECK:-unknown}"
|
||||||
notification+="<br><b>System:</b> ${SITE_NAME:-$(hostname)}"
|
notification+="<br><b>System:</b> ${SITE_NAME:-$(hostname)}"
|
||||||
notification+="<br>Available disk space: $(df -h "$HOME/BirdSongs" | awk 'NR==2 {print $4}')"
|
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>"
|
[[ -n "$BIRDNETPI_URL" ]] && notification+="<br><a href=\"$BIRDNETPI_URL\">Access your BirdNET-Pi</a>"
|
||||||
[[ -n "$no_files_alert" ]] && notification+="<br><b>Alert:</b> No files in folder for at least 30 seconds."
|
|
||||||
|
|
||||||
# Send notification
|
# Send notification
|
||||||
TITLE="BirdNET-Analyzer stopped"
|
TITLE="BirdNET-Analyzer Alert"
|
||||||
"$HOME/BirdNET-Pi/birdnet/bin/apprise" -vv -t "$TITLE" -b "$notification" --input-format=html --config="$HOME/BirdNET-Pi/apprise.txt"
|
if [[ -f "$HOME/BirdNET-Pi/birdnet/bin/apprise" && -s "$HOME/BirdNET-Pi/apprise.txt" ]]; then
|
||||||
|
"$HOME/BirdNET-Pi/birdnet/bin/apprise" -vv -t "$TITLE" -b "$notification" --input-format=html --config="$HOME/BirdNET-Pi/apprise.txt"
|
||||||
|
else
|
||||||
|
log_red "Apprise not configured or missing!"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main loop
|
# Main loop
|
||||||
@@ -65,7 +69,8 @@ while true; do
|
|||||||
if ((counter <= 0)); then
|
if ((counter <= 0)); then
|
||||||
current_file="$(cat "$ingest_dir/analyzing_now.txt")"
|
current_file="$(cat "$ingest_dir/analyzing_now.txt")"
|
||||||
if [[ "$current_file" == "$analyzing_now" ]]; then
|
if [[ "$current_file" == "$analyzing_now" ]]; then
|
||||||
log_yellow "$(date) WARNING: no change in analyzing_now for 10 iterations, restarting services"
|
log_yellow "$(date) WARNING: No change in analyzing_now for 10 iterations, restarting services"
|
||||||
|
apprisealert "No change in analyzing_now for 10 iterations"
|
||||||
"$HOME/BirdNET-Pi/scripts/restart_services.sh"
|
"$HOME/BirdNET-Pi/scripts/restart_services.sh"
|
||||||
fi
|
fi
|
||||||
counter=10
|
counter=10
|
||||||
@@ -79,28 +84,21 @@ while true; do
|
|||||||
|
|
||||||
log_green "$(date) INFO: $wav_count wav files waiting in $ingest_dir, $srv state is $service_state, $srv2 state is $analysis_state"
|
log_green "$(date) INFO: $wav_count wav files waiting in $ingest_dir, $srv state is $service_state, $srv2 state is $analysis_state"
|
||||||
|
|
||||||
# Alert if no files in the folder for at least 30 seconds
|
|
||||||
if ((wav_count == 0)); then
|
|
||||||
((no_files_counter++))
|
|
||||||
if ((no_files_counter >= 1)); then
|
|
||||||
log_red "$(date) WARNING: No files in folder for at least 30 seconds"
|
|
||||||
if [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]]; then
|
|
||||||
apprisealert "no_files"
|
|
||||||
fi
|
|
||||||
no_files_counter=0
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
no_files_counter=0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Pause recorder if queue is too large
|
# Pause recorder if queue is too large
|
||||||
if ((wav_count > 50)); then
|
if ((wav_count > 50)); then
|
||||||
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"
|
||||||
|
apprisealert "Too many files in queue (>50)"
|
||||||
sudo systemctl stop "$srv"
|
sudo systemctl stop "$srv"
|
||||||
sudo systemctl restart "$srv2"
|
sudo systemctl restart "$srv2"
|
||||||
sleep 30
|
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"
|
||||||
|
apprisealert "Queue growing large (>30)"
|
||||||
|
sudo systemctl restart "$srv2"
|
||||||
|
sleep 30
|
||||||
|
elif ((wav_count == 0)); then
|
||||||
|
log_red "$(date) WARNING: No files in queue, restarting $srv2"
|
||||||
|
apprisealert "No files in queue"
|
||||||
sudo systemctl restart "$srv2"
|
sudo systemctl restart "$srv2"
|
||||||
sleep 30
|
sleep 30
|
||||||
fi
|
fi
|
||||||
@@ -110,14 +108,10 @@ while true; do
|
|||||||
state="$(systemctl is-active "$service")"
|
state="$(systemctl is-active "$service")"
|
||||||
if [[ "$state" != "active" ]]; then
|
if [[ "$state" != "active" ]]; then
|
||||||
log_yellow "$(date) INFO: Restarting $service service"
|
log_yellow "$(date) INFO: Restarting $service service"
|
||||||
|
apprisealert "$service is not active, restarting"
|
||||||
sudo systemctl restart "$service"
|
sudo systemctl restart "$service"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# Send alert if needed
|
|
||||||
if ((wav_count > 30)) && [[ -s "$HOME/BirdNET-Pi/apprise.txt" ]]; then
|
|
||||||
apprisealert
|
|
||||||
fi
|
|
||||||
|
|
||||||
((counter--))
|
((counter--))
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user