diff --git a/ibsystem/ibsystem2mqtt_watchdog.sh b/ibsystem/ibsystem2mqtt_watchdog.sh index f0a88fb..6b331ab 100755 --- a/ibsystem/ibsystem2mqtt_watchdog.sh +++ b/ibsystem/ibsystem2mqtt_watchdog.sh @@ -1,39 +1,23 @@ -# Watchdog dla ibsystem2mqtt -HEALTH_URL="http://127.0.0.1:8080/health" -LOG_FILE="/tmp/ibsystem2mqtt_watchdog.log" +#!/bin/bash +HEALTH_URL=http://127.0.0.1:8080/health +FAILURE_FILE=/tmp/ibsystem2mqtt_failures MAX_FAILURES=3 -FAILURE_COUNT_FILE="/tmp/ibsystem2mqtt_failures" -log() { echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE"; } - -check_health() { - response=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$HEALTH_URL" 2>/dev/null) - [ "$response" = "200" ] -} - -reset_failures() { echo "0" > "$FAILURE_COUNT_FILE"; } - -increment_failures() { - [ -f "$FAILURE_COUNT_FILE" ] && count=$(cat "$FAILURE_COUNT_FILE") || count=0 - count=$((count + 1)) - echo "$count" > "$FAILURE_COUNT_FILE" - echo "$count" -} - -if check_health; then - reset_failures - exit 0 +if curl -sf --max-time 5 "$HEALTH_URL" > /dev/null 2>&1; then + echo 0 > "$FAILURE_FILE" +else + FAILURES=$(cat "$FAILURE_FILE" 2>/dev/null || echo 0) + FAILURES=$((FAILURES + 1)) + echo $FAILURES > "$FAILURE_FILE" + if [ $FAILURES -ge $MAX_FAILURES ]; then + systemctl restart ibsystem2mqtt + echo 0 > "$FAILURE_FILE" + fi fi -failures=$(increment_failures) -log "Health check failed ($failures/$MAX_FAILURES)" - -if [ "$failures" -ge "$MAX_FAILURES" ]; then - log "Max failures reached, restarting" - pid=$(pgrep -f 'ibsystem2mqtt' | head -1) - [ -n "$pid" ] && kill "$pid" 2>/dev/null && sleep 3 - cd /ibsystem - nohup python3 ibsystem2mqtt_v5.py -c config.yaml >> /tmp/ibsystem2mqtt_stdout.log 2>&1 & - log "Restarted (new PID: $!)" - reset_failures +AVAIL=$(mosquitto_sub -h 192.168.50.151 -p 1883 -u mqtt -P mqtt123 \ + -t ibsystem/bridge/availability -C 1 --quiet 2>/dev/null) +if [ "$AVAIL" = "offline" ] && systemctl is-active --quiet ibsystem2mqtt; then + logger -t ibsystem_watchdog "availability=offline - restarting" + systemctl restart ibsystem2mqtt fi