From 01762fec0ab7576e3b371eda3dd3fd576e640798 Mon Sep 17 00:00:00 2001 From: Justinas Stankevicius Date: Mon, 3 Aug 2020 17:48:19 +0300 Subject: [PATCH 1/3] prometheus-sql-exporter: init at 0.3.0 --- .../monitoring/prometheus/sql-exporter.nix | 23 +++++++++++++++++++ pkgs/top-level/all-packages.nix | 1 + 2 files changed, 24 insertions(+) create mode 100644 pkgs/servers/monitoring/prometheus/sql-exporter.nix diff --git a/pkgs/servers/monitoring/prometheus/sql-exporter.nix b/pkgs/servers/monitoring/prometheus/sql-exporter.nix new file mode 100644 index 00000000000..c689cc1ed79 --- /dev/null +++ b/pkgs/servers/monitoring/prometheus/sql-exporter.nix @@ -0,0 +1,23 @@ +{ stdenv, buildGoModule, fetchFromGitHub, nixosTests }: + +buildGoModule rec { + pname = "sql_exporter"; + version = "0.3.0"; + + vendorSha256 = null; + + src = fetchFromGitHub { + owner = "justwatchcom"; + repo = "sql_exporter"; + rev = "v${version}"; + sha256 = "125brlxgwhkn3z5v0522gpm0sk6v905ghh05c4c3wf1hlm7bhnrc"; + }; + + meta = with stdenv.lib; { + description = "Flexible SQL exporter for Prometheus"; + homepage = "https://github.com/justwatchcom/sql_exporter"; + license = licenses.mit; + maintainers = with maintainers; [ justinas ]; + platforms = platforms.unix; + }; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index c540fc45b97..ee0946f5d02 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -17048,6 +17048,7 @@ in prometheus-redis-exporter = callPackage ../servers/monitoring/prometheus/redis-exporter.nix { }; prometheus-rabbitmq-exporter = callPackage ../servers/monitoring/prometheus/rabbitmq-exporter.nix { }; prometheus-snmp-exporter = callPackage ../servers/monitoring/prometheus/snmp-exporter.nix { }; + prometheus-sql-exporter = callPackage ../servers/monitoring/prometheus/sql-exporter.nix { }; prometheus-tor-exporter = callPackage ../servers/monitoring/prometheus/tor-exporter.nix { }; prometheus-statsd-exporter = callPackage ../servers/monitoring/prometheus/statsd-exporter.nix { }; prometheus-surfboard-exporter = callPackage ../servers/monitoring/prometheus/surfboard-exporter.nix { }; From d447c2413cbace4fdcbeb6a364bc08d310309c4e Mon Sep 17 00:00:00 2001 From: Justinas Stankevicius Date: Thu, 6 Aug 2020 09:56:35 +0300 Subject: [PATCH 2/3] nixos/prometheus-sql-exporter: new module --- .../monitoring/prometheus/exporters.nix | 9 ++ .../monitoring/prometheus/exporters/sql.nix | 104 ++++++++++++++++++ nixos/tests/prometheus-exporters.nix | 44 ++++++++ 3 files changed, 157 insertions(+) create mode 100644 nixos/modules/services/monitoring/prometheus/exporters/sql.nix diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix index 1233e5cdd1a..754a775605a 100644 --- a/nixos/modules/services/monitoring/prometheus/exporters.nix +++ b/nixos/modules/services/monitoring/prometheus/exporters.nix @@ -44,6 +44,7 @@ let "redis" "rspamd" "snmp" + "sql" "surfboard" "tor" "unifi" @@ -217,6 +218,14 @@ in Please specify either 'services.prometheus.exporters.mail.configuration' or 'services.prometheus.exporters.mail.configFile'. ''; + } { + assertion = cfg.sql.enable -> ( + (cfg.sql.configFile == null) != (cfg.sql.configuration == null) + ); + message = '' + Please specify either 'services.prometheus.exporters.sql.configuration' or + 'services.prometheus.exporters.sql.configFile' + ''; } ]; }] ++ [(mkIf config.services.minio.enable { services.prometheus.exporters.minio.minioAddress = mkDefault "http://localhost:9000"; diff --git a/nixos/modules/services/monitoring/prometheus/exporters/sql.nix b/nixos/modules/services/monitoring/prometheus/exporters/sql.nix new file mode 100644 index 00000000000..d9be724ebc0 --- /dev/null +++ b/nixos/modules/services/monitoring/prometheus/exporters/sql.nix @@ -0,0 +1,104 @@ +{ config, lib, pkgs, options }: +with lib; +let + cfg = config.services.prometheus.exporters.sql; + cfgOptions = { + options = with types; { + jobs = mkOption { + type = attrsOf (submodule jobOptions); + default = { }; + description = "An attrset of metrics scraping jobs to run."; + }; + }; + }; + jobOptions = { + options = with types; { + interval = mkOption { + type = str; + description = '' + How often to run this job, specified in + Go duration format. + ''; + }; + connections = mkOption { + type = listOf str; + description = "A list of connection strings of the SQL servers to scrape metrics from"; + }; + startupSql = mkOption { + type = listOf str; + default = []; + description = "A list of SQL statements to execute once after making a connection."; + }; + queries = mkOption { + type = attrsOf (submodule queryOptions); + description = "SQL queries to run."; + }; + }; + }; + queryOptions = { + options = with types; { + help = mkOption { + type = nullOr str; + default = null; + description = "A human-readable description of this metric."; + }; + labels = mkOption { + type = listOf str; + default = [ ]; + description = "A set of columns that will be used as Prometheus labels."; + }; + query = mkOption { + type = str; + description = "The SQL query to run."; + }; + values = mkOption { + type = listOf str; + description = "A set of columns that will be used as values of this metric."; + }; + }; + }; + + configFile = + if cfg.configFile != null + then cfg.configFile + else + let + nameInline = mapAttrsToList (k: v: v // { name = k; }); + renameStartupSql = j: removeAttrs (j // { startup_sql = j.startupSql; }) [ "startupSql" ]; + configuration = { + jobs = map renameStartupSql + (nameInline (mapAttrs (k: v: (v // { queries = nameInline v.queries; })) cfg.configuration.jobs)); + }; + in + builtins.toFile "config.yaml" (builtins.toJSON configuration); +in +{ + extraOpts = { + configFile = mkOption { + type = with types; nullOr path; + default = null; + description = '' + Path to configuration file. + ''; + }; + configuration = mkOption { + type = with types; nullOr (submodule cfgOptions); + default = null; + description = '' + Exporter configuration as nix attribute set. Mutually exclusive with 'configFile' option. + ''; + }; + }; + + port = 9237; + serviceOpts = { + serviceConfig = { + ExecStart = '' + ${pkgs.prometheus-sql-exporter}/bin/sql_exporter \ + -web.listen-address ${cfg.listenAddress}:${toString cfg.port} \ + -config.file ${configFile} \ + ${concatStringsSep " \\\n " cfg.extraFlags} + ''; + }; + }; +} diff --git a/nixos/tests/prometheus-exporters.nix b/nixos/tests/prometheus-exporters.nix index ad2fff2b01f..a7e946f5c46 100644 --- a/nixos/tests/prometheus-exporters.nix +++ b/nixos/tests/prometheus-exporters.nix @@ -578,6 +578,50 @@ let ''; }; + sql = { + exporterConfig = { + configuration.jobs.points = { + interval = "1m"; + connections = [ + "postgres://prometheus-sql-exporter@/data?host=/run/postgresql&sslmode=disable" + ]; + queries = { + points = { + labels = [ "name" ]; + help = "Amount of points accumulated per person"; + values = [ "amount" ]; + query = "SELECT SUM(amount) as amount, name FROM points GROUP BY name"; + }; + }; + }; + enable = true; + user = "postgres"; + }; + metricProvider = { + services.postgresql = { + enable = true; + initialScript = builtins.toFile "init.sql" '' + CREATE DATABASE data; + \c data; + CREATE TABLE points (amount INT, name TEXT); + INSERT INTO points(amount, name) VALUES (1, 'jack'); + INSERT INTO points(amount, name) VALUES (2, 'jill'); + INSERT INTO points(amount, name) VALUES (3, 'jack'); + + CREATE USER "prometheus-sql-exporter"; + GRANT ALL PRIVILEGES ON DATABASE data TO "prometheus-sql-exporter"; + GRANT SELECT ON points TO "prometheus-sql-exporter"; + ''; + }; + systemd.services.prometheus-sql-exporter.after = [ "postgresql.service" ]; + }; + exporterTest = '' + wait_for_unit("prometheus-sql-exporter.service") + wait_for_open_port(9237) + succeed("curl http://localhost:9237/metrics | grep -c 'sql_points{' | grep -q 2") + ''; + }; + surfboard = { exporterConfig = { enable = true; From 428fc4e297093eefa5a17689a3a9e9a6b8b7f154 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Fri, 6 Nov 2020 16:43:07 +0100 Subject: [PATCH 3/3] nixos/prometheus-exporters: fix `sql` test --- nixos/tests/prometheus-exporters.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/tests/prometheus-exporters.nix b/nixos/tests/prometheus-exporters.nix index a7e946f5c46..53080403dd6 100644 --- a/nixos/tests/prometheus-exporters.nix +++ b/nixos/tests/prometheus-exporters.nix @@ -595,7 +595,7 @@ let }; }; enable = true; - user = "postgres"; + user = "prometheus-sql-exporter"; }; metricProvider = { services.postgresql = {