1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.nginxExporter;
7in {
8 options = {
9 services.prometheus.nginxExporter = {
10 enable = mkEnableOption "prometheus nginx exporter";
11
12 port = mkOption {
13 type = types.int;
14 default = 9113;
15 description = ''
16 Port to listen on.
17 '';
18 };
19
20 listenAddress = mkOption {
21 type = types.string;
22 default = "0.0.0.0";
23 description = ''
24 Address to listen on.
25 '';
26 };
27
28 scrapeUri = mkOption {
29 type = types.string;
30 default = "http://localhost/nginx_status";
31 description = ''
32 Address to access the nginx status page.
33 Can be enabled with services.nginx.statusPage = true.
34 '';
35 };
36
37 extraFlags = mkOption {
38 type = types.listOf types.str;
39 default = [];
40 description = ''
41 Extra commandline options when launching the nginx exporter.
42 '';
43 };
44
45 openFirewall = mkOption {
46 type = types.bool;
47 default = false;
48 description = ''
49 Open port in firewall for incoming connections.
50 '';
51 };
52 };
53 };
54
55 config = mkIf cfg.enable {
56 networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
57
58 systemd.services.prometheus-nginx-exporter = {
59 after = [ "network.target" "nginx.service" ];
60 description = "Prometheus exporter for nginx metrics";
61 unitConfig.Documentation = "https://github.com/discordianfish/nginx_exporter";
62 wantedBy = [ "multi-user.target" ];
63 serviceConfig = {
64 User = "nobody";
65 Restart = "always";
66 PrivateTmp = true;
67 WorkingDirectory = /tmp;
68 ExecStart = ''
69 ${pkgs.prometheus-nginx-exporter}/bin/nginx_exporter \
70 -nginx.scrape_uri '${cfg.scrapeUri}' \
71 -telemetry.address ${cfg.listenAddress}:${toString cfg.port} \
72 ${concatStringsSep " \\\n " cfg.extraFlags}
73 '';
74 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
75 };
76 };
77 };
78}