From d83bd16a2eab64e3e1b67cd5689807ec844450ba Mon Sep 17 00:00:00 2001 From: Flakebi Date: Tue, 18 May 2021 22:49:32 +0200 Subject: [PATCH 1/2] prometheus-script-exporter: init at 1.2.0 --- .../monitoring/prometheus/script-exporter.nix | 25 +++++++++++++++++++ pkgs/top-level/all-packages.nix | 1 + 2 files changed, 26 insertions(+) create mode 100644 pkgs/servers/monitoring/prometheus/script-exporter.nix diff --git a/pkgs/servers/monitoring/prometheus/script-exporter.nix b/pkgs/servers/monitoring/prometheus/script-exporter.nix new file mode 100644 index 000000000000..d0806b6c5fa4 --- /dev/null +++ b/pkgs/servers/monitoring/prometheus/script-exporter.nix @@ -0,0 +1,25 @@ +{ lib, buildGoModule, fetchFromGitHub, nixosTests }: + +buildGoModule rec { + pname = "script_exporter"; + version = "1.2.0"; + + src = fetchFromGitHub { + owner = "adhocteam"; + repo = pname; + rev = "v${version}"; + sha256 = "t/xgRalcHxEcT1peU1ePJUItD02rQdfz1uWpXDBo6C0="; + }; + + vendorSha256 = "Hs1SNpC+t1OCcoF3FBgpVGkhR97ulq6zYhi8BQlgfVc="; + + passthru.tests = { inherit (nixosTests.prometheus-exporters) script; }; + + meta = with lib; { + description = "Shell script prometheus exporter"; + homepage = "https://github.com/adhocteam/script_exporter"; + license = licenses.mit; + maintainers = with maintainers; [ Flakebi ]; + platforms = platforms.linux; + }; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 88e6731df859..5cdce3509045 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -19701,6 +19701,7 @@ in prometheus-redis-exporter = callPackage ../servers/monitoring/prometheus/redis-exporter.nix { }; prometheus-rabbitmq-exporter = callPackage ../servers/monitoring/prometheus/rabbitmq-exporter.nix { }; prometheus-rtl_433-exporter = callPackage ../servers/monitoring/prometheus/rtl_433-exporter.nix { }; + prometheus-script-exporter = callPackage ../servers/monitoring/prometheus/script-exporter.nix { }; prometheus-smokeping-prober = callPackage ../servers/monitoring/prometheus/smokeping-prober.nix { }; prometheus-snmp-exporter = callPackage ../servers/monitoring/prometheus/snmp-exporter.nix { }; prometheus-statsd-exporter = callPackage ../servers/monitoring/prometheus/statsd-exporter.nix { }; From 3bcf4e31ef70e2d6ec3fd387c174673aa01ef736 Mon Sep 17 00:00:00 2001 From: Flakebi Date: Tue, 18 May 2021 22:53:10 +0200 Subject: [PATCH 2/2] nixos/prometheus: add script exporter --- .../monitoring/prometheus/exporters.nix | 1 + .../prometheus/exporters/script.nix | 64 +++++++++++++++++++ nixos/tests/prometheus-exporters.nix | 18 ++++++ 3 files changed, 83 insertions(+) create mode 100644 nixos/modules/services/monitoring/prometheus/exporters/script.nix diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix index 9fcfe7b52e08..b57b73522f8d 100644 --- a/nixos/modules/services/monitoring/prometheus/exporters.nix +++ b/nixos/modules/services/monitoring/prometheus/exporters.nix @@ -55,6 +55,7 @@ let "redis" "rspamd" "rtl_433" + "script" "snmp" "smokeping" "sql" diff --git a/nixos/modules/services/monitoring/prometheus/exporters/script.nix b/nixos/modules/services/monitoring/prometheus/exporters/script.nix new file mode 100644 index 000000000000..104ab859f2ee --- /dev/null +++ b/nixos/modules/services/monitoring/prometheus/exporters/script.nix @@ -0,0 +1,64 @@ +{ config, lib, pkgs, options }: + +with lib; + +let + cfg = config.services.prometheus.exporters.script; + configFile = pkgs.writeText "script-exporter.yaml" (builtins.toJSON cfg.settings); +in +{ + port = 9172; + extraOpts = { + settings.scripts = mkOption { + type = with types; listOf (submodule { + options = { + name = mkOption { + type = str; + example = "sleep"; + description = "Name of the script."; + }; + script = mkOption { + type = str; + example = "sleep 5"; + description = "Shell script to execute when metrics are requested."; + }; + timeout = mkOption { + type = nullOr int; + default = null; + example = 60; + description = "Optional timeout for the script in seconds."; + }; + }; + }); + example = literalExample '' + { + scripts = [ + { name = "sleep"; script = "sleep 5"; } + ]; + } + ''; + 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 = { + ExecStart = '' + ${pkgs.prometheus-script-exporter}/bin/script_exporter \ + --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \ + --config.file ${configFile} \ + ${concatStringsSep " \\\n " cfg.extraFlags} + ''; + NoNewPrivileges = true; + ProtectHome = true; + ProtectSystem = "strict"; + ProtectKernelTunables = true; + ProtectKernelModules = true; + ProtectControlGroups = true; + }; + }; +} diff --git a/nixos/tests/prometheus-exporters.nix b/nixos/tests/prometheus-exporters.nix index e3bfff218adb..defec2b8376d 100644 --- a/nixos/tests/prometheus-exporters.nix +++ b/nixos/tests/prometheus-exporters.nix @@ -945,6 +945,24 @@ let ''; }; + script = { + exporterConfig = { + enable = true; + settings.scripts = [ + { name = "success"; script = "sleep 1"; } + ]; + }; + exporterTest = '' + wait_for_unit("prometheus-script-exporter.service") + wait_for_open_port(9172) + wait_until_succeeds( + "curl -sSf 'localhost:9172/probe?name=success' | grep -q '{}'".format( + 'script_success{script="success"} 1' + ) + ) + ''; + }; + smokeping = { exporterConfig = { enable = true;