From 65fd0312778cc86f0db9c4c87edcd5b06aacee7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20G=C3=BCntner?= Date: Sun, 13 Dec 2020 18:26:13 +0100 Subject: [PATCH] nixos: add prometheus_nginxlog_exporter module + test --- .../monitoring/prometheus/exporters.nix | 1 + .../prometheus/exporters/nginxlog.nix | 51 ++++++++++++++++ nixos/tests/prometheus-exporters.nix | 61 +++++++++++++++++++ .../prometheus/nginxlog-exporter.nix | 4 +- 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 nixos/modules/services/monitoring/prometheus/exporters/nginxlog.nix diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix index 1f8c5aeded1f..683dc0390c59 100644 --- a/nixos/modules/services/monitoring/prometheus/exporters.nix +++ b/nixos/modules/services/monitoring/prometheus/exporters.nix @@ -37,6 +37,7 @@ let "modemmanager" "nextcloud" "nginx" + "nginxlog" "node" "openvpn" "postfix" diff --git a/nixos/modules/services/monitoring/prometheus/exporters/nginxlog.nix b/nixos/modules/services/monitoring/prometheus/exporters/nginxlog.nix new file mode 100644 index 000000000000..8c1f552d58a7 --- /dev/null +++ b/nixos/modules/services/monitoring/prometheus/exporters/nginxlog.nix @@ -0,0 +1,51 @@ +{ config, lib, pkgs, options }: + +with lib; + +let + cfg = config.services.prometheus.exporters.nginxlog; +in { + port = 9117; + extraOpts = { + settings = mkOption { + type = types.attrs; + default = {}; + description = '' + All settings of nginxlog expressed as an Nix attrset. + + Check the official documentation for the corresponding YAML + settings that can all be used here: https://github.com/martin-helmich/prometheus-nginxlog-exporter + + The `listen` object is already generated by `port`, `listenAddress` and `metricsEndpoint` and + will be merged with the value of `settings` before writting it as JSON. + ''; + }; + + metricsEndpoint = mkOption { + type = types.str; + default = "/metrics"; + description = '' + Path under which to expose metrics. + ''; + }; + }; + + serviceOpts = let + listenConfig = { + listen = { + port = cfg.port; + address = cfg.listenAddress; + metrics_endpoint = cfg.metricsEndpoint; + }; + }; + completeConfig = pkgs.writeText "nginxlog-exporter.yaml" (builtins.toJSON (lib.recursiveUpdate listenConfig cfg.settings)); + in { + serviceConfig = { + ExecStart = '' + ${pkgs.prometheus-nginxlog-exporter}/bin/prometheus-nginxlog-exporter -config-file ${completeConfig} + ''; + Restart="always"; + ProtectSystem="full"; + }; + }; +} diff --git a/nixos/tests/prometheus-exporters.nix b/nixos/tests/prometheus-exporters.nix index 3eb4341e39cb..ffa7f420c093 100644 --- a/nixos/tests/prometheus-exporters.nix +++ b/nixos/tests/prometheus-exporters.nix @@ -444,6 +444,67 @@ let ''; }; + nginxlog = { + exporterConfig = { + enable = true; + group = "nginx"; + settings = { + namespaces = [ + { + name = "filelogger"; + source = { + files = [ "/var/log/nginx/filelogger.access.log" ]; + }; + } + { + name = "syslogger"; + source = { + syslog = { + listen_address = "udp://127.0.0.1:10000"; + format = "rfc3164"; + tags = ["nginx"]; + }; + }; + } + ]; + }; + }; + metricProvider = { + services.nginx = { + enable = true; + httpConfig = '' + server { + listen 80; + server_name filelogger.local; + access_log /var/log/nginx/filelogger.access.log; + } + server { + listen 81; + server_name syslogger.local; + access_log syslog:server=127.0.0.1:10000,tag=nginx,severity=info; + } + ''; + }; + }; + exporterTest = '' + wait_for_unit("nginx.service") + wait_for_unit("prometheus-nginxlog-exporter.service") + wait_for_open_port(9117) + wait_for_open_port(80) + wait_for_open_port(81) + succeed("curl http://localhost") + execute("sleep 1") + succeed( + "curl -sSf http://localhost:9117/metrics | grep 'filelogger_http_response_count_total' | grep -q 1" + ) + succeed("curl http://localhost:81") + execute("sleep 1") + succeed( + "curl -sSf http://localhost:9117/metrics | grep 'syslogger_http_response_count_total' | grep -q 1" + ) + ''; + }; + node = { exporterConfig = { enable = true; diff --git a/pkgs/servers/monitoring/prometheus/nginxlog-exporter.nix b/pkgs/servers/monitoring/prometheus/nginxlog-exporter.nix index a9276e0c924a..771bbf39b081 100644 --- a/pkgs/servers/monitoring/prometheus/nginxlog-exporter.nix +++ b/pkgs/servers/monitoring/prometheus/nginxlog-exporter.nix @@ -1,4 +1,4 @@ -{ stdenv, buildGoModule, fetchFromGitHub }: +{ stdenv, buildGoModule, fetchFromGitHub, nixosTests }: buildGoModule rec { pname = "nginxlog_exporter"; @@ -17,6 +17,8 @@ buildGoModule rec { runVend = true; + passthru.tests = { inherit (nixosTests.prometheus-exporters) nginxlog; }; + meta = with stdenv.lib; { description = "Export metrics from Nginx access log files to Prometheus"; homepage = "https://github.com/martin-helmich/prometheus-nginxlog-exporter";