1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.nodeExporter;
7 cmdlineArgs = cfg.extraFlags ++ [
8 "-web.listen-address=${cfg.listenAddress}"
9 ];
10in {
11 options = {
12 services.prometheus.nodeExporter = {
13 enable = mkEnableOption "prometheus node exporter";
14
15 port = mkOption {
16 type = types.int;
17 default = 9100;
18 description = ''
19 Port to listen on.
20 '';
21 };
22
23 listenAddress = mkOption {
24 type = types.string;
25 default = "0.0.0.0";
26 description = ''
27 Address to listen on.
28 '';
29 };
30
31 enabledCollectors = mkOption {
32 type = types.listOf types.string;
33 default = [];
34 example = ''[ "systemd" ]'';
35 description = ''
36 Collectors to enable, additionally to the defaults.
37 '';
38 };
39
40 extraFlags = mkOption {
41 type = types.listOf types.str;
42 default = [];
43 description = ''
44 Extra commandline options when launching the node exporter.
45 '';
46 };
47
48 openFirewall = mkOption {
49 type = types.bool;
50 default = false;
51 description = ''
52 Open port in firewall for incoming connections.
53 '';
54 };
55 };
56 };
57
58 config = mkIf cfg.enable {
59 networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
60
61 systemd.services.prometheus-node-exporter = {
62 description = "Prometheus exporter for machine metrics";
63 unitConfig.Documentation = "https://github.com/prometheus/node_exporter";
64 wantedBy = [ "multi-user.target" ];
65 script = ''
66 exec ${pkgs.prometheus-node-exporter}/bin/node_exporter \
67 ${optionalString (cfg.enabledCollectors != [])
68 ''-collectors.enabled ${concatStringsSep "," cfg.enabledCollectors}''} \
69 -web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
70 ${concatStringsSep " \\\n " cfg.extraFlags}
71 '';
72 serviceConfig = {
73 User = "nobody";
74 Restart = "always";
75 PrivateTmp = true;
76 WorkingDirectory = /tmp;
77 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
78 };
79 };
80 };
81}