1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.nodeExporter;
7in {
8 options = {
9 services.prometheus.nodeExporter = {
10 enable = mkEnableOption "prometheus node exporter";
11
12 port = mkOption {
13 type = types.int;
14 default = 9100;
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 enabledCollectors = mkOption {
29 type = types.listOf types.string;
30 default = [];
31 example = ''[ "systemd" ]'';
32 description = ''
33 Collectors to enable. The collectors listed here are enabled in addition to the default ones.
34 '';
35 };
36
37 disabledCollectors = mkOption {
38 type = types.listOf types.str;
39 default = [];
40 example = ''[ "timex" ]'';
41 description = ''
42 Collectors to disable which are enabled by default.
43 '';
44 };
45
46 extraFlags = mkOption {
47 type = types.listOf types.str;
48 default = [];
49 description = ''
50 Extra commandline options when launching the node exporter.
51 '';
52 };
53
54 openFirewall = mkOption {
55 type = types.bool;
56 default = false;
57 description = ''
58 Open port in firewall for incoming connections.
59 '';
60 };
61 };
62 };
63
64 config = mkIf cfg.enable {
65 networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
66
67 systemd.services.prometheus-node-exporter = {
68 description = "Prometheus exporter for machine metrics";
69 unitConfig.Documentation = "https://github.com/prometheus/node_exporter";
70 wantedBy = [ "multi-user.target" ];
71 script = ''
72 exec ${pkgs.prometheus-node-exporter}/bin/node_exporter \
73 ${concatMapStringsSep " " (x: "--collector." + x) cfg.enabledCollectors} \
74 ${concatMapStringsSep " " (x: "--no-collector." + x) cfg.disabledCollectors} \
75 --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
76 ${concatStringsSep " \\\n " cfg.extraFlags}
77 '';
78 serviceConfig = {
79 User = "nobody";
80 Restart = "always";
81 PrivateTmp = true;
82 WorkingDirectory = /tmp;
83 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
84 };
85 };
86 };
87}