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";
@ -23,7 +28,8 @@
gatus = {
enable = true;
domain = "status.${config.custom.services.tailscale.domain}";
endpointDomains = config.meta.domains.globalList;
domainsToMonitor = config.meta.domains.globalList;
hostsToMonitor = self.nixosConfigurations |> lib.attrNames;
customEndpoints = {
"alerts" = {
group = "Monitoring";

View file

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