commit 4b982aea08e01fcdbf81857cc6dcbf33b33a8df4 Author: irrelevant Date: Mon Oct 5 00:30:04 2026 +0100 initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6dbc6ff --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.12-slim + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY app.py . + +EXPOSE 8080 + +CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"] + diff --git a/app.py b/app.py new file mode 100644 index 0000000..a3fbdf1 --- /dev/null +++ b/app.py @@ -0,0 +1,114 @@ +import os +import requests +import urllib3 +from requests.auth import HTTPBasicAuth + +from flask import Flask, request, Response + +# Suppress warnings about self-signed certificates +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + +app = Flask(__name__) + + +api_key = os.getenv("API_KEY") +api_secret = os.getenv("API_SECRET") +API_URL = os.getenv("API_URL") +HEALTHY_STATUS = os.getenv("HEALTHY_STATUS") +JSONROWS = os.getenv("JSONROWS") + + +@app.route("/check") +def check(): + name = request.args.get("name") + + if not name: + return Response( + "Missing name parameter\n", + status=400, + mimetype="text/plain" + ) + + try: + session = requests.Session() + session.auth = HTTPBasicAuth(api_key, api_secret) + session.headers.update({"Accept": "application/json"}) + + response = session.get( + API_URL, + timeout=10, + verify=False, + ) + + response.raise_for_status() + + data = response.json() + + gateway = next( + ( + row for row in data.get(JSONROWS, []) + if row.get("name", "").lower() == name.lower() + ), + None + ) + + + if gateway is None: + return Response( + f"{name} not found\n", + status=400, + mimetype="text/plain" + ) + + status = gateway.get("status", "").lower() + + if status == HEALTHY_STATUS: + return Response( + "OK\n", + status=200, + mimetype="text/plain" + ) + + return Response( + f"Status is {status}\n", + status=503, + mimetype="text/plain" + ) + + except requests.exceptions.RequestException as ex: + return Response( + f"API communication error: {ex}\n", + status=500, + mimetype="text/plain" + ) + + except ValueError: + return Response( + "Invalid JSON received from API\n", + status=500, + mimetype="text/plain" + ) + + except Exception as ex: + return Response( + f"Unexpected error: {ex}\n", + status=500, + mimetype="text/plain" + ) + + +@app.route("/health") +def health(): + return Response( + "healthy\n", + status=200, + mimetype="text/plain" + ) + + +if __name__ == "__main__": + app.run( + host="0.0.0.0", + port=8080 + ) + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ff2ee9c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +services: + host-check: + build: . + container_name: host-check + restart: unless-stopped + ports: + - "8765:8080" + environment: + API_KEY: 'key' + API_SECRET: 'secret' +# API_URL: "https://192.168.0.23/api/routes/gateway/status" +# HEALTHY_STATUS: "none" +# JSONROWS: "items" + API_URL: "https://192.168.0.23/api/routing/settings/searchGateway" # Requires System: Gateways + JSONROWS: "rows" + HEALTHY_STATUS: "online" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d2d1c02 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +flask +requests +gunicorn +