summary refs log tree commit diff
diff options
context:
space:
mode:
authorWilliButz <WilliButz@users.noreply.github.com>2021-03-11 18:58:59 +0100
committerGitHub <noreply@github.com>2021-03-11 18:58:59 +0100
commit3ceef8186cc4e70149c23e21ffb85ecccbce10aa (patch)
tree6ad1124a1a98a2a33e444e21d5af19ed8ef2179c
parenta72148f3bc821d9c1456dd4d8f2840c9d27f8431 (diff)
parentc62eef5675e6f8ebeeb04fbb13c529899846cf16 (diff)
downloadnixpkgs-3ceef8186cc4e70149c23e21ffb85ecccbce10aa.tar
nixpkgs-3ceef8186cc4e70149c23e21ffb85ecccbce10aa.tar.gz
nixpkgs-3ceef8186cc4e70149c23e21ffb85ecccbce10aa.tar.bz2
nixpkgs-3ceef8186cc4e70149c23e21ffb85ecccbce10aa.tar.lz
nixpkgs-3ceef8186cc4e70149c23e21ffb85ecccbce10aa.tar.xz
nixpkgs-3ceef8186cc4e70149c23e21ffb85ecccbce10aa.tar.zst
nixpkgs-3ceef8186cc4e70149c23e21ffb85ecccbce10aa.zip
Merge pull request #115185 from Ma27/knot-exporter
prometheus-knot-exporter: init at 2021-01-30; minor module improvements
-rw-r--r--nixos/modules/services/monitoring/prometheus/exporters.nix24
-rw-r--r--nixos/modules/services/monitoring/prometheus/exporters/knot.nix50
-rw-r--r--nixos/tests/prometheus-exporters.nix18
-rw-r--r--pkgs/servers/monitoring/prometheus/knot-exporter.nix39
-rw-r--r--pkgs/top-level/all-packages.nix1
5 files changed, 124 insertions, 8 deletions
diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix
index 34cb0740cef..4c247848685 100644
--- a/nixos/modules/services/monitoring/prometheus/exporters.nix
+++ b/nixos/modules/services/monitoring/prometheus/exporters.nix
@@ -3,7 +3,7 @@
 let
   inherit (lib) concatStrings foldl foldl' genAttrs literalExample maintainers
                 mapAttrsToList mkDefault mkEnableOption mkIf mkMerge mkOption
-                optional types;
+                optional types mkOptionDefault flip attrNames;
 
   cfg = config.services.prometheus.exporters;
 
@@ -32,6 +32,7 @@ let
     "fritzbox"
     "json"
     "keylight"
+    "knot"
     "lnd"
     "mail"
     "mikrotik"
