From 5e5a3c39edfbb3022c2c8cdffd6f291471437481 Mon Sep 17 00:00:00 2001 From: Flakebi Date: Tue, 18 May 2021 22:23:49 +0200 Subject: [PATCH] nixos/prometheus: add process exporter --- .../monitoring/prometheus/exporters.nix | 1 + .../prometheus/exporters/process.nix | 48 +++++++++++++++++++ nixos/tests/prometheus-exporters.nix | 19 ++++++++ .../prometheus/process-exporter.nix | 4 +- 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 nixos/modules/services/monitoring/prometheus/exporters/process.nix diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix index 9fcfe7b52e0..212ba06ef75 100644 --- a/nixos/modules/services/monitoring/prometheus/exporters.nix +++ b/nixos/modules/services/monitoring/prometheus/exporters.nix @@ -51,6 +51,7 @@ let "pihole" "postfix" "postgres" + "process" "py-air-control" "redis" "rspamd" diff --git a/nixos/modules/services/monitoring/prometheus/exporters/process.nix b/nixos/modules/services/monitoring/prometheus/exporters/process.nix new file mode 100644 index 00000000000..e3b3d18367f --- /dev/null +++ b/nixos/modules/services/monitoring/prometheus/exporters/process.nix @@ -0,0 +1,48 @@ +{ config, lib, pkgs, options }: + +with lib; + +let + cfg = config.services.prometheus.exporters.process; + configFile = pkgs.writeText "process-exporter.yaml" (builtins.toJSON cfg.settings); +in +{ + port = 9256; + extraOpts = { + settings.process_names = mkOption { + type = types.listOf types.anything; + default = {}; + example = literalExample '' + { + process_names = [ + # Remove nix store path from process name + { name = "{{.Matches.Wrapped}} {{ .Matches.Args }}"; cmdline = [ "^/nix/store[^ ]*/(?P[^ /]*) (?P.*)" ]; } + ]; + } + ''; + description = '' + All settings expressed as an Nix attrset. + + Check the official documentation for the corresponding YAML + settings that can all be used here: + ''; + }; + }; + serviceOpts = { + serviceConfig = { + DynamicUser = false; + ExecStart = '' + ${pkgs.prometheus-process-exporter}/bin/process-exporter \ + --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \ + --config.path ${configFile} \ + ${concatStringsSep " \\\n " cfg.extraFlags} + ''; + NoNewPrivileges = true; + ProtectHome = true; + ProtectSystem = true; + ProtectKernelTunables = true; + ProtectKernelModules = true; + ProtectControlGroups = true; + }; + }; +} diff --git a/nixos/tests/prometheus-exporters.nix b/nixos/tests/prometheus-exporters.nix index e3bfff218ad..d13058dff4c 100644 --- a/nixos/tests/prometheus-exporters.nix +++ b/nixos/tests/prometheus-exporters.nix @@ -864,6 +864,25 @@ let ''; }; + process = { + exporterConfig = { + enable = true; + settings.process_names = [ + # Remove nix store path from process name + { name = "{{.Matches.Wrapped}} {{ .Matches.Args }}"; cmdline = [ "^/nix/store[^ ]*/(?P[^ /]*) (?P.*)" ]; } + ]; + }; + exporterTest = '' + wait_for_unit("prometheus-process-exporter.service") + wait_for_open_port(9256) + wait_until_succeeds( + "curl -sSf localhost:9256/metrics | grep -q '{}'".format( + 'namedprocess_namegroup_cpu_seconds_total{groupname="process-exporter ' + ) + ) + ''; + }; + py-air-control = { nodeName = "py_air_control"; exporterConfig = { diff --git a/pkgs/servers/monitoring/prometheus/process-exporter.nix b/pkgs/servers/monitoring/prometheus/process-exporter.nix index 048a3ff264c..e4bfed9821b 100644 --- a/pkgs/servers/monitoring/prometheus/process-exporter.nix +++ b/pkgs/servers/monitoring/prometheus/process-exporter.nix @@ -1,4 +1,4 @@ -{ lib, buildGoModule, fetchFromGitHub }: +{ lib, buildGoModule, fetchFromGitHub, nixosTests }: buildGoModule rec { pname = "process-exporter"; @@ -19,6 +19,8 @@ buildGoModule rec { doCheck = true; + passthru.tests = { inherit (nixosTests.prometheus-exporters) process; }; + meta = with lib; { description = "Prometheus exporter that mines /proc to report on selected processes"; homepage = "https://github.com/ncabatoff/process-exporter";