dotfiles/modules/nixos/services/prometheus.nix

64 lines
2.0 KiB
Nix
Raw Normal View History

{ config, pkgs, lib, ... }: {
2022-10-14 04:01:41 +00:00
2023-07-05 20:19:54 +00:00
config = let
2022-10-14 04:01:41 +00:00
2023-07-05 20:19:54 +00:00
# If hosting Grafana, host local Prometheus and listen for inbound jobs. If
# not hosting Grafana, send remote Prometheus writes to primary host.
isServer = config.services.grafana.enable;
in lib.mkIf config.services.prometheus.enable {
2022-10-14 04:01:41 +00:00
services.prometheus = {
exporters.node.enable = true;
scrapeConfigs = [{
job_name = "local";
static_configs = [{ targets = [ "127.0.0.1:9100" ]; }];
}];
2023-07-07 16:16:07 +00:00
webExternalUrl =
lib.mkIf isServer "https://${config.hostnames.prometheus}";
2023-07-04 23:05:56 +00:00
# Web config file: https://prometheus.io/docs/prometheus/latest/configuration/https/
2023-07-05 20:19:54 +00:00
webConfigFile = lib.mkIf isServer
2023-07-04 23:21:53 +00:00
((pkgs.formats.yaml { }).generate "webconfig.yml" {
basic_auth_users = {
# Generate password: htpasswd -nBC 10 "" | tr -d ':\n'
# Encrypt and place in private/prometheus.age
"prometheus" =
"$2y$10$r7FWHLHTGPAY312PdhkPEuvb05aGn9Nk1IO7qtUUUjmaDl35l6sLa";
};
2023-07-04 23:21:53 +00:00
});
2023-07-05 20:19:54 +00:00
remoteWrite = lib.mkIf (!isServer) [{
name = config.networking.hostName;
2023-07-07 16:16:07 +00:00
url = "https://${config.hostnames.prometheus}";
basic_auth = {
# Uses password hashed with bcrypt above
username = "prometheus";
password_file = config.secrets.prometheus.dest;
};
}];
};
# Create credentials file for remote Prometheus push
2023-07-05 20:19:54 +00:00
secrets.prometheus = lib.mkIf (!isServer) {
source = ../../../private/prometheus.age;
dest = "${config.secretsDirectory}/prometheus";
owner = "prometheus";
group = "prometheus";
permissions = "0440";
2022-10-14 04:01:41 +00:00
};
2023-07-05 20:19:54 +00:00
systemd.services.prometheus-secret = lib.mkIf (!isServer) {
requiredBy = [ "prometheus.service" ];
before = [ "prometheus.service" ];
};
2022-10-14 04:01:41 +00:00
2023-07-05 20:19:54 +00:00
caddy.routes = lib.mkIf isServer [{
2023-07-07 16:16:07 +00:00
match = [{ host = [ config.hostnames.prometheus ]; }];
2022-10-14 04:01:41 +00:00
handle = [{
handler = "reverse_proxy";
upstreams = [{ dial = "localhost:9090"; }];
2022-10-14 04:01:41 +00:00
}];
}];
};
}