modules/monitoring: clean up tailscale and caddy config

This commit is contained in:
xinyangli 2024-12-05 17:36:01 +08:00
parent 756357552a
commit 9a21ab6621
Signed by: xin
SSH key fingerprint: SHA256:UU5pRTl7NiLFJbWJZa+snLylZSXIz5rgHmwjzv8v4oE
10 changed files with 109 additions and 47 deletions

View file

@ -1,6 +1,7 @@
{ {
pkgs, pkgs,
lib, lib,
config,
... ...
}: }:
@ -41,14 +42,19 @@
promtail.enable = true; promtail.enable = true;
}; };
custom.prometheus.exporters = {
enable = true;
};
services.tailscale.enable = true;
services.caddy.enable = true;
sops = { sops = {
defaultSopsFile = ./secrets.yaml; defaultSopsFile = ./secrets.yaml;
age.sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ]; age.sshKeyPaths = [ "/etc/ssh/ssh_host_ed25519_key" ];
}; };
services.caddy.enable = true;
services.tailscale.enable = true;
services.postgresql = { services.postgresql = {
enable = true; enable = true;
package = pkgs.postgresql_17; package = pkgs.postgresql_17;

View file

@ -31,6 +31,7 @@ in
services.matrix-synapse = { services.matrix-synapse = {
enable = true; enable = true;
withJemalloc = true;
settings = { settings = {
server_name = "xiny.li"; server_name = "xiny.li";
public_baseurl = synapseDelegateUrl; public_baseurl = synapseDelegateUrl;

View file

@ -1,3 +1,4 @@
{ config, ... }:
{ {
imports = [ imports = [
./hardware-configurations.nix ./hardware-configurations.nix
@ -30,6 +31,10 @@
443 443
]; ];
services.tailscale.enable = true;
services.caddy.enable = true;
commonSettings = { commonSettings = {
auth.enable = true; auth.enable = true;
}; };

View file

@ -116,6 +116,8 @@ in
]) ])
++ (mkCaddyScrapes [ ++ (mkCaddyScrapes [
{ address = "thorite.coho-tet.ts.net"; } { address = "thorite.coho-tet.ts.net"; }
{ address = "biotite.coho-tet.ts.net"; }
{ address = "weilite.coho-tet.ts.net"; }
]) ])
++ (mkNodeScrapes [ ++ (mkNodeScrapes [
{ address = "thorite.coho-tet.ts.net"; } { address = "thorite.coho-tet.ts.net"; }

View file

@ -82,19 +82,9 @@ in
]; ];
} }
(mkIf cfg.enable { (mkIf cfg.enable {
services.caddy.virtualHosts."${config.networking.hostName}.coho-tet.ts.net".extraConfig = ''
services.tailscale = { reverse_proxy 127.0.0.1:${toString config.services.prometheus.port}
enable = true; '';
permitCertUid = config.services.caddy.user;
openFirewall = true;
};
services.caddy = {
enable = true;
virtualHosts."${config.networking.hostName}.coho-tet.ts.net".extraConfig = ''
reverse_proxy 127.0.0.1:${toString config.services.prometheus.port}
'';
};
services.prometheus = mkIf cfg.enable { services.prometheus = mkIf cfg.enable {
enable = true; enable = true;
port = 9091; port = 9091;

View file

@ -5,7 +5,8 @@
... ...
}: }:
let let
inherit (lib) mkIf; inherit (lib) mkIf concatStringsSep;
inherit (config.my-lib.settings) prometheusCollectors;
cfg = config.custom.prometheus.exporters; cfg = config.custom.prometheus.exporters;
in in
{ {
@ -95,8 +96,28 @@ in
metrics metrics
} }
admin ${config.networking.hostName}.coho-tet.ts.net:2019 { admin unix//var/run/caddy/admin.sock {
origins 127.0.0.1 ${config.networking.hostName}.coho-tet.ts.net:2019
} }
''; '';
systemd.services.caddy.serviceConfig = {
RuntimeDirectory = "caddy";
RuntimeDirectoryMode = "0700";
};
services.tailscale = {
permitCertUid = config.services.caddy.user;
openFirewall = true;
};
services.caddy = {
virtualHosts."https://${config.networking.hostName}.coho-tet.ts.net:2019".extraConfig = ''
handle /metrics {
reverse_proxy unix//var/run/caddy/admin.sock
}
respond 403
'';
};
}; };
} }

View file

@ -241,17 +241,42 @@ in
]; ];
} }
# { # {
# job_name = "caddy-access"; # job_name = "caddy";
# file_sd_configs = { # static_configs = [
# files = [ # {
# "/var/log/caddy/*.log" # targets = [ "localhost" ];
# ]; # labels = {
# refresh_interval = "5m"; # job = "caddy";
# }; # __path__ = "/var/log/caddy/*log";
# agent = "caddy-promtail";
# };
# }
# ];
# pipeline_stages = [
# {
# json = {
# expressions = {
# duration = "duration";
# status = "status";
# };
# };
# }
# {
# labels = {
# duration = null;
# status = null;
# };
# }
# ];
# } # }
]; ];
}; };
}; };
services.caddy.logFormat = ''
format json
level INFO
'';
}) })
]; ];
} }

View file

@ -3,7 +3,8 @@
Demonstrate disk usage by nix-store path. Demonstrate disk usage by nix-store path.
## TODO ## TODO
- change caddy admin to unix socket - [x] change caddy admin to unix socket
- admin config persist = false - [ ] admin config persist = false
- synapse jmalloc - [x] synapse jmalloc
- backup all directories under /var/lib/forgejo - [ ] backup all directories under /var/lib/forgejo
- [ ] collect caddy access logs with promtail (waiting for caddy v2.9.0 release after which log file mode can be set)

View file

@ -28,17 +28,21 @@ in
) )
); );
mkCaddyScrapes = mkFunction ( mkCaddyScrapes = targets: [
{ {
address, job_name = "caddy";
port ? 2019, scheme = "https";
... static_configs = map (
}: {
{ address,
job_name = "caddy_${address}"; port ? 2019,
static_configs = [ { targets = [ "${address}${mkPort port}" ]; } ]; }:
{
targets = [ "${address}${mkPort port}" ];
}
) targets;
} }
); ];
mkCaddyRules = mkFunction ( mkCaddyRules = mkFunction (
{ {
@ -63,17 +67,20 @@ in
} }
); );
mkNodeScrapes = mkFunction ( mkNodeScrapes = targets: [
{ {
address, job_name = "node_exporter";
port ? 9100, static_configs = map (
... {
}: address,
{ port ? 9100,
job_name = "node_${address}"; }:
static_configs = [ { targets = [ "${address}${mkPort port}" ]; } ]; {
targets = [ "${address}${mkPort port}" ];
}
) targets;
} }
); ];
mkNodeRules = mkFunction ( mkNodeRules = mkFunction (
{ {

View file

@ -12,5 +12,9 @@
grafanaUrl = "https://grafana.xiny.li"; grafanaUrl = "https://grafana.xiny.li";
synapseUrl = "https://xiny.li"; synapseUrl = "https://xiny.li";
synapseDelegateUrl = "https://synapse.xiny.li"; synapseDelegateUrl = "https://synapse.xiny.li";
prometheusCollectors = [
"thorite.coho-tet.ts.net"
];
}; };
} }