2019-07-17 14:19:18 +02:00
|
|
|
{ config, pkgs, lib, options, ... }:
|
2018-03-09 21:33:09 +01:00
|
|
|
|
|
|
|
let
|
2019-07-17 14:19:18 +02:00
|
|
|
inherit (lib) concatStrings foldl foldl' genAttrs literalExample maintainers
|
|
|
|
mapAttrsToList mkDefault mkEnableOption mkIf mkMerge mkOption
|
2021-03-05 13:35:16 +01:00
|
|
|
optional types mkOptionDefault flip attrNames;
|
2019-07-17 14:19:18 +02:00
|
|
|
|
2018-03-09 21:33:09 +01:00
|
|
|
cfg = config.services.prometheus.exporters;
|
|
|
|
|
|
|
|
# each attribute in `exporterOpts` is expected to have specified:
|
|
|
|
# - port (types.int): port on which the exporter listens
|
|
|
|
# - serviceOpts (types.attrs): config that is merged with the
|
|
|
|
# default definition of the exporter's
|
|
|
|
# systemd service
|
|
|
|
# - extraOpts (types.attrs): extra configuration options to
|
|
|
|
# configure the exporter with, which
|
|
|
|
# are appended to the default options
|
|
|
|
#
|
|
|
|
# Note that `extraOpts` is optional, but a script for the exporter's
|
|
|
|
# systemd service must be provided by specifying either
|
|
|
|
# `serviceOpts.script` or `serviceOpts.serviceConfig.ExecStart`
|
2019-07-17 14:19:18 +02:00
|
|
|
|
|
|
|
exporterOpts = genAttrs [
|
2020-06-10 20:13:07 +02:00
|
|
|
"apcupsd"
|
2021-01-31 01:03:55 +01:00
|
|
|
"artifactory"
|
2019-07-17 14:19:18 +02:00
|
|
|
"bind"
|
2021-01-01 05:43:59 +01:00
|
|
|
"bird"
|
2019-07-17 14:19:18 +02:00
|
|
|
"blackbox"
|
|
|
|
"collectd"
|
|
|
|
"dnsmasq"
|
|
|
|
"dovecot"
|
|
|
|
"fritzbox"
|
|
|
|
"json"
|
2021-03-12 11:11:16 +01:00
|
|
|
"jitsi"
|
2020-06-10 19:12:43 +02:00
|
|
|
"keylight"
|
2021-03-05 13:06:40 +01:00
|
|
|
"knot"
|
2020-06-04 01:38:09 +02:00
|
|
|
"lnd"
|
2019-07-30 00:34:45 +02:00
|
|
|
"mail"
|
2020-03-05 22:39:38 +01:00
|
|
|
"mikrotik"
|
2019-07-17 14:19:18 +02:00
|
|
|
"minio"
|
2020-07-16 20:37:09 +02:00
|
|
|
"modemmanager"
|
2019-10-12 22:35:15 +02:00
|
|
|
"nextcloud"
|
2019-07-17 14:19:18 +02:00
|
|
|
"nginx"
|
2020-12-13 18:26:13 +01:00
|
|
|
"nginxlog"
|
2019-07-17 14:19:18 +02:00
|
|
|
"node"
|
2020-09-25 12:24:09 +02:00
|
|
|
"openvpn"
|
2019-07-17 14:19:18 +02:00
|
|
|
"postfix"
|
2019-08-01 23:10:08 +02:00
|
|
|
"postgres"
|
2020-12-06 00:26:59 +01:00
|
|
|
"py-air-control"
|
2020-06-18 07:46:52 +02:00
|
|
|
"redis"
|
2018-08-21 01:43:47 +02:00
|
|
|
"rspamd"
|
2020-08-28 23:10:58 +02:00
|
|
|
"rtl_433"
|
2019-07-17 14:19:18 +02:00
|
|
|
"snmp"
|
2020-12-29 18:47:58 +01:00
|
|
|
"smokeping"
|
2020-08-06 08:56:35 +02:00
|
|
|
"sql"
|
2019-07-17 14:19:18 +02:00
|
|
|
"surfboard"
|
2021-02-17 08:08:45 +01:00
|
|
|
"systemd"
|
2019-07-17 14:19:18 +02:00
|
|
|
"tor"
|
|
|
|
"unifi"
|
2020-08-12 14:10:08 +02:00
|
|
|
"unifi-poller"
|
2019-07-17 14:19:18 +02:00
|
|
|
"varnish"
|
|
|
|
"wireguard"
|
2021-02-08 22:56:50 +01:00
|
|
|
"flow"
|
2019-07-17 14:19:18 +02:00
|
|
|
] (name:
|
|
|
|
import (./. + "/exporters/${name}.nix") { inherit config lib pkgs options; }
|
|
|
|
);
|
2018-03-09 21:33:09 +01:00
|
|
|
|
|
|
|
mkExporterOpts = ({ name, port }: {
|
|
|
|
enable = mkEnableOption "the prometheus ${name} exporter";
|
|
|
|
port = mkOption {
|
2021-03-05 13:06:03 +01:00
|
|
|
type = types.port;
|
2018-03-09 21:33:09 +01:00
|
|
|
default = port;
|
|
|
|
description = ''
|
|
|
|
Port to listen on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
listenAddress = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0.0.0.0";
|
|
|
|
description = ''
|
|
|
|
Address to listen on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Extra commandline options to pass to the ${name} exporter.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Open port in firewall for incoming connections.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
firewallFilter = mkOption {
|
2021-03-05 13:35:16 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2018-03-09 21:33:09 +01:00
|
|
|
example = literalExample ''
|
|
|
|
"-i eth0 -p tcp -m tcp --dport ${toString port}"
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Specify a filter for iptables to use when
|
|
|
|
<option>services.prometheus.exporters.${name}.openFirewall</option>
|
2018-08-02 15:27:00 +02:00
|
|
|
is true. It is used as `ip46tables -I nixos-fw <option>firewallFilter</option> -j nixos-fw-accept`.
|
2018-03-09 21:33:09 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
2019-08-02 15:10:27 +02:00
|
|
|
default = "${name}-exporter";
|
2018-03-09 21:33:09 +01:00
|
|
|
description = ''
|
|
|
|
User name under which the ${name} exporter shall be run.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
2019-08-02 15:10:27 +02:00
|
|
|
default = "${name}-exporter";
|
2018-03-09 21:33:09 +01:00
|
|
|
description = ''
|
|
|
|
Group under which the ${name} exporter shall be run.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-07-17 14:33:40 +02:00
|
|
|
mkSubModule = { name, port, extraOpts, imports }: {
|
2018-03-09 21:33:09 +01:00
|
|
|
${name} = mkOption {
|
2021-03-05 13:35:16 +01:00
|
|
|
type = types.submodule [{
|
2019-07-17 14:33:40 +02:00
|
|
|
inherit imports;
|
2018-03-09 21:33:09 +01:00
|
|
|
options = (mkExporterOpts {
|
|
|
|
inherit name port;
|
|
|
|
} // extraOpts);
|
2021-03-05 13:35:16 +01:00
|
|
|
} ({ config, ... }: mkIf config.openFirewall {
|
2021-03-13 15:21:19 +01:00
|
|
|
firewallFilter = mkDefault "-p tcp -m tcp --dport ${toString config.port}";
|
2021-03-05 13:35:16 +01:00
|
|
|
})];
|
2018-03-09 21:33:09 +01:00
|
|
|
internal = true;
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkSubModules = (foldl' (a: b: a//b) {}
|
|
|
|
(mapAttrsToList (name: opts: mkSubModule {
|
|
|
|
inherit name;
|
2019-07-17 14:33:40 +02:00
|
|
|
inherit (opts) port;
|
2018-03-09 21:33:09 +01:00
|
|
|
extraOpts = opts.extraOpts or {};
|
2019-07-17 14:33:40 +02:00
|
|
|
imports = opts.imports or [];
|
2018-03-09 21:33:09 +01:00
|
|
|
}) exporterOpts)
|
|
|
|
);
|
|
|
|
|
|
|
|
mkExporterConf = { name, conf, serviceOpts }:
|
2019-08-02 15:23:23 +02:00
|
|
|
let
|
|
|
|
enableDynamicUser = serviceOpts.serviceConfig.DynamicUser or true;
|
|
|
|
in
|
2018-03-09 21:33:09 +01:00
|
|
|
mkIf conf.enable {
|
2019-07-17 14:33:40 +02:00
|
|
|
warnings = conf.warnings or [];
|
2019-09-01 12:31:30 +02:00
|
|
|
users.users."${name}-exporter" = (mkIf (conf.user == "${name}-exporter" && !enableDynamicUser) {
|
|
|
|
description = "Prometheus ${name} exporter service user";
|
|
|
|
isSystemUser = true;
|
|
|
|
inherit (conf) group;
|
2019-08-02 15:10:27 +02:00
|
|
|
});
|
2019-08-02 15:23:23 +02:00
|
|
|
users.groups = (mkIf (conf.group == "${name}-exporter" && !enableDynamicUser) {
|
2019-08-02 15:10:27 +02:00
|
|
|
"${name}-exporter" = {};
|
|
|
|
});
|
2018-08-02 15:27:00 +02:00
|
|
|
networking.firewall.extraCommands = mkIf conf.openFirewall (concatStrings [
|
2019-03-13 11:44:36 +01:00
|
|
|
"ip46tables -A nixos-fw ${conf.firewallFilter} "
|
2018-08-02 15:27:00 +02:00
|
|
|
"-m comment --comment ${name}-exporter -j nixos-fw-accept"
|
|
|
|
]);
|
2018-03-09 21:33:09 +01:00
|
|
|
systemd.services."prometheus-${name}-exporter" = mkMerge ([{
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2018-10-08 16:30:53 +02:00
|
|
|
serviceConfig.Restart = mkDefault "always";
|
|
|
|
serviceConfig.PrivateTmp = mkDefault true;
|
|
|
|
serviceConfig.WorkingDirectory = mkDefault /tmp;
|
2019-08-02 15:23:23 +02:00
|
|
|
serviceConfig.DynamicUser = mkDefault enableDynamicUser;
|
2018-10-08 16:30:53 +02:00
|
|
|
serviceConfig.User = conf.user;
|
|
|
|
serviceConfig.Group = conf.group;
|
2020-09-29 10:46:59 +02:00
|
|
|
} serviceOpts ]);
|
2018-03-09 21:33:09 +01:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2019-12-10 02:51:19 +01:00
|
|
|
|
|
|
|
imports = (lib.forEach [ "blackboxExporter" "collectdExporter" "fritzboxExporter"
|
|
|
|
"jsonExporter" "minioExporter" "nginxExporter" "nodeExporter"
|
|
|
|
"snmpExporter" "unifiExporter" "varnishExporter" ]
|
|
|
|
(opt: lib.mkRemovedOptionModule [ "services" "prometheus" "${opt}" ] ''
|
|
|
|
The prometheus exporters are now configured using `services.prometheus.exporters'.
|
|
|
|
See the 18.03 release notes for more information.
|
|
|
|
'' ));
|
|
|
|
|
2018-03-09 21:33:09 +01:00
|
|
|
options.services.prometheus.exporters = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
options = (mkSubModules);
|
|
|
|
};
|
|
|
|
description = "Prometheus exporter configuration";
|
|
|
|
default = {};
|
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
node = {
|
|
|
|
enable = true;
|
|
|
|
enabledCollectors = [ "systemd" ];
|
|
|
|
};
|
|
|
|
varnish.enable = true;
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge ([{
|
2019-07-30 00:34:45 +02:00
|
|
|
assertions = [ {
|
2020-03-06 01:11:21 +01:00
|
|
|
assertion = cfg.snmp.enable -> (
|
|
|
|
(cfg.snmp.configurationPath == null) != (cfg.snmp.configuration == null)
|
|
|
|
);
|
2018-03-09 21:33:09 +01:00
|
|
|
message = ''
|
|
|
|
Please ensure you have either `services.prometheus.exporters.snmp.configuration'
|
|
|
|
or `services.prometheus.exporters.snmp.configurationPath' set!
|
|
|
|
'';
|
2020-03-05 22:39:38 +01:00
|
|
|
} {
|
|
|
|
assertion = cfg.mikrotik.enable -> (
|
|
|
|
(cfg.mikrotik.configFile == null) != (cfg.mikrotik.configuration == null)
|
|
|
|
);
|
|
|
|
message = ''
|
|
|
|
Please specify either `services.prometheus.exporters.mikrotik.configuration'
|
|
|
|
or `services.prometheus.exporters.mikrotik.configFile'.
|
|
|
|
'';
|
2019-07-30 00:34:45 +02:00
|
|
|
} {
|
2020-03-06 01:41:40 +01:00
|
|
|
assertion = cfg.mail.enable -> (
|
|
|
|
(cfg.mail.configFile == null) != (cfg.mail.configuration == null)
|
|
|
|
);
|
2019-07-30 00:34:45 +02:00
|
|
|
message = ''
|
|
|
|
Please specify either 'services.prometheus.exporters.mail.configuration'
|
|
|
|
or 'services.prometheus.exporters.mail.configFile'.
|
|
|
|
'';
|
2020-08-06 08:56:35 +02:00
|
|
|
} {
|
|
|
|
assertion = cfg.sql.enable -> (
|
|
|
|
(cfg.sql.configFile == null) != (cfg.sql.configuration == null)
|
|
|
|
);
|
|
|
|
message = ''
|
|
|
|
Please specify either 'services.prometheus.exporters.sql.configuration' or
|
|
|
|
'services.prometheus.exporters.sql.configFile'
|
|
|
|
'';
|
2021-03-05 13:35:16 +01:00
|
|
|
} ] ++ (flip map (attrNames cfg) (exporter: {
|
|
|
|
assertion = cfg.${exporter}.firewallFilter != null -> cfg.${exporter}.openFirewall;
|
|
|
|
message = ''
|
|
|
|
The `firewallFilter'-option of exporter ${exporter} doesn't have any effect unless
|
|
|
|
`openFirewall' is set to `true'!
|
|
|
|
'';
|
|
|
|
}));
|
2018-03-09 21:33:09 +01:00
|
|
|
}] ++ [(mkIf config.services.minio.enable {
|
|
|
|
services.prometheus.exporters.minio.minioAddress = mkDefault "http://localhost:9000";
|
|
|
|
services.prometheus.exporters.minio.minioAccessKey = mkDefault config.services.minio.accessKey;
|
|
|
|
services.prometheus.exporters.minio.minioAccessSecret = mkDefault config.services.minio.secretKey;
|
2020-08-28 23:10:58 +02:00
|
|
|
})] ++ [(mkIf config.services.prometheus.exporters.rtl_433.enable {
|
|
|
|
hardware.rtl-sdr.enable = mkDefault true;
|
2018-07-06 21:13:13 +02:00
|
|
|
})] ++ [(mkIf config.services.postfix.enable {
|
|
|
|
services.prometheus.exporters.postfix.group = mkDefault config.services.postfix.setgidGroup;
|
2018-03-09 21:33:09 +01:00
|
|
|
})] ++ (mapAttrsToList (name: conf:
|
|
|
|
mkExporterConf {
|
|
|
|
inherit name;
|
|
|
|
inherit (conf) serviceOpts;
|
|
|
|
conf = cfg.${name};
|
|
|
|
}) exporterOpts)
|
|
|
|
);
|
2018-03-14 15:59:28 +01:00
|
|
|
|
2018-10-08 16:30:53 +02:00
|
|
|
meta = {
|
|
|
|
doc = ./exporters.xml;
|
|
|
|
maintainers = [ maintainers.willibutz ];
|
|
|
|
};
|
2018-03-09 21:33:09 +01:00
|
|
|
}
|