@@ -65,7 +66,7 @@ let
   mkExporterOpts = ({ name, port }: {
     enable = mkEnableOption "the prometheus ${name} exporter";
     port = mkOption {
-      type = types.int;
+      type = types.port;
       default = port;
       description = ''
         Port to listen on.
@@ -93,9 +94,8 @@ let
       '';
     };
     firewallFilter = mkOption {
-      type = types.str;
-      default = "-p tcp -m tcp --dport ${toString cfg.${name}.port}";
-      defaultText = "-p tcp -m tcp --dport ${toString port}";
+      type = types.nullOr types.str;
+      default = null;
       example = literalExample ''
         "-i eth0 -p tcp -m tcp --dport ${toString port}"
       '';
@@ -123,12 +123,14 @@ let
 
   mkSubModule = { name, port, extraOpts, imports }: {
     ${name} = mkOption {
-      type = types.submodule {
+      type = types.submodule [{
         inherit imports;
         options = (mkExporterOpts {
           inherit name port;
         } // extraOpts);
-      };
+      } ({ config, ... }: mkIf config.openFirewall {
+        firewallFilter = mkOptionDefault "-p tcp -m tcp --dport ${toString config.port}";
+      })];
       internal = true;
       default = {};
     };
@@ -233,7 +235,13 @@ in
         Please specify either 'services.prometheus.exporters.sql.configuration' or
           'services.prometheus.exporters.sql.configFile'
       '';
-    } ];
+    } ] ++ (flip map (attrNames cfg) (exporter: {
+      assertion = cfg.${exporter}.firewallFilter != null -> cfg.${exporter}.openFirewall;
+      message = ''
+        The `firewallFilter'-option of exporter ${exporter} doesn't have any effect unless
+        `openFirewall' is set to `true'!
+      '';
+    }));
   }] ++ [(mkIf config.services.minio.enable {
     services.prometheus.exporters.minio.minioAddress  = mkDefault "http://localhost:9000";
     services.prometheus.exporters.minio.minioAccessKey = mkDefault config.services.minio.accessKey;
diff --git a/nixos/modules/services/monitoring/prometheus/exporters/knot.nix b/nixos/modules/services/monitoring/prometheus/exporters/knot.nix
new file mode 100644
index 00000000000..46c28fe0a57
--- /dev/null
+++ b/nixos/modules/services/monitoring/prometheus/exporters/knot.nix
@@ -0,0 +1,50 @@
+{ config, lib, pkgs, options }:
+
+with lib;
+
+let
+  cfg = config.services.prometheus.exporters.knot;
+in {
+  port = 9433;
+  extraOpts = {
+    knotLibraryPath = mkOption {
+      type = types.str;
+      default = "${pkgs.knot-dns.out}/lib/libknot.so";
+      defaultText = "\${pkgs.knot-dns}/lib/libknot.so";
+      description = ''
+        Path to the library of <package>knot-dns</package>.
+      '';
+    };
+
+    knotSocketPath = mkOption {
+      type = types.str;
+      default = "/run/knot/knot.sock";
+      description = ''
+        Socket path of <citerefentry><refentrytitle>knotd</refentrytitle>
+        <manvolnum>8</manvolnum></citerefentry>.
+      '';
+    };
+
+    knotSocketTimeout = mkOption {
+      type = types.int;
+      default = 2000;
+      description = ''
+        Timeout in seconds.
+      '';
+    };
+  };
+  serviceOpts = {
+    serviceConfig = {
+      ExecStart = ''
+        ${pkgs.prometheus-knot-exporter}/bin/knot_exporter \
+          --web-listen-addr ${cfg.listenAddress} \
+          --web-listen-port ${toString cfg.port} \
+          --knot-library-path ${cfg.knotLibraryPath} \
+          --knot-socket-path ${cfg.knotSocketPath} \
+          --knot-socket-timeout ${toString cfg.knotSocketTimeout} \
+          ${concatStringsSep " \\\n  " cfg.extraFlags}
+      '';
+      SupplementaryGroups = [ "knot" ];
+    };
+  };
+}
diff --git a/nixos/tests/prometheus-exporters.nix b/nixos/tests/prometheus-exporters.nix
index 161762de723..a97a44e02f1 100644
--- a/nixos/tests/prometheus-exporters.nix
+++ b/nixos/tests/prometheus-exporters.nix
@@ -263,6 +263,24 @@ let
       '';
     };
 
+    knot = {
+      exporterConfig = {
+        enable = true;
+      };
+      metricProvider = {
+        services.knot = {
+          enable = true;
+          extraArgs = [ "-v" ];
+        };
+      };
+      exporterTest = ''
+        wait_for_unit("knot.service")
+        wait_for_unit("prometheus-knot-exporter.service")
+        wait_for_open_port(9433)
+        succeed("curl -sSf 'localhost:9433' | grep -q 'knot_server_zone_count 0.0'")
+      '';
+    };
+
     keylight = {
       # A hardware device is required to properly test this exporter, so just
       # perform a couple of basic sanity checks that the exporter is running
diff --git a/pkgs/servers/monitoring/prometheus/knot-exporter.nix b/pkgs/servers/monitoring/prometheus/knot-exporter.nix
new file mode 100644
index 00000000000..9d99685b569
--- /dev/null
+++ b/pkgs/servers/monitoring/prometheus/knot-exporter.nix
@@ -0,0 +1,39 @@
+{ stdenv, fetchFromGitHub, lib, python3, nixosTests }:
+
+stdenv.mkDerivation rec {
+  pname = "knot-exporter";
+  version = "unstable-2020-01-30";
+
+  src = fetchFromGitHub {
+    owner = "ghedo";
+    repo = "knot_exporter";
+    rev = "21dd46b401e0c1aea0b173e19462cdf89e1f444e";
+    sha256 = "sha256-4au4lpaq3jcqC2JXdCcf8h+YN8Nmm4eE0kZwA+1rWlc=";
+  };
+
+  dontBuild = true;
+
+  nativeBuildInputs = [ python3.pkgs.wrapPython ];
+  buildInputs = [ python3 ];
+
+  installPhase = ''
+    runHook preInstall
+
+    install -Dm0755 knot_exporter $out/bin/knot_exporter
+    patchShebangs $out/bin
+    buildPythonPath ${python3.pkgs.prometheus_client}
+    patchPythonScript $out/bin/knot_exporter
+
+    runHook postInstall
+  '';
+
+  passthru.tests = { inherit (nixosTests.prometheus-exporters) knot; };
+
+  meta = with lib; {
+    homepage = "https://github.com/ghedo/knot_exporter";
+    description = " Prometheus exporter for Knot DNS";
+    license = licenses.gpl3Only;
+    maintainers = with maintainers; [ ma27 ];
+    platforms = platforms.linux;
+  };
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index bf4dd9a1a43..2037857acf9 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -18596,6 +18596,7 @@ in
   prometheus-haproxy-exporter = callPackage ../servers/monitoring/prometheus/haproxy-exporter.nix { };
   prometheus-json-exporter = callPackage ../servers/monitoring/prometheus/json-exporter.nix { };
   prometheus-keylight-exporter = callPackage ../servers/monitoring/prometheus/keylight-exporter.nix { };
+  prometheus-knot-exporter = callPackage ../servers/monitoring/prometheus/knot-exporter.nix { };
   prometheus-lnd-exporter = callPackage ../servers/monitoring/prometheus/lnd-exporter.nix { };
   prometheus-mail-exporter = callPackage ../servers/monitoring/prometheus/mail-exporter.nix { };
   prometheus-mesos-exporter = callPackage ../servers/monitoring/prometheus/mesos-exporter.nix { };