diff --git a/Dockerfile b/Dockerfile index 83039dc..63441cc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,10 @@ COPY requirements.txt /tmp RUN pip3 install --no-cache-dir -r /tmp/requirements.txt COPY exporter.py /usr/local/bin/homematic_exporter +COPY healthcheck.sh /usr/local/bin/healthcheck.sh ENTRYPOINT [ "/usr/local/bin/homematic_exporter" ] EXPOSE 8010 +HEALTHCHECK --interval=20s --timeout=3s \ + CMD bash /usr/local/bin/healthcheck.sh diff --git a/healthcheck.sh b/healthcheck.sh new file mode 100644 index 0000000..4d4c84e --- /dev/null +++ b/healthcheck.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# First argument is the maximum age in seconds (default: 60) +maxage=${1:-60} + +# Get the age of the last successful metrics refresh in seconds +age=$(curl -s http://localhost:9040/metrics | grep 'homematic_refresh_age{' | cut -d ' ' -f2 | cut -d '.' -f1) + +if [[ $age -lt $maxage ]]; then + exit 0 +else + # Maximum age exceeded → unhealthy + exit 1 +fi