From 7a9796e02cab26c4fe120e5ed2d6e4656f4dfbb2 Mon Sep 17 00:00:00 2001 From: SebastianStork Date: Sun, 15 Jun 2025 00:02:13 +0200 Subject: [PATCH] Declare gatus endpoints in each of their respective modules --- .../system/services/actualbudget/default.nix | 5 + modules/system/services/forgejo/default.nix | 12 ++ modules/system/services/gatus.nix | 130 +++++++----------- modules/system/services/hedgedoc/default.nix | 6 + modules/system/services/nextcloud/default.nix | 10 ++ modules/system/services/ntfy.nix | 6 + modules/system/services/syncthing/default.nix | 12 ++ 7 files changed, 104 insertions(+), 77 deletions(-) diff --git a/modules/system/services/actualbudget/default.nix b/modules/system/services/actualbudget/default.nix index cbdeaae..6294caa 100644 --- a/modules/system/services/actualbudget/default.nix +++ b/modules/system/services/actualbudget/default.nix @@ -25,5 +25,10 @@ in inherit (cfg) port; }; }; + + custom.services.gatus.endpoints."Actual Budget" = { + group = "Private"; + url = "https://${cfg.domain}/"; + }; }; } diff --git a/modules/system/services/forgejo/default.nix b/modules/system/services/forgejo/default.nix index 96cb536..1336a7c 100644 --- a/modules/system/services/forgejo/default.nix +++ b/modules/system/services/forgejo/default.nix @@ -93,5 +93,17 @@ in "d ${config.services.forgejo.customDir}/public 750 ${user} ${group} - -" "L+ ${config.services.forgejo.customDir}/public/robots.txt 750 - - - ${disallow-all-robots}" ]; + + custom.services.gatus.endpoints = { + "Forgejo" = { + group = "Public"; + url = "https://${cfg.domain}/api/healthz"; + extraConditions = [ "[BODY].status == pass" ]; + }; + "Forgejo SSH" = { + group = "Public"; + url = "ssh://${cfg.domain}"; + }; + }; }; } diff --git a/modules/system/services/gatus.nix b/modules/system/services/gatus.nix index 9048eaf..918d01a 100644 --- a/modules/system/services/gatus.nix +++ b/modules/system/services/gatus.nix @@ -1,4 +1,9 @@ -{ config, lib, ... }: +{ + config, + self, + lib, + ... +}: let cfg = config.custom.services.gatus; @@ -15,6 +20,34 @@ in type = lib.types.port; default = 8080; }; + endpoints = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule ( + { name, ... }: + { + options = { + name = lib.mkOption { + type = lib.types.nonEmptyStr; + default = name; + }; + group = lib.mkOption { + type = lib.types.str; + default = ""; + }; + url = lib.mkOption { + type = lib.types.nonEmptyStr; + default = ""; + }; + extraConditions = lib.mkOption { + type = lib.types.listOf lib.types.nonEmptyStr; + default = [ ]; + }; + }; + } + ) + ); + default = { }; + }; }; config = lib.mkIf cfg.enable { @@ -54,82 +87,25 @@ in }; endpoints = - let - mkHttpCheck = - { - name, - group, - url, - conditions ? [ ], - }: - { - inherit name group url; - conditions = [ "[STATUS] == 200" ] ++ conditions; - interval = "30s"; - alerts = [ { type = "ntfy"; } ]; - }; - in - [ - { - name = "Syncthing"; - group = "Private"; - url = "tcp://alto.${tailscaleDomain}:22000"; - conditions = [ "[CONNECTED] == true" ]; - interval = "30s"; - alerts = [ { type = "ntfy"; } ]; - } - (mkHttpCheck { - name = "Syncthing GUI"; - group = "Private"; - url = "https://syncthing.${tailscaleDomain}/rest/noauth/health"; - conditions = [ "[BODY].status == OK" ]; - }) - (mkHttpCheck { - name = "Nextcloud"; - group = "Private"; - url = "https://cloud.${tailscaleDomain}/status.php"; - conditions = [ - "[BODY].installed == true" - "[BODY].maintenance == false" - "[BODY].needsDbUpgrade == false" - ]; - }) - (mkHttpCheck { - name = "Actual Budget"; - group = "Private"; - url = "https://budget.${tailscaleDomain}/"; - }) - (mkHttpCheck { - name = "Hedgedoc"; - group = "Public"; - url = "https://docs.sprouted.cloud/_health"; - conditions = [ "[BODY].ready == true" ]; - }) - (mkHttpCheck { - name = "Forgejo"; - group = "Public"; - url = "https://git.sstork.dev/api/healthz"; - conditions = [ "[BODY].status == pass" ]; - }) - { - name = "Forgejo SSH"; - group = "Public"; - url = "ssh://git.sstork.dev"; - ssh = { - username = ""; - password = ""; - }; - conditions = [ "[CONNECTED] == true" ]; - interval = "30s"; - alerts = [ { type = "ntfy"; } ]; - } - (mkHttpCheck { - name = "Ntfy"; - group = "Monitoring"; - url = "https://alerts.${tailscaleDomain}/v1/health"; - conditions = [ "[BODY].healthy == true" ]; - }) - ]; + self.nixosConfigurations + |> lib.mapAttrsToList (_: value: value.config.custom.services.gatus.endpoints) + |> lib.map (entry: lib.mapAttrsToList (_: value: value) entry) + |> lib.flatten + |> lib.map (value: { + inherit (value) name group url; + interval = "30s"; + alerts = [ { type = "ntfy"; } ]; + ssh = lib.mkIf (lib.hasPrefix "ssh" value.url) { + username = ""; + password = ""; + }; + conditions = lib.flatten [ + value.extraConditions + (lib.optional (lib.hasPrefix "http" value.url) "[STATUS] == 200") + (lib.optional (lib.hasPrefix "tcp" value.url) "[CONNECTED] == true") + (lib.optional (lib.hasPrefix "ssh" value.url) "[CONNECTED] == true") + ]; + }); }; }; }; diff --git a/modules/system/services/hedgedoc/default.nix b/modules/system/services/hedgedoc/default.nix index d144347..4a4fae8 100644 --- a/modules/system/services/hedgedoc/default.nix +++ b/modules/system/services/hedgedoc/default.nix @@ -79,5 +79,11 @@ in }; environment.shellAliases.hedgedoc-manage-users = "sudo --user=${user} ${manage_users}"; + + custom.services.gatus.endpoints."Hedgedoc" = { + group = "Public"; + url = "https://${cfg.domain}/_health"; + extraConditions = [ "[BODY].ready == true" ]; + }; }; } diff --git a/modules/system/services/nextcloud/default.nix b/modules/system/services/nextcloud/default.nix index 0300909..1054e1d 100644 --- a/modules/system/services/nextcloud/default.nix +++ b/modules/system/services/nextcloud/default.nix @@ -72,5 +72,15 @@ in }; }; }; + + custom.services.gatus.endpoints."Nextcloud" = { + group = "Private"; + url = "https://${cfg.domain}/status.php"; + extraConditions = [ + "[BODY].installed == true" + "[BODY].maintenance == false" + "[BODY].needsDbUpgrade == false" + ]; + }; }; } diff --git a/modules/system/services/ntfy.nix b/modules/system/services/ntfy.nix index b86f47a..1cb30e7 100644 --- a/modules/system/services/ntfy.nix +++ b/modules/system/services/ntfy.nix @@ -27,5 +27,11 @@ in web-root = "disable"; }; }; + + custom.services.gatus.endpoints."Ntfy" = { + group = "Monitoring"; + url = "https://${cfg.domain}/v1/health"; + extraConditions = [ "[BODY].healthy == true" ]; + }; }; } diff --git a/modules/system/services/syncthing/default.nix b/modules/system/services/syncthing/default.nix index 5cea27a..6abfbb9 100644 --- a/modules/system/services/syncthing/default.nix +++ b/modules/system/services/syncthing/default.nix @@ -97,5 +97,17 @@ in }; }; }; + + custom.services.gatus.endpoints = lib.mkIf cfg.isServer { + "Syncthing" = { + group = "Private"; + url = "tcp://${config.networking.hostName}.${tailscaleCfg.domain}:22000"; + }; + "Syncthing GUI" = { + group = "Private"; + url = "https://${cfg.gui.domain}/rest/noauth/health"; + extraConditions = [ "[BODY].status == OK" ]; + }; + }; }; }