Skip to content

Commit c1fef62

Browse files
committed
Add provides.<name>.monitor option
1 parent 504c122 commit c1fef62

27 files changed

Lines changed: 538 additions & 370 deletions

File tree

hosts/bastion/profiles/caddy/default.nix

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
virtualHosts = let
2121
providedVirtualHosts =
2222
flake.lib.provides.caddyVirtualHostsForServices hosts.bastion
23-
(flake.lib.provides.allHTTPServices (flake.lib.provides.allServices
24-
(lib.filterAttrs (name: _value: name != "monitor")
25-
flake.nixosConfigurations)));
23+
(flake.lib.provides.allHTTPServices
24+
(flake.lib.provides.allServicesForHosts
25+
(lib.filterAttrs (name: _value: name != "monitor")
26+
flake.nixosConfigurations)));
2627
in {
2728
"e10.camp" = {
2829
logFormat = ''

hosts/monitor/profiles/caddy.nix

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{ flake, lib, hosts, ... }: {
22
services.caddy = {
33
virtualHosts = flake.lib.provides.caddyVirtualHostsForServices hosts.monitor
4-
(flake.lib.provides.allHTTPServices (flake.lib.provides.allServices
5-
(lib.filterAttrs (name: _value: name == "monitor")
6-
flake.nixosConfigurations)));
4+
(flake.lib.provides.allHTTPServices
5+
(flake.lib.provides.allServicesForHosts
6+
(lib.filterAttrs (name: _value: name == "monitor")
7+
flake.nixosConfigurations)));
78
};
89
}

lib/src/provides.nix

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ with lib;
44

55
let
66
# Get all services (entries in `provides`) from the given hosts
7-
allServices = hosts:
7+
allServicesForHosts = hosts:
88
lib.pipe hosts [
99
attrValues
1010
(map (host: host.config.provides or { }))
@@ -15,13 +15,34 @@ let
1515
allHTTPServices = services:
1616
filterAttrs (_name: value: value.http.proxy.enable) services;
1717

18+
# Get all monitored services
19+
allMonitoredServices = services:
20+
filterAttrs (_name: value: value.http.monitor.enable) services;
21+
1822
# Transform a set of services into Caddy virtual hosts
1923
caddyVirtualHostsForServices = caddyHost: services:
2024
mapAttrs' (name: attrs:
2125
let inherit (attrs) http;
2226
in nameValuePair http.proxy.domain (mkVirtualHost caddyHost name http))
2327
(filterAttrs (_name: attrs: attrs.http.proxy.enable) services);
2428

29+
gatusEndpointsForServices = services:
30+
mapAttrsToList (_name: mkEndpoint)
31+
(filterAttrs (_name: attrs: attrs.monitor.proxy.enable) services);
32+
33+
mkEndpoint = service:
34+
{
35+
enabled = true;
36+
alerts = [{
37+
enabled = true;
38+
type = "ntfy";
39+
description = "healthcheck failed";
40+
send-on-resolved = true;
41+
}];
42+
} // extraConfig // optionalAttrs service.http.proxy.protected {
43+
headers.Authorization = "Basic $AUTHELIA_BASIC_AUTH";
44+
};
45+
2546
# Makes a single virtual host for HTTP service provides
2647
mkVirtualHost = caddyHost: name: http:
2748
let
@@ -61,4 +82,7 @@ let
6182
${http.proxy.extraVirtualHostConfig}
6283
'';
6384
};
64-
in { inherit allServices allHTTPServices caddyVirtualHostsForServices; }
85+
in {
86+
inherit allServicesForHosts allHTTPServices allMonitoredServices
87+
caddyVirtualHostsForServices gatusEndpointsForServices;
88+
}

lib/src/strings.nix

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{ lib, ... }:
2+
3+
with lib;
4+
5+
let
6+
# TODO: Inefficient (mapping over whole list when not necessary)
7+
capitalizeString = let specialCases = { htpc = "HTPC"; };
8+
in str:
9+
if (hasAttr str specialCases) then
10+
specialCases.${str}
11+
else
12+
concatStrings
13+
(imap0 (i: l: if i == 0 then (toUpper l) else l) (stringToCharacters str));
14+
in { inherit capitalizeString; }

modules/nixos/provides.nix

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
{ config, lib, ... }:
1+
{ flake, config, lib, ... }:
22

33
with lib;
44

5-
{
5+
let nixosConfig = config;
6+
in {
67
options.provides = mkOption {
7-
type = types.attrsOf (types.submodule {
8+
type = types.attrsOf (types.submodule ({ config, ... }: {
89
options = {
910
name = mkOption {
1011
type = types.str;
@@ -17,7 +18,7 @@ with lib;
1718
host = mkOption {
1819
type = types.str;
1920
description = "The host of this HTTP service";
20-
default = config.networking.hostName;
21+
default = nixosConfig.networking.hostName;
2122
};
2223

2324
port = mkOption {
@@ -92,8 +93,62 @@ with lib;
9293
};
9394
};
9495
};
96+
97+
monitor = mkOption {
98+
type = types.submodule {
99+
options = {
100+
enable = mkEnableOption
101+
"Enabling monitoring of this service using Gatus";
102+
103+
name = mkOption {
104+
type = types.str;
105+
description = "Name of the service being monitored";
106+
default = config.name;
107+
};
108+
109+
url = mkOption {
110+
type = types.str;
111+
description = "URL of the service to monitor";
112+
default = if config.http.proxy.enable then
113+
(if config.http.proxy.skipTLSVerify then
114+
"http://${config.http.proxy.domain}"
115+
else
116+
"https://${config.http.proxy.domain}")
117+
else
118+
"http://${config.http.host}:${toString config.http.port}";
119+
};
120+
121+
group = mkOption {
122+
type = types.str;
123+
description = "Group to put the monitor in";
124+
default = flake.lib.strings.capitalizeString
125+
nixosConfig.networking.hostName;
126+
};
127+
128+
conditions = mkOption {
129+
type = types.listOf types.str;
130+
description =
131+
"List of Gatus conditions to evaluate if the service is healthy";
132+
default = [ "[STATUS] == 200" ];
133+
};
134+
135+
interval = mkOption {
136+
type = types.str;
137+
description = "Interval to check if the service is healthy";
138+
default = "60s";
139+
};
140+
141+
extraConfig = mkOption {
142+
type = types.attrs;
143+
description = "Extra config for the Gatus endpoint";
144+
default = { };
145+
};
146+
};
147+
};
148+
default = { };
149+
};
95150
};
96-
});
151+
}));
97152
default = { };
98153
};
99154
}

modules/profiles/media-management/bazarr/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,6 @@
401401
domain = "bazarr.e10.camp";
402402
};
403403
};
404+
monitor.enable = true;
404405
};
405406
}

modules/profiles/media-management/jellyseerr.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
domain = "requests.e10.video";
1414
};
1515
};
16+
monitor.enable = true;
1617
};
1718
}

modules/profiles/media-management/plex.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040
'';
4141
};
4242
};
43+
monitor.enable = true;
4344
};
4445
}

modules/profiles/media-management/profilarr.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
domain = "profilarr.e10.camp";
1414
};
1515
};
16+
monitor.enable = true;
1617
};
1718
}

modules/profiles/media-management/prowlarr/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
domain = "prowlarr.e10.camp";
2525
};
2626
};
27+
monitor.enable = true;
2728
};
2829
}

0 commit comments

Comments
 (0)