2019-07-17 13:19:18 +01:00
|
|
|
{ config, pkgs, lib, options, ... }:
|
2018-03-09 20:33:09 +00:00
|
|
|
|
|
|
|
let
|
2019-07-17 13:19:18 +01:00
|
|
|
inherit (lib) concatStrings foldl foldl' genAttrs literalExample maintainers
|
|
|
|
mapAttrsToList mkDefault mkEnableOption mkIf mkMerge mkOption
|
|
|
|
optional types;
|
|
|
|
|
2018-03-09 20:33:09 +00: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 13:19:18 +01:00
|
|
|
|
|
|
|
exporterOpts = genAttrs [
|
|
|
|
"bind"
|
|
|
|
"blackbox"
|
|
|
|
"collectd"
|
|
|
|
"dnsmasq"
|
|
|
|
"dovecot"
|
|
|
|
"fritzbox"
|
|
|
|
"json"
|
2019-07-29 23:34:45 +01:00
|
|
|
"mail"
|
2019-07-17 13:19:18 +01:00
|
|
|
"minio"
|
2019-10-12 21:35:15 +01:00
|
|
|
"nextcloud"
|
2019-07-17 13:19:18 +01:00
|
|
|
"nginx"
|
|
|
|
"node"
|
|
|
|
"postfix"
|
2019-08-01 22:10:08 +01:00
|
|
|
"postgres"
|
2018-08-21 00:43:47 +01:00
|
|
|
"rspamd"
|
2019-07-17 13:19:18 +01:00
|
|
|
"snmp"
|
|
|
|
"surfboard"
|
|
|
|
"tor"
|
|
|
|
"unifi"
|
|
|
|
"varnish"
|
|
|
|
"wireguard"
|
|
|
|
] (name:
|
|
|
|
import (./. + "/exporters/${name}.nix") { inherit config lib pkgs options; }
|
|
|
|
);
|
2018-03-09 20:33:09 +00:00
|
|
|
|
|
|
|
mkExporterOpts = ({ name, port }: {
|
|
|
|
enable = mkEnableOption "the prometheus ${name} exporter";
|
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
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 {
|
|
|
|
type = types.str;
|
|
|
|
default = "-p tcp -m tcp --dport ${toString port}";
|
|
|
|
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 14:27:00 +01:00
|
|
|
is true. It is used as `ip46tables -I nixos-fw <option>firewallFilter</option> -j nixos-fw-accept`.
|
2018-03-09 20:33:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
2019-08-02 14:10:27 +01:00
|
|
|
default = "${name}-exporter";
|
2018-03-09 20:33:09 +00:00
|
|
|
description = ''
|
|
|
|
User name under which the ${name} exporter shall be run.
|
|
|
|
Has no effect when <option>systemd.services.prometheus-${name}-exporter.serviceConfig.DynamicUser</option> is true.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
2019-08-02 14:10:27 +01:00
|
|
|
default = "${name}-exporter";
|
2018-03-09 20:33:09 +00:00
|
|
|
description = ''
|
|
|
|
Group under which the ${name} exporter shall be run.
|
|
|
|
Has no effect when <option>systemd.services.prometheus-${name}-exporter.serviceConfig.DynamicUser</option> is true.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-07-17 13:33:40 +01:00
|
|
|
mkSubModule = { name, port, extraOpts, imports }: {
|
2018-03-09 20:33:09 +00:00
|
|
|
${name} = mkOption {
|
|
|
|
type = types.submodule {
|
2019-07-17 13:33:40 +01:00
|
|
|
inherit imports;
|
2018-03-09 20:33:09 +00:00
|
|
|
options = (mkExporterOpts {
|
|
|
|
inherit name port;
|
|
|
|
} // extraOpts);
|
|
|
|
};
|
|
|
|
internal = true;
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkSubModules = (foldl' (a: b: a//b) {}
|
|
|
|
(mapAttrsToList (name: opts: mkSubModule {
|
|
|
|
inherit name;
|
2019-07-17 13:33:40 +01:00
|
|
|
inherit (opts) port;
|
2018-03-09 20:33:09 +00:00
|
|
|
extraOpts = opts.extraOpts or {};
|
2019-07-17 13:33:40 +01:00
|
|
|
imports = opts.imports or [];
|
2018-03-09 20:33:09 +00:00
|
|
|
}) exporterOpts)
|
|
|
|
);
|
|
|
|
|
|
|
|
mkExporterConf = { name, conf, serviceOpts }:
|
2019-08-02 14:23:23 +01:00
|
|
|
let
|
|
|
|
enableDynamicUser = serviceOpts.serviceConfig.DynamicUser or true;
|
|
|
|
in
|
2018-03-09 20:33:09 +00:00
|
|
|
mkIf conf.enable {
|
2019-07-17 13:33:40 +01:00
|
|
|
warnings = conf.warnings or [];
|
2019-09-01 11:31:30 +01: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 14:10:27 +01:00
|
|
|
});
|
2019-08-02 14:23:23 +01:00
|
|
|
users.groups = (mkIf (conf.group == "${name}-exporter" && !enableDynamicUser) {
|
2019-08-02 14:10:27 +01:00
|
|
|
"${name}-exporter" = {};
|
|
|
|
});
|
2018-08-02 14:27:00 +01:00
|
|
|
networking.firewall.extraCommands = mkIf conf.openFirewall (concatStrings [
|
2019-03-13 10:44:36 +00:00
|
|
|
"ip46tables -A nixos-fw ${conf.firewallFilter} "
|
2018-08-02 14:27:00 +01:00
|
|
|
"-m comment --comment ${name}-exporter -j nixos-fw-accept"
|
|
|
|
]);
|
2018-03-09 20:33:09 +00:00
|
|
|
systemd.services."prometheus-${name}-exporter" = mkMerge ([{
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2018-10-08 15:30:53 +01:00
|
|
|
serviceConfig.Restart = mkDefault "always";
|
|
|
|
serviceConfig.PrivateTmp = mkDefault true;
|
|
|
|
serviceConfig.WorkingDirectory = mkDefault /tmp;
|
2019-08-02 14:23:23 +01:00
|
|
|
serviceConfig.DynamicUser = mkDefault enableDynamicUser;
|
|
|
|
} serviceOpts ] ++ optional (!enableDynamicUser) {
|
2018-10-08 15:30:53 +01:00
|
|
|
serviceConfig.User = conf.user;
|
|
|
|
serviceConfig.Group = conf.group;
|
|
|
|
});
|
2018-03-09 20:33:09 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
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-29 23:34:45 +01:00
|
|
|
assertions = [ {
|
2018-03-09 20:33:09 +00:00
|
|
|
assertion = (cfg.snmp.configurationPath == null) != (cfg.snmp.configuration == null);
|
|
|
|
message = ''
|
|
|
|
Please ensure you have either `services.prometheus.exporters.snmp.configuration'
|
|
|
|
or `services.prometheus.exporters.snmp.configurationPath' set!
|
|
|
|
'';
|
2019-07-29 23:34:45 +01:00
|
|
|
} {
|
|
|
|
assertion = (cfg.mail.configFile == null) != (cfg.mail.configuration == {});
|
|
|
|
message = ''
|
|
|
|
Please specify either 'services.prometheus.exporters.mail.configuration'
|
|
|
|
or 'services.prometheus.exporters.mail.configFile'.
|
|
|
|
'';
|
|
|
|
} ];
|
2018-03-09 20:33:09 +00: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;
|
2018-08-21 00:43:47 +01:00
|
|
|
})] ++ [(mkIf config.services.rspamd.enable {
|
|
|
|
services.prometheus.exporters.rspamd.url = mkDefault "http://localhost:11334/stat";
|
2018-03-09 20:33:09 +00:00
|
|
|
})] ++ (mapAttrsToList (name: conf:
|
|
|
|
mkExporterConf {
|
|
|
|
inherit name;
|
|
|
|
inherit (conf) serviceOpts;
|
|
|
|
conf = cfg.${name};
|
|
|
|
}) exporterOpts)
|
|
|
|
);
|
2018-03-14 14:59:28 +00:00
|
|
|
|
2018-10-08 15:30:53 +01:00
|
|
|
meta = {
|
|
|
|
doc = ./exporters.xml;
|
|
|
|
maintainers = [ maintainers.willibutz ];
|
|
|
|
};
|
2018-03-09 20:33:09 +00:00
|
|
|
}
|