From 3d551fab52461de35379b2403ddd86d5af0ab54c Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 21 Jul 2026 16:15:05 +0900 Subject: [PATCH] ildkule/gatus: add healthcheck for mariadb --- .../services/monitoring/gatus/default.nix | 7 ++ .../monitoring/gatus/mariadb-checker.nix | 79 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 hosts/ildkule/services/monitoring/gatus/mariadb-checker.nix diff --git a/hosts/ildkule/services/monitoring/gatus/default.nix b/hosts/ildkule/services/monitoring/gatus/default.nix index 0b765d9..ff60c35 100644 --- a/hosts/ildkule/services/monitoring/gatus/default.nix +++ b/hosts/ildkule/services/monitoring/gatus/default.nix @@ -6,6 +6,7 @@ in imports = [ ./minecraft-checker.nix ./postgres-checker.nix + ./mariadb-checker.nix ]; services.gatus = { @@ -140,6 +141,12 @@ in "[BODY].ok == true" ]; }) + (mkService "MariaDB" "http://localhost:1339" // { + conditions = [ + "[STATUS] == 200" + "[BODY].ok == true" + ]; + }) (mkService "Email (SMTP)" "starttls://mail.pvv.ntnu.no:587") (mkService "Email (POP3)" "tls://mail.pvv.ntnu.no:995") (mkService "Email (IMAP)" "tls://mail.pvv.ntnu.no:993") diff --git a/hosts/ildkule/services/monitoring/gatus/mariadb-checker.nix b/hosts/ildkule/services/monitoring/gatus/mariadb-checker.nix new file mode 100644 index 0000000..d22402d --- /dev/null +++ b/hosts/ildkule/services/monitoring/gatus/mariadb-checker.nix @@ -0,0 +1,79 @@ +{ config, pkgs, ... }: +{ + sops.secrets."keys/gatus/mariadb" = { + restartUnits = [ "phh-gatus-mariadb-checker.service" ]; + }; + + sops.templates."gatus-mariadb-checker.env" = { + restartUnits = [ "phh-gatus-mariadb-checker.service" ]; + content = '' + MYSQL_HOST=mysql.pvv.ntnu.no + MYSQL_PORT=3306 + MYSQL_DATABASE=mysql + MYSQL_USER=gatus_healthcheck + MYSQL_PASSWORD=${config.sops.placeholder."keys/gatus/mariadb"} + ''; + }; + + services.python-http-handlers."gatus-mariadb-checker" = { + listenStreams = [ "127.0.0.1:1339" ]; + libraries = with pkgs.python3Packages; [ + pymysql + ]; + serviceConfig = { + EnvironmentFile = config.sops.templates."gatus-mariadb-checker.env".path; + }; + handler = '' + import os + import json + import pymysql + + class Handler(BaseHTTPRequestHandler): + def do_GET(self): + try: + conn = pymysql.connect( + host=os.environ["MYSQL_HOST"], + port=int(os.environ.get("MYSQL_PORT", "3306")), + user=os.environ["MYSQL_USER"], + password=os.environ["MYSQL_PASSWORD"], + database=os.environ.get("MYSQL_DATABASE", "mysql"), + connect_timeout=5, + ) + + try: + with conn.cursor() as cur: + cur.execute("SELECT VERSION();") + (version,) = cur.fetchone() + + cur.execute("SHOW STATUS LIKE 'Threads_connected';") + (_, connections) = cur.fetchone() + + cur.execute("SHOW DATABASES;") + databases = cur.fetchall() + finally: + conn.close() + + body = { + "ok": True, + "version": version, + "connections": int(connections), + "databases": len(databases), + } + data = json.dumps(body).encode() + + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + except Exception as e: + data = json.dumps({"ok": False, "error": str(e)}).encode() + self.send_response(500) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + ''; + }; +}