gatus: use the usual endpoints framework for healthchecks.io

This commit is contained in:
SebastianStork 2025-06-29 21:53:50 +02:00
parent 33dbfbced2
commit 9c5c7cd9ad

View file

@ -2,19 +2,43 @@
let let
cfg = config.custom.services.gatus; cfg = config.custom.services.gatus;
endpointType = lib.types.attrsOf (
lib.types.submodule (
{ name, ... }:
{
options = {
name = lib.mkOption {
type = lib.types.nonEmptyStr;
default = name;
};
group = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
url = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "";
};
interval = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "30s";
};
extraConditions = lib.mkOption {
type = lib.types.listOf lib.types.nonEmptyStr;
default = [ ];
};
};
}
)
);
defaultEndpoints = defaultEndpoints =
let let
getSubdomain = domain: domain |> lib.splitString "." |> lib.head; getSubdomain = domain: domain |> lib.splitString "." |> lib.head;
in in
cfg.endpointDomains cfg.endpointDomains
|> lib.filter (domain: domain != cfg.domain) |> lib.filter (domain: domain != cfg.domain)
|> lib.map ( |> lib.map (domain: lib.nameValuePair (getSubdomain domain) { url = "https://${domain}"; })
domain:
lib.nameValuePair (getSubdomain domain) {
name = getSubdomain domain;
url = "https://${domain}";
}
)
|> lib.listToAttrs; |> lib.listToAttrs;
in in
{ {
@ -33,35 +57,11 @@ in
default = [ ]; default = [ ];
}; };
customEndpoints = lib.mkOption { customEndpoints = lib.mkOption {
type = lib.types.attrsOf ( type = endpointType;
lib.types.submodule (
{ name, ... }:
{
options = {
name = lib.mkOption {
type = lib.types.nonEmptyStr;
default = name;
};
group = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
url = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "";
};
extraConditions = lib.mkOption {
type = lib.types.listOf lib.types.nonEmptyStr;
default = [ ];
};
};
}
)
);
default = { }; default = { };
}; };
finalEndpoints = lib.mkOption { finalEndpoints = lib.mkOption {
type = lib.types.attrsOf lib.types.anything; type = endpointType;
default = defaultEndpoints // cfg.customEndpoints; default = defaultEndpoints // cfg.customEndpoints;
readOnly = true; readOnly = true;
}; };
@ -77,6 +77,12 @@ in
''; '';
}; };
custom.services.gatus.customEndpoints."healthchecks.io" = {
group = "Monitoring";
url = "https://hc-ping.com/\${HEALTHCHECKS_PING_KEY}/${config.networking.hostName}-gatus-uptime?create=1";
interval = "2h";
};
services.gatus = { services.gatus = {
enable = true; enable = true;
environmentFile = config.sops.templates."gatus.env".path; environmentFile = config.sops.templates."gatus.env".path;
@ -127,18 +133,18 @@ in
mkEndpoint = ( mkEndpoint = (
{ {
name, name,
group ? null, group,
url, url,
extraConditions ? [ ], interval,
extraConditions,
}: }:
let let
isPrivate = lib.hasInfix config.custom.services.tailscale.domain url; isPrivate = lib.hasInfix config.custom.services.tailscale.domain url;
deducedGroup = if isPrivate then "Private" else "Public"; deducedGroup = if isPrivate then "Private" else "Public";
in in
{ {
inherit name; inherit name url interval;
group = if group != null then group else deducedGroup; group = if group != null then group else deducedGroup;
url = url;
alerts = [ { type = "ntfy"; } ]; alerts = [ { type = "ntfy"; } ];
ssh = lib.mkIf (lib.hasPrefix "ssh" url) { ssh = lib.mkIf (lib.hasPrefix "ssh" url) {
username = ""; username = "";
@ -153,18 +159,7 @@ in
} }
); );
in in
[ cfg.finalEndpoints |> lib.mapAttrsToList (_: value: value) |> lib.map (entry: mkEndpoint entry);
{
name = "healthchecks.io";
group = "Monitoring";
url = "https://hc-ping.com/\${HEALTHCHECKS_PING_KEY}/${config.networking.hostName}-gatus-uptime?create=1";
interval = "2h";
conditions = [ "[STATUS] == 200" ];
}
]
++ (
cfg.finalEndpoints |> lib.mapAttrsToList (_: value: value) |> lib.map (entry: mkEndpoint entry)
);
}; };
}; };
}; };