Health Checks eingebaut

This commit is contained in:
hubobel 2026-01-11 17:45:47 +01:00
parent 613df62e32
commit 62b2ee23f4
5 changed files with 67 additions and 5 deletions

1
Python/Cron Normal file
View file

@ -0,0 +1 @@
@reboot sleep 30 && /usr/bin/python3 /home/christof/lcd/test3.py >> /var/log/start_after_boot.log 2>&1

60
Python/LCD Normal file
View file

@ -0,0 +1,60 @@
import subprocess
import time
import sys
CONTAINERS = ["telegraf", "influx-influxdb-1", "grafana", "mqtt5"] # <- Container-Namen anpassen
TIMEOUT_S = 300
SLEEP_S = 2
def sh(cmd: list[str]) -> str:
return subprocess.check_output(cmd, text=True).strip()
def is_running(name: str) -> bool:
try:
state = sh(["docker", "inspect", "-f", "{{.State.Running}}", name])
return state.lower() == "true"
except Exception:
return False
def health(name: str) -> str:
# returns: healthy, unhealthy, starting, or "none" if no healthcheck
try:
return sh(["docker", "inspect", "-f", "{{.State.Health.Status}}", name])
except Exception:
return "none"
def main():
deadline = time.time() + TIMEOUT_S
while time.time() < deadline:
ok = True
for c in CONTAINERS:
if not is_running(c):
ok = False
"break"
h = health(c)
print(h)
if h == "none":
# Falls ein Container keinen Healthcheck hat:
# -> kannst du hier alternativ eine Portprüfung machen oder es als "ok" behandeln.
ok = False
break
if h != "healthy":
ok = False
break
if ok:
print("Alle Container sind running + healthy.")
subprocess.run(
["python3", "test.py"],
check=True
)
return 0
time.sleep(SLEEP_S)
print("Timeout: Container nicht rechtzeitig healthy.", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())

3
Python/README-md Normal file
View file

@ -0,0 +1,3 @@
python3 -m pip config set global.break-system-packages true
sudo pip3 install adafruit-blinka

View file

@ -4,10 +4,8 @@ services:
image: eclipse-mosquitto image: eclipse-mosquitto
container_name: mqtt5 container_name: mqtt5
healthcheck: healthcheck:
test: [ "CMD-SHELL", "mosquitto_sub -h localhost -p 1883 -t '$SYS/#' -C 1 -W 2 >/dev/null 2>&1 || exit 1" ] test: [ "CMD-SHELL", "mosquitto_pub -h localhost -p 1883 -u 'mqtt_user' -P 'mqtt_pw' -t 'healthcheck/ping' -n -q 0 >/dev/null 2>&1 || exit 1" ]
interval: 10s
timeout: 5s
retries: 12
ports: ports:
- "1883:1883" #default mqtt port - "1883:1883" #default mqtt port
- "9001:9001" #default mqtt port for websockets - "9001:9001" #default mqtt port for websockets

View file

@ -4,7 +4,7 @@ services:
container_name: telegraf container_name: telegraf
restart: unless-stopped restart: unless-stopped
healthcheck: healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:8094" ] test: [ "CMD-SHELL", "pidof telegraf >/dev/null 2>&1 || exit 1" ]
interval: 10s interval: 10s
timeout: 3s timeout: 3s
retries: 12 retries: 12