1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.collectdExporter;
7
8 collectSettingsArgs = if (cfg.collectdBinary.enable) then ''
9 -collectd.listen-address ${optionalString (cfg.collectdBinary.listenAddress != null) cfg.collectdBinary.listenAddress}:${toString cfg.collectdBinary.port} \
10 -collectd.security-level ${cfg.collectdBinary.securityLevel} \
11 '' else "";
12
13in {
14 options = {
15 services.prometheus.collectdExporter = {
16 enable = mkEnableOption "prometheus collectd exporter";
17
18 port = mkOption {
19 type = types.int;
20 default = 9103;
21 description = ''
22 Port to listen on.
23 This is used for scraping as well as the to receive collectd data via the write_http plugin.
24 '';
25 };
26
27 listenAddress = mkOption {
28 type = types.nullOr types.str;
29 default = null;
30 example = "0.0.0.0";
31 description = ''
32 Address to listen on for web interface, telemetry and collectd JSON data.
33 '';
34 };
35
36 collectdBinary = {
37 enable = mkEnableOption "collectd binary protocol receiver";
38
39 authFile = mkOption {
40 default = null;
41 type = types.nullOr types.path;
42 description = "File mapping user names to pre-shared keys (passwords).";
43 };
44
45 port = mkOption {
46 type = types.int;
47 default = 25826;
48 description = ''Network address on which to accept collectd binary network packets.'';
49 };
50
51 listenAddress = mkOption {
52 type = types.nullOr types.str;
53 default = null;
54 example = "0.0.0.0";
55 description = ''
56 Address to listen on for binary network packets.
57 '';
58 };
59
60 securityLevel = mkOption {
61 type = types.enum ["None" "Sign" "Encrypt"];
62 default = "None";
63 description = ''
64 Minimum required security level for accepted packets.
65 '';
66 };
67 };
68
69 extraFlags = mkOption {
70 type = types.listOf types.str;
71 default = [];
72 description = ''
73 Extra commandline options when launching the collectd exporter.
74 '';
75 };
76
77 logFormat = mkOption {
78 type = types.str;
79 default = "logger:stderr";
80 example = "logger:syslog?appname=bob&local=7 or logger:stdout?json=true";
81 description = ''
82 Set the log target and format.
83 '';
84 };
85
86 logLevel = mkOption {
87 type = types.enum ["debug" "info" "warn" "error" "fatal"];
88 default = "info";
89 description = ''
90 Only log messages with the given severity or above.
91 '';
92 };
93
94 openFirewall = mkOption {
95 type = types.bool;
96 default = false;
97 description = ''
98 Open port in firewall for incoming connections.
99 '';
100 };
101 };
102 };
103
104 config = mkIf cfg.enable {
105 networking.firewall.allowedTCPPorts = (optional cfg.openFirewall cfg.port) ++
106 (optional (cfg.openFirewall && cfg.collectdBinary.enable) cfg.collectdBinary.port);
107
108 systemd.services.prometheus-collectd-exporter = {
109 description = "Prometheus exporter for Collectd metrics";
110 unitConfig.Documentation = "https://github.com/prometheus/collectd_exporter";
111 wantedBy = [ "multi-user.target" ];
112 serviceConfig = {
113 DynamicUser = true;
114 Restart = "always";
115 PrivateTmp = true;
116 WorkingDirectory = /tmp;
117 ExecStart = ''
118 ${pkgs.prometheus-collectd-exporter}/bin/collectd_exporter \
119 -log.format ${cfg.logFormat} \
120 -log.level ${cfg.logLevel} \
121 -web.listen-address ${optionalString (cfg.listenAddress != null) cfg.listenAddress}:${toString cfg.port} \
122 ${collectSettingsArgs} \
123 ${concatStringsSep " " cfg.extraFlags}
124 '';
125 };
126 };
127 };
128}