From 13b3baa80500120ccc93d3af446c41da1f5df3e2 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Wed, 27 Jan 2021 21:49:03 +0100 Subject: [PATCH] monitoring: refacto, extract from main config --- configuration.nix | 71 ++------------- services/default.nix | 7 ++ .../node-exporter-full_rev21.json | 0 services/monitoring.nix | 89 +++++++++++++++++++ 4 files changed, 102 insertions(+), 65 deletions(-) create mode 100644 services/default.nix rename {grafana-dashboards => services/grafana-dashboards}/node-exporter-full_rev21.json (100%) create mode 100644 services/monitoring.nix diff --git a/configuration.nix b/configuration.nix index 74d03c5..48b06e6 100644 --- a/configuration.nix +++ b/configuration.nix @@ -8,6 +8,7 @@ imports = [ # Include the results of the hardware scan. ./hardware-configuration.nix + ./services ]; # Use the GRUB 2 boot loader. @@ -16,6 +17,7 @@ boot.loader.grub.device = "/dev/sda"; # or "nodev" for efi only networking.hostName = "poseidon"; # Define your hostname. + networking.domain = "alarsyo.net"; # Set your time zone. time.timeZone = "Europe/Paris"; @@ -67,72 +69,11 @@ programs.fish.enable = true; # List services that you want to enable: - services.grafana = { - enable = true; - domain = "monitoring-test.alarsyo.net"; - port = 3000; - addr = "127.0.0.1"; - - provision = { + my.services = { + monitoring = { enable = true; - - datasources = [ - { - name = "Prometheus"; - type = "prometheus"; - url = "http://localhost:${toString config.services.prometheus.port}"; - } - ]; - - dashboards = [ - { - name = "Node Exporter"; - options.path = ./grafana-dashboards; - disableDeletion = true; - } - ]; - }; - }; - - services.prometheus = { - enable = true; - port = 9090; - listenAddress = "127.0.0.1"; - - exporters = { - node = { - enable = true; - enabledCollectors = [ "systemd" ]; - port = 9100; - }; - }; - - scrapeConfigs = [ - { - job_name = config.networking.hostName; - static_configs = [{ - targets = [ "127.0.0.1:${toString config.services.prometheus.exporters.node.port}" ]; - }]; - } - ]; - }; - - services.nginx = { - enable = true; - - recommendedGzipSettings = true; - recommendedOptimisation = true; - recommendedProxySettings = true; - recommendedTlsSettings = true; - - virtualHosts.${config.services.grafana.domain} = { - locations."/" = { - proxyPass = "http://127.0.0.1:${toString config.services.grafana.port}"; - proxyWebsockets = true; - }; - - forceSSL = true; - enableACME = true; + useACME = true; + domain = "monitoring-test.${config.networking.domain}"; }; }; diff --git a/services/default.nix b/services/default.nix new file mode 100644 index 0000000..3b3fbef --- /dev/null +++ b/services/default.nix @@ -0,0 +1,7 @@ +{ ... }: + +{ + imports = [ + ./monitoring.nix + ]; +} diff --git a/grafana-dashboards/node-exporter-full_rev21.json b/services/grafana-dashboards/node-exporter-full_rev21.json similarity index 100% rename from grafana-dashboards/node-exporter-full_rev21.json rename to services/grafana-dashboards/node-exporter-full_rev21.json diff --git a/services/monitoring.nix b/services/monitoring.nix new file mode 100644 index 0000000..db60d6e --- /dev/null +++ b/services/monitoring.nix @@ -0,0 +1,89 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let cfg = config.my.services.monitoring; +in { + options.my.services.monitoring = { + enable = mkEnableOption "Enable monitoring"; + useACME = mkEnableOption "Get HTTPS certs"; + + domain = mkOption { + type = types.str; + default = "monitoring.${config.networking.domain}"; + example = "monitoring.example.com"; + description = "Domain to use in reverse proxy"; + }; + }; + + config = mkIf cfg.enable { + services.grafana = { + enable = true; + domain = cfg.domain; + port = 3000; + addr = "127.0.0.1"; + + provision = { + enable = true; + + datasources = [ + { + name = "Prometheus"; + type = "prometheus"; + url = "http://localhost:${toString config.services.prometheus.port}"; + } + ]; + + dashboards = [ + { + name = "Node Exporter"; + options.path = ./grafana-dashboards; + disableDeletion = true; + } + ]; + }; + }; + + services.prometheus = { + enable = true; + port = 9090; + listenAddress = "127.0.0.1"; + + exporters = { + node = { + enable = true; + enabledCollectors = [ "systemd" ]; + port = 9100; + }; + }; + + scrapeConfigs = [ + { + job_name = config.networking.hostName; + static_configs = [{ + targets = [ "127.0.0.1:${toString config.services.prometheus.exporters.node.port}" ]; + }]; + } + ]; + }; + + services.nginx = { + enable = true; + + recommendedGzipSettings = true; + recommendedOptimisation = true; + recommendedProxySettings = true; + recommendedTlsSettings = true; + + virtualHosts.${config.services.grafana.domain} = { + locations."/" = { + proxyPass = "http://127.0.0.1:${toString config.services.grafana.port}"; + proxyWebsockets = true; + }; + + forceSSL = cfg.useACME; + enableACME = cfg.useACME; + }; + }; + }; +}