From 52ee0710877540d43af5d0836bd07de0e7a31f9f Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Sun, 3 Mar 2024 12:54:35 +0100 Subject: [PATCH] enable node exporter on all machines enable prometheus scraper and web interface on warwick --- nix/machines/warwick.nix | 2 ++ nix/modules/default.nix | 1 + nix/modules/prometheus.nix | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 nix/modules/prometheus.nix diff --git a/nix/machines/warwick.nix b/nix/machines/warwick.nix index 2e7c91f..e677e3e 100644 --- a/nix/machines/warwick.nix +++ b/nix/machines/warwick.nix @@ -3,5 +3,7 @@ kind = "physical"; arch = "aarch64-linux"; isRaspberryPi = true; + + nixosModule.lab.services.prometheus.server.enable = true; }; } diff --git a/nix/modules/default.nix b/nix/modules/default.nix index 8663dd3..dbc70fa 100644 --- a/nix/modules/default.nix +++ b/nix/modules/default.nix @@ -6,5 +6,6 @@ ./networking ./data-sharing.nix ./globals.nix + ./prometheus.nix ]; } diff --git a/nix/modules/prometheus.nix b/nix/modules/prometheus.nix new file mode 100644 index 0000000..46bb5b3 --- /dev/null +++ b/nix/modules/prometheus.nix @@ -0,0 +1,49 @@ +{ lib, config, machines, ... }: +let + cfg = config.lab.services.prometheus; +in +{ + options = { + lab.services.prometheus = { + enable = lib.mkOption { + default = true; + type = lib.types.bool; + }; + + server.enable = lib.mkOption { + default = false; + type = lib.types.bool; + }; + }; + }; + + config = lib.mkIf cfg.enable { + networking.firewall.allowedTCPPorts = [ config.services.prometheus.exporters.node.port ] + ++ lib.lists.optionals cfg.server.enable [ config.services.prometheus.port ]; + + services.prometheus = { + enable = cfg.server.enable; + + exporters = { + node = { + enable = true; + }; + }; + + scrapeConfigs = lib.mkIf cfg.server.enable ( + lib.attrsets.mapAttrsToList + (name: machine: + let + domain = if machine.isPhysical then "hyp" else "dmz"; + in + { + job_name = name; + static_configs = [{ + targets = [ "${name}.${domain}:${toString config.services.prometheus.exporters.node.port}" ]; + }]; + }) + machines + ); + }; + }; +}