1 <chapter xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xmlns:xi="http://www.w3.org/2001/XInclude"
5 xml:id="module-services-prometheus-exporters">
6 <title>Prometheus exporters</title>
8 Prometheus exporters provide metrics for the
9 <link xlink:href="https://prometheus.io">prometheus monitoring system</link>.
11 <section xml:id="module-services-prometheus-exporters-configuration">
12 <title>Configuration</title>
15 One of the most common exporters is the
16 <link xlink:href="https://github.com/prometheus/node_exporter">node
17 exporter</link>, it provides hardware and OS metrics from the host it's
18 running on. The exporter could be configured as follows:
20 services.prometheus.exporters.node = {
27 disabledCollectors = [
31 firewallFilter = "-i br0 -p tcp -m tcp --dport 9100";
34 It should now serve all metrics from the collectors that are explicitly
35 enabled and the ones that are
36 <link xlink:href="https://github.com/prometheus/node_exporter#enabled-by-default">enabled
37 by default</link>, via http under <literal>/metrics</literal>. In this
38 example the firewall should just allow incoming connections to the
39 exporter's port on the bridge interface <literal>br0</literal> (this would
40 have to be configured seperately of course). For more information about
41 configuration see <literal>man configuration.nix</literal> or search through
43 <link xlink:href="https://nixos.org/nixos/options.html#prometheus.exporters">available
48 Prometheus can now be configured to consume the metrics produced by the exporter:
50 services.prometheus = {
57 targets = [ "localhost:${toString config.services.prometheus.exporters.node.port}" ];
67 <section xml:id="module-services-prometheus-exporters-new-exporter">
68 <title>Adding a new exporter</title>
71 To add a new exporter, it has to be packaged first (see
72 <literal>nixpkgs/pkgs/servers/monitoring/prometheus/</literal> for
73 examples), then a module can be added. The postfix exporter is used in this
80 Some default options for all exporters are provided by
81 <literal>nixpkgs/nixos/modules/services/monitoring/prometheus/exporters.nix</literal>:
84 <listitem override='none'>
88 <literal>enable</literal>
93 <literal>port</literal>
98 <literal>listenAddress</literal>
103 <literal>extraFlags</literal>
108 <literal>openFirewall</literal>
113 <literal>firewallFilter</literal>
118 <literal>user</literal>
123 <literal>group</literal>
130 As there is already a package available, the module can now be added. This
131 is accomplished by adding a new file to the
132 <literal>nixos/modules/services/monitoring/prometheus/exporters/</literal>
133 directory, which will be called postfix.nix and contains all exporter
134 specific options and configuration:
136 # nixpgs/nixos/modules/services/prometheus/exporters/postfix.nix
137 { config, lib, pkgs, options }:
142 # for convenience we define cfg here
143 cfg = config.services.prometheus.exporters.postfix;
146 port = 9154; # The postfix exporter listens on this port by default
148 # `extraOpts` is an attribute set which contains additional options
149 # (and optional overrides for default options).
150 # Note that this attribute is optional.
152 telemetryPath = mkOption {
154 default = "/metrics";
156 Path under which to expose metrics.
159 logfilePath = mkOption {
161 default = /var/log/postfix_exporter_input.log;
162 example = /var/log/mail.log;
164 Path where Postfix writes log entries.
165 This file will be truncated by this exporter!
168 showqPath = mkOption {
170 default = /var/spool/postfix/public/showq;
171 example = /var/lib/postfix/queue/public/showq;
173 Path at which Postfix places its showq socket.
178 # `serviceOpts` is an attribute set which contains configuration
179 # for the exporter's systemd service. One of
180 # `serviceOpts.script` and `serviceOpts.serviceConfig.ExecStart`
181 # has to be specified here. This will be merged with the default
182 # service confiuration.
183 # Note that by default 'DynamicUser' is 'true'.
188 ${pkgs.prometheus-postfix-exporter}/bin/postfix_exporter \
189 --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
190 --web.telemetry-path ${cfg.telemetryPath} \
191 ${concatStringsSep " \\\n " cfg.extraFlags}
201 This should already be enough for the postfix exporter. Additionally one
202 could now add assertions and conditional default values. This can be done
203 in the 'meta-module' that combines all exporter definitions and generates
205 <literal>nixpkgs/nixos/modules/services/prometheus/exporters.nix</literal>
210 <section xml:id="module-services-prometheus-exporters-update-exporter-module">
211 <title>Updating an exporter module</title>
213 Should an exporter option change at some point, it is possible to add
214 information about the change to the exporter definition similar to
215 <literal>nixpkgs/nixos/modules/rename.nix</literal>:
217 { config, lib, pkgs, options }:
222 cfg = config.services.prometheus.exporters.nginx;
227 # additional module options
231 # service configuration
235 # 'services.prometheus.exporters.nginx.telemetryEndpoint' -> 'services.prometheus.exporters.nginx.telemetryPath'
236 (mkRenamedOptionModule [ "telemetryEndpoint" ] [ "telemetryPath" ])
238 # removed option 'services.prometheus.exporters.nginx.insecure'
239 (mkRemovedOptionModule [ "insecure" ] ''
240 This option was replaced by 'prometheus.exporters.nginx.sslVerify' which defaults to true.
242 ({ options.warnings = options.warnings; })