1{
2 config,
3 lib,
4 pkgs,
5 options,
6 ...
7}:
8
9let
10 cfg = config.services.prometheus.exporters.nginx;
11 inherit (lib)
12 mkOption
13 types
14 mkMerge
15 mkRemovedOptionModule
16 mkRenamedOptionModule
17 mkIf
18 concatStringsSep
19 ;
20in
21{
22 port = 9113;
23 extraOpts = {
24 scrapeUri = mkOption {
25 type = types.str;
26 default = "http://localhost/nginx_status";
27 description = ''
28 Address to access the nginx status page.
29 Can be enabled with services.nginx.statusPage = true.
30 '';
31 };
32 telemetryPath = mkOption {
33 type = types.str;
34 default = "/metrics";
35 description = ''
36 Path under which to expose metrics.
37 '';
38 };
39 sslVerify = mkOption {
40 type = types.bool;
41 default = true;
42 description = ''
43 Whether to perform certificate verification for https.
44 '';
45 };
46 constLabels = mkOption {
47 type = types.listOf types.str;
48 default = [ ];
49 example = [
50 "label1=value1"
51 "label2=value2"
52 ];
53 description = ''
54 A list of constant labels that will be used in every metric.
55 '';
56 };
57 };
58 serviceOpts = mkMerge (
59 [
60 {
61 environment.CONST_LABELS = concatStringsSep "," cfg.constLabels;
62 serviceConfig = {
63 ExecStart = ''
64 ${pkgs.prometheus-nginx-exporter}/bin/nginx-prometheus-exporter \
65 --nginx.scrape-uri='${cfg.scrapeUri}' \
66 --${lib.optionalString (!cfg.sslVerify) "no-"}nginx.ssl-verify \
67 --web.listen-address=${cfg.listenAddress}:${toString cfg.port} \
68 --web.telemetry-path=${cfg.telemetryPath} \
69 ${concatStringsSep " \\\n " cfg.extraFlags}
70 '';
71 };
72 }
73 ]
74 ++ [
75 (mkIf config.services.nginx.enable {
76 after = [ "nginx.service" ];
77 requires = [ "nginx.service" ];
78 })
79 ]
80 );
81 imports = [
82 (mkRenamedOptionModule [ "telemetryEndpoint" ] [ "telemetryPath" ])
83 (mkRemovedOptionModule [ "insecure" ] ''
84 This option was replaced by 'prometheus.exporters.nginx.sslVerify'.
85 '')
86 ({
87 options.warnings = options.warnings;
88 options.assertions = options.assertions;
89 })
90 ];
91}