Also monitor hosts with gatus

This commit is contained in:
SebastianStork 2025-07-10 10:57:04 +02:00
parent 74cb6b3cb1
commit 893a488be1
2 changed files with 110 additions and 85 deletions

View file

@ -1,4 +1,9 @@
{ config, ... }: {
config,
self,
lib,
...
}:
{ {
system.stateVersion = "24.11"; system.stateVersion = "24.11";
@ -23,7 +28,8 @@
gatus = { gatus = {
enable = true; enable = true;
domain = "status.${config.custom.services.tailscale.domain}"; domain = "status.${config.custom.services.tailscale.domain}";
endpointDomains = config.meta.domains.globalList; domainsToMonitor = config.meta.domains.globalList;
hostsToMonitor = self.nixosConfigurations |> lib.attrNames;
customEndpoints = { customEndpoints = {
"alerts" = { "alerts" = {
group = "Monitoring"; group = "Monitoring";

View file

@ -1,7 +1,10 @@
{ config, lib, ... }: { config, lib, ... }:
let let
cfg = config.custom.services.gatus; cfg = config.custom.services.gatus;
in
{
options.custom.services.gatus =
let
endpointType = lib.types.attrsOf ( endpointType = lib.types.attrsOf (
lib.types.submodule ( lib.types.submodule (
{ name, ... }: { name, ... }:
@ -27,22 +30,37 @@ let
type = lib.types.listOf lib.types.nonEmptyStr; type = lib.types.listOf lib.types.nonEmptyStr;
default = [ ]; default = [ ];
}; };
enableAlerts = lib.mkEnableOption "" // {
default = true;
};
}; };
} }
) )
); );
defaultEndpoints = defaultDomainEndpoints =
let let
getSubdomain = domain: domain |> lib.splitString "." |> lib.head; getSubdomain = domain: domain |> lib.splitString "." |> lib.head;
in in
cfg.endpointDomains cfg.domainsToMonitor
|> lib.filter (domain: domain != cfg.domain) |> lib.filter (domain: domain != cfg.domain)
|> lib.map (domain: lib.nameValuePair (getSubdomain domain) { url = "https://${domain}"; }) |> lib.map (domain: lib.nameValuePair (getSubdomain domain) { url = "https://${domain}"; })
|> lib.listToAttrs; |> lib.listToAttrs;
in
{ defaultHostEndpoints =
options.custom.services.gatus = { cfg.hostsToMonitor
|> lib.filter (hostName: hostName != config.networking.hostName)
|> lib.map (
hostName:
lib.nameValuePair hostName {
group = "Hosts";
url = "icmp://${hostName}.${config.custom.services.tailscale.domain}";
enableAlerts = false;
}
)
|> lib.listToAttrs;
in
{
enable = lib.mkEnableOption ""; enable = lib.mkEnableOption "";
domain = lib.mkOption { domain = lib.mkOption {
type = lib.types.nonEmptyStr; type = lib.types.nonEmptyStr;
@ -52,7 +70,11 @@ in
type = lib.types.port; type = lib.types.port;
default = 8080; default = 8080;
}; };
endpointDomains = lib.mkOption { domainsToMonitor = lib.mkOption {
type = lib.types.listOf lib.types.nonEmptyStr;
default = [ ];
};
hostsToMonitor = lib.mkOption {
type = lib.types.listOf lib.types.nonEmptyStr; type = lib.types.listOf lib.types.nonEmptyStr;
default = [ ]; default = [ ];
}; };
@ -62,7 +84,7 @@ in
}; };
finalEndpoints = lib.mkOption { finalEndpoints = lib.mkOption {
type = endpointType; type = endpointType;
default = defaultEndpoints // cfg.customEndpoints; default = defaultDomainEndpoints // defaultHostEndpoints // cfg.customEndpoints;
readOnly = true; readOnly = true;
}; };
}; };
@ -88,8 +110,8 @@ in
services.gatus = { services.gatus = {
enable = true; enable = true;
environmentFile = config.sops.templates."gatus.env".path;
environmentFile = config.sops.templates."gatus.env".path;
settings = { settings = {
web.port = cfg.port; web.port = cfg.port;
@ -100,7 +122,7 @@ in
maximum-number-of-events = 100; maximum-number-of-events = 100;
}; };
connectivity.checker.target = "1.1.1.1:53"; connectivity.checker.target = "1.1.1.1:53"; # Cloudflare DNS
alerting.ntfy = { alerting.ntfy = {
topic = "uptime"; topic = "uptime";
@ -133,37 +155,34 @@ in
endpoints = endpoints =
let let
mkEndpoint = ( mkEndpoint =
{ value:
name,
group,
url,
interval,
extraConditions,
}:
let let
isPrivate = lib.hasInfix config.custom.services.tailscale.domain url; isPrivate = lib.hasInfix config.custom.services.tailscale.domain value.url;
deducedGroup = if isPrivate then "Private" else "Public"; deducedGroup = if isPrivate then "Private" else "Public";
in in
{ {
inherit name url interval; inherit (value) name url interval;
group = if group != null then group else deducedGroup; group = if value.group != null then value.group else deducedGroup;
alerts = [ { type = "ntfy"; } ]; alerts = lib.mkIf value.enableAlerts [ { type = "ntfy"; } ];
ssh = lib.mkIf (lib.hasPrefix "ssh" url) { ssh = lib.mkIf (lib.hasPrefix "ssh" value.url) {
username = ""; username = "";
password = ""; password = "";
}; };
conditions = lib.concatLists [ conditions = lib.concatLists [
extraConditions value.extraConditions
(lib.optional (lib.hasPrefix "http" url) "[STATUS] == 200") (lib.optional (lib.hasPrefix "http" value.url) "[STATUS] == 200")
(lib.optional (lib.hasPrefix "tcp" url) "[CONNECTED] == true") (lib.optional (lib.hasPrefix "tcp" value.url) "[CONNECTED] == true")
(lib.optional (lib.hasPrefix "ssh" url) "[CONNECTED] == true") (lib.optional (lib.hasPrefix "ssh" value.url) "[CONNECTED] == true")
(lib.optional (lib.hasPrefix "icmp" value.url) "[CONNECTED] == true")
]; ];
} };
);
in in
cfg.finalEndpoints |> lib.mapAttrsToList (_: value: value) |> lib.map (entry: mkEndpoint entry); cfg.finalEndpoints |> lib.mapAttrsToList (_: value: value) |> lib.map (entry: mkEndpoint entry);
}; };
}; };
systemd.services.gatus.serviceConfig.AmbientCapabilities = "CAP_NET_RAW"; # Allow icmp/pings
}; };
} }