abstract grafana in prometheus config

This commit is contained in:
Noah Masur 2023-07-05 14:19:54 -06:00
parent 4c4e250fa1
commit ee58f7a4ee

View File

@ -6,10 +6,13 @@
default = null; default = null;
}; };
# If hosting Grafana, host local Prometheus and listen for inbound jobs. config = let
# If not hosting Grafana, send remote Prometheus writes to primary host
config = lib.mkIf config.services.prometheus.enable { # 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 {
services.prometheus = { services.prometheus = {
exporters.node.enable = true; exporters.node.enable = true;
@ -17,10 +20,9 @@
job_name = "local"; job_name = "local";
static_configs = [{ targets = [ "127.0.0.1:9100" ]; }]; static_configs = [{ targets = [ "127.0.0.1:9100" ]; }];
}]; }];
webExternalUrl = lib.mkIf config.services.grafana.enable webExternalUrl = lib.mkIf isServer "https://${config.prometheusServer}";
"https://${config.prometheusServer}";
# Web config file: https://prometheus.io/docs/prometheus/latest/configuration/https/ # Web config file: https://prometheus.io/docs/prometheus/latest/configuration/https/
webConfigFile = lib.mkIf config.services.grafana.enable webConfigFile = lib.mkIf isServer
((pkgs.formats.yaml { }).generate "webconfig.yml" { ((pkgs.formats.yaml { }).generate "webconfig.yml" {
basic_auth_users = { basic_auth_users = {
# Generate password: htpasswd -nBC 10 "" | tr -d ':\n' # Generate password: htpasswd -nBC 10 "" | tr -d ':\n'
@ -29,7 +31,7 @@
"$2y$10$r7FWHLHTGPAY312PdhkPEuvb05aGn9Nk1IO7qtUUUjmaDl35l6sLa"; "$2y$10$r7FWHLHTGPAY312PdhkPEuvb05aGn9Nk1IO7qtUUUjmaDl35l6sLa";
}; };
}); });
remoteWrite = lib.mkIf (!config.services.grafana.enable) [{ remoteWrite = lib.mkIf (!isServer) [{
name = config.networking.hostName; name = config.networking.hostName;
url = "https://${config.prometheusServer}"; url = "https://${config.prometheusServer}";
basic_auth = { basic_auth = {
@ -41,20 +43,19 @@
}; };
# Create credentials file for remote Prometheus push # Create credentials file for remote Prometheus push
secrets.prometheus = lib.mkIf (!config.services.grafana.enable) { secrets.prometheus = lib.mkIf (!isServer) {
source = ../../../private/prometheus.age; source = ../../../private/prometheus.age;
dest = "${config.secretsDirectory}/prometheus"; dest = "${config.secretsDirectory}/prometheus";
owner = "prometheus"; owner = "prometheus";
group = "prometheus"; group = "prometheus";
permissions = "0440"; permissions = "0440";
}; };
systemd.services.prometheus-secret = systemd.services.prometheus-secret = lib.mkIf (!isServer) {
lib.mkIf (!config.services.grafana.enable) { requiredBy = [ "prometheus.service" ];
requiredBy = [ "prometheus.service" ]; before = [ "prometheus.service" ];
before = [ "prometheus.service" ]; };
};
caddy.routes = lib.mkIf config.services.grafana.enable [{ caddy.routes = lib.mkIf isServer [{
match = [{ host = [ config.prometheusServer ]; }]; match = [{ host = [ config.prometheusServer ]; }];
handle = [{ handle = [{
handler = "reverse_proxy"; handler = "reverse_proxy";