diff --git a/Python/Cron b/Python/Cron new file mode 100644 index 0000000..0153110 --- /dev/null +++ b/Python/Cron @@ -0,0 +1 @@ +@reboot sleep 30 && /usr/bin/python3 /home/christof/lcd/test3.py >> /var/log/start_after_boot.log 2>&1 \ No newline at end of file diff --git a/Python/LCD b/Python/LCD new file mode 100644 index 0000000..839d4c5 --- /dev/null +++ b/Python/LCD @@ -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()) \ No newline at end of file diff --git a/Python/README-md b/Python/README-md new file mode 100644 index 0000000..05c2ff2 --- /dev/null +++ b/Python/README-md @@ -0,0 +1,3 @@ +python3 -m pip config set global.break-system-packages true + +sudo pip3 install adafruit-blinka diff --git a/mqtt/docker-compose.yml b/mqtt/docker-compose.yml index 09cff99..1e0e384 100644 --- a/mqtt/docker-compose.yml +++ b/mqtt/docker-compose.yml @@ -4,10 +4,8 @@ services: image: eclipse-mosquitto container_name: mqtt5 healthcheck: - test: [ "CMD-SHELL", "mosquitto_sub -h localhost -p 1883 -t '$SYS/#' -C 1 -W 2 >/dev/null 2>&1 || exit 1" ] - interval: 10s - timeout: 5s - retries: 12 + 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" ] + ports: - "1883:1883" #default mqtt port - "9001:9001" #default mqtt port for websockets diff --git a/telegraf/docker-compose.yml b/telegraf/docker-compose.yml index ae11c4f..0c9974b 100644 --- a/telegraf/docker-compose.yml +++ b/telegraf/docker-compose.yml @@ -4,7 +4,7 @@ services: container_name: telegraf restart: unless-stopped healthcheck: - test: [ "CMD", "curl", "-f", "http://localhost:8094" ] + test: [ "CMD-SHELL", "pidof telegraf >/dev/null 2>&1 || exit 1" ] interval: 10s timeout: 3s retries: 12