gatus: Refactor

This commit is contained in:
SebastianStork 2025-09-02 22:54:00 +02:00
parent 02296be975
commit 6f53e67250
2 changed files with 86 additions and 77 deletions

View file

@ -26,14 +26,19 @@
enable = true; enable = true;
domain = "status.${config.custom.services.tailscale.domain}"; domain = "status.${config.custom.services.tailscale.domain}";
domainsToMonitor = config.meta.domains.globalList; domainsToMonitor = config.meta.domains.globalList;
customEndpoints = { endpoints = {
"alerts" = { "alerts" = {
group = "Monitoring"; group = "Monitoring";
url = "https://${config.custom.services.ntfy.domain}/v1/health"; path = "/v1/health";
extraConditions = [ "[BODY].healthy == true" ]; extraConditions = [ "[BODY].healthy == true" ];
}; };
"git ssh".url = "ssh://git.sstork.dev"; "grafana".group = "Monitoring";
"speedtest".url = "http://speedtest.sprouted.cloud"; "logs".group = "Monitoring";
"git ssh" = {
protocol = "ssh";
domain = "git.sstork.dev";
};
"speedtest".protocol = "http";
}; };
}; };

View file

@ -1,13 +1,27 @@
{ config, lib, ... }: { config, lib, ... }:
let let
cfg = config.custom.services.gatus; cfg = config.custom.services.gatus;
tailscaleCfg = config.custom.services.tailscale;
in in
{ {
options.custom.services.gatus = options.custom.services.gatus = {
let enable = lib.mkEnableOption "";
endpointType = lib.types.attrsOf ( domain = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "";
};
port = lib.mkOption {
type = lib.types.port;
default = 8080;
};
domainsToMonitor = lib.mkOption {
type = lib.types.listOf lib.types.nonEmptyStr;
default = [ ];
};
endpoints = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule ( lib.types.submodule (
{ name, ... }: { name, config, ... }:
{ {
options = { options = {
name = lib.mkOption { name = lib.mkOption {
@ -15,11 +29,23 @@ in
default = name; default = name;
}; };
group = lib.mkOption { group = lib.mkOption {
type = lib.types.nullOr lib.types.nonEmptyStr;
default = null;
};
url = lib.mkOption {
type = lib.types.nonEmptyStr; type = lib.types.nonEmptyStr;
default =
let
isTailscale = config.domain |> lib.hasSuffix tailscaleCfg.domain;
in
if isTailscale then "Private" else "Public";
};
protocol = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "https";
};
domain = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "";
};
path = lib.mkOption {
type = lib.types.str;
default = ""; default = "";
}; };
interval = lib.mkOption { interval = lib.mkOption {
@ -37,39 +63,8 @@ in
} }
) )
); );
defaultEndpoints =
let
getSubdomain = domain: domain |> lib.splitString "." |> lib.head;
in
cfg.domainsToMonitor
|> lib.filter (domain: domain != cfg.domain)
|> lib.map (domain: lib.nameValuePair (getSubdomain domain) { url = "https://${domain}"; })
|> lib.listToAttrs;
in
{
enable = lib.mkEnableOption "";
domain = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "";
};
port = lib.mkOption {
type = lib.types.port;
default = 8080;
};
domainsToMonitor = lib.mkOption {
type = lib.types.listOf lib.types.nonEmptyStr;
default = [ ];
};
customEndpoints = lib.mkOption {
type = endpointType;
default = { }; default = { };
}; };
finalEndpoints = lib.mkOption {
type = endpointType;
default = defaultEndpoints // cfg.customEndpoints;
readOnly = true;
};
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
@ -85,11 +80,26 @@ in
''; '';
}; };
custom.services.gatus.customEndpoints."healthchecks.io" = { custom.services.gatus.endpoints =
let
getSubdomain = domain: domain |> lib.splitString "." |> lib.head;
defaultEndpoints =
cfg.domainsToMonitor
|> lib.filter (domain: domain != cfg.domain)
|> lib.map (domain: lib.nameValuePair (getSubdomain domain) { domain = lib.mkDefault domain; })
|> lib.listToAttrs;
in
{
"healthchecks.io" = {
group = "Monitoring"; group = "Monitoring";
url = "https://hc-ping.com/\${HEALTHCHECKS_PING_KEY}/${config.networking.hostName}-gatus-uptime?create=1"; protocol = "https";
domain = "hc-ping.com";
path = "/\${HEALTHCHECKS_PING_KEY}/${config.networking.hostName}-gatus-uptime?create=1";
interval = "2h"; interval = "2h";
}; };
}
// defaultEndpoints;
services.gatus = { services.gatus = {
enable = true; enable = true;
@ -100,14 +110,11 @@ in
address = "localhost"; address = "localhost";
port = cfg.port; port = cfg.port;
}; };
storage = { storage = {
type = "sqlite"; type = "sqlite";
path = "/var/lib/gatus/data.db"; path = "/var/lib/gatus/data.db";
}; };
connectivity.checker.target = "1.1.1.1:53"; # Cloudflare DNS connectivity.checker.target = "1.1.1.1:53"; # Cloudflare DNS
alerting.ntfy = { alerting.ntfy = {
topic = "uptime"; topic = "uptime";
url = "https://alerts.${config.custom.services.tailscale.domain}"; url = "https://alerts.${config.custom.services.tailscale.domain}";
@ -130,7 +137,6 @@ in
} }
]; ];
}; };
maintenance = { maintenance = {
start = "03:00"; start = "03:00";
duration = "1h"; duration = "1h";
@ -139,31 +145,29 @@ in
endpoints = endpoints =
let let
mkEndpoint = mkEndpoint = value: {
value: inherit (value) name group interval;
let url = "${value.protocol}://${value.domain}${value.path}";
isPrivate = value.url |> lib.hasInfix config.custom.services.tailscale.domain;
deducedGroup = if isPrivate then "Private" else "Public";
in
{
inherit (value) name url interval;
group = if value.group != null then value.group else deducedGroup;
alerts = lib.mkIf value.enableAlerts [ { type = "ntfy"; } ]; alerts = lib.mkIf value.enableAlerts [ { type = "ntfy"; } ];
ssh = lib.mkIf (lib.hasPrefix "ssh" value.url) { ssh = lib.mkIf (value.protocol == "ssh") {
username = ""; username = "";
password = ""; password = "";
}; };
conditions = lib.concatLists [ conditions = lib.concatLists [
value.extraConditions value.extraConditions
(lib.optional (lib.hasPrefix "http" value.url) "[STATUS] == 200") (lib.optional (lib.elem value.protocol [
(lib.optional (lib.hasPrefix "tcp" value.url) "[CONNECTED] == true") "http"
(lib.optional (lib.hasPrefix "ssh" value.url) "[CONNECTED] == true") "https"
(lib.optional (lib.hasPrefix "icmp" value.url) "[CONNECTED] == true") ]) "[STATUS] == 200")
(lib.optional (lib.elem value.protocol [
"tcp"
"ssh"
"icmp"
]) "[CONNECTED] == true")
]; ];
}; };
in in
cfg.finalEndpoints |> lib.attrValues |> lib.map (entry: mkEndpoint entry); cfg.endpoints |> lib.attrValues |> lib.map (entry: mkEndpoint entry);
}; };
}; };
}; };