From c9d6f0f5e8ca5ea25dd06ba98ac454ec422e698f Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 19 Feb 2024 10:40:10 -0500 Subject: [PATCH] Add node-exporter. --- nix/base-system/darwin-configuration.nix | 4 + nix/base-system/flake.nix | 1 + nix/base-system/modules/darwin-monitor.nix | 98 ++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 nix/base-system/modules/darwin-monitor.nix diff --git a/nix/base-system/darwin-configuration.nix b/nix/base-system/darwin-configuration.nix index cdd34ed..4948be8 100644 --- a/nix/base-system/darwin-configuration.nix +++ b/nix/base-system/darwin-configuration.nix @@ -27,6 +27,7 @@ }) ]; + # TODO(zaphar): Move this to a module. launchd.user.agents.ipfs = { serviceConfig = { ProgramArguments = [ @@ -39,6 +40,9 @@ }; }; + services.node-exporter.enable = true; + services.prometheus.enable = true; + # TODO launchd.user.agents.prometheus; # Use a custom configuration.nix location. # $ darwin-rebuild switch -I darwin-config=$HOME/.config/nixpkgs/darwin/configuration.nix diff --git a/nix/base-system/flake.nix b/nix/base-system/flake.nix index aed71aa..cc3e928 100644 --- a/nix/base-system/flake.nix +++ b/nix/base-system/flake.nix @@ -325,6 +325,7 @@ EOF"; modules = [ (systemModule system) (vimModule system) + ./modules/darwin-monitor.nix ./darwin-configuration.nix ]; }; diff --git a/nix/base-system/modules/darwin-monitor.nix b/nix/base-system/modules/darwin-monitor.nix new file mode 100644 index 0000000..39c479d --- /dev/null +++ b/nix/base-system/modules/darwin-monitor.nix @@ -0,0 +1,98 @@ +{ pkgs, lib, config, ...}: +with lib; +{ + + options.services.node-exporter = { + enable = mkEnableOption "Enable the prometheus node_exporter"; + listen = mkOption { + description = "[host]:port to listen on for the node_exporter"; + default = "localhost:9100"; + defaultText = "localhost:9100"; + }; + }; + + options.services.prometheus = { + enable = lib.mkEnableOption "Enable the prometheus server"; + listen = mkOption { + description = "[host]:port to listen on for the prometheus server"; + default = "localhost:9000"; + defaultText = "localhost:9000"; + }; + settings = mkOption { + description = "The yaml configuration file settings for prometheus"; + default = { + scrape_configs = [ + { + job_name = "node_exporter"; + scrape_interval = "30s"; + metrics_path = "/metrics"; + static_configs = [ + { + targets = [ + "${config.services.node-exporter.listen}" + ]; + } + ]; + } + ]; + }; + defaultText = ''{ + scrape_configs = [ + { + job_name = "node_exporter"; + scrape_interval = "30s"; + metrics_path = "/metrics"; + static_configs = [ + { + targets = [ + "${config.services.node-exporter.listen}" + ]; + } + ]; + } + ]; + }''; + }; + dataPath = mkOption { + description = "Storage path for the tsdb"; + default = "/Users/zaphar/opt/prometheus"; + defaultText = "~/opt/prometheus"; + }; + }; + + + config = { + launchd.user.agents.node-exporter = mkIf config.services.node-exporter.enable { + serviceConfig = { + ProgramArguments = [ + "${pkgs.prometheus-node-exporter}/bin/node_exporter" + "--web.listen-address=${config.services.node-exporter.listen}" + ]; + KeepAlive = true; + RunAtLoad = true; + }; + }; + + environment.etc."prometheus.yaml" = mkIf config.services.prometheus.enable { + text = (generators.toYAML {} config.services.prometheus.settings); + }; + + launchd.user.agents.prometheus = mkIf config.services.prometheus.enable { + serviceConfig = { + ProgramArguments = [ + "${pkgs.prometheus}/bin/prometheus" + "--web.listen-address=${config.services.prometheus.listen}" + "--config.file=/etc/${config.environment.etc."prometheus.yaml".target}" + #"--storage.tsdb.path=${config.services.prometheus.dataPath}" + ]; + StandardOutPath = "${config.services.prometheus.dataPath}/prometheus-out.log"; + StandardErrorPath = "${config.services.prometheus.dataPath}/prometheus-err.log"; + WorkingDirectory=config.services.prometheus.dataPath; + KeepAlive = true; + RunAtLoad = true; + }; + }; + }; + +} +