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 [
|
2020-06-10 19:13:07 +01:00
|
|
|
"apcupsd"
|
2019-07-17 13:19:18 +01:00
|
|
|
"bind"
|
2021-01-01 04:43:59 +00:00
|
|
|
"bird"
|
2019-07-17 13:19:18 +01:00
|
|
|
"blackbox"
|
|
|
|
"collectd"
|
|
|
|
"dnsmasq"
|
|
|
|
"dovecot"
|
|
|
|
"fritzbox"
|
|
|
|
"json"
|
2020-06-10 18:12:43 +01:00
|
|
|
"keylight"
|
2020-06-04 00:38:09 +01:00
|
|
|
"lnd"
|
2019-07-29 23:34:45 +01:00
|
|
|
"mail"
|
2020-03-05 21:39:38 +00:00
|
|
|
"mikrotik"
|
2019-07-17 13:19:18 +01:00
|
|
|
"minio"
|
2020-07-16 19:37:09 +01:00
|
|
|
"modemmanager"
|
2019-10-12 21:35:15 +01:00
|
|
|
"nextcloud"
|
2019-07-17 13:19:18 +01:00
|
|
|
"nginx"
|
2020-12-13 17:26:13 +00:00
|
|
|
"nginxlog"
|
2019-07-17 13:19:18 +01:00
|
|
|
"node"
|
2020-09-25 11:24:09 +01:00
|
|
|
"openvpn"
|
2019-07-17 13:19:18 +01:00
|
|
|
"postfix"
|
2019-08-01 22:10:08 +01:00
|
|
|
"postgres"
|
2020-12-05 23:26:59 +00:00
|
|
|
"py-air-control"
|
2020-06-18 06:46:52 +01:00
|
|
|
"redis"
|
2018-08-21 00:43:47 +01:00
|
|
|
"rspamd"
|
2020-08-28 22:10:58 +01:00
|
|
|
"rtl_433"
|
2019-07-17 13:19:18 +01:00
|
|
|
"snmp"
|
2020-12-29 17:47:58 +00:00
|
|
|
"smokeping"
|
2020-08-06 07:56:35 +01:00
|
|
|
"sql"
|
2019-07-17 13:19:18 +01:00
|
|
|
"surfboard"
|
|
|
|
"tor"
|
|
|
|
"unifi"
|
2020-08-12 13:10:08 +01:00
|
|
|
"unifi-poller"
|
2019-07-17 13:19:18 +01:00
|
|
|
"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;
|
2020-09-07 09:14:33 +01:00
|
|
|
default = "-p tcp -m tcp --dport ${toString cfg.${name}.port}";
|
|
|
|
defaultText = "-p tcp -m tcp --dport ${toString port}";
|
2018-03-09 20:33:09 +00: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 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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
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;
|
2018-10-08 15:30:53 +01:00
|
|
|
serviceConfig.User = conf.user;
|
|
|
|
serviceConfig.Group = conf.group;
|
2020-09-29 09:46:59 +01:00
|
|
|
} serviceOpts ]);
|
2018-03-09 20:33:09 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2019-12-10 01:51:19 +00: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 20:33:09 +00: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-29 23:34:45 +01:00
|
|
|
assertions = [ {
|
2020-03-06 00:11:21 +00:00
|
|
|
assertion = cfg.snmp.enable -> (
|
|
|
|
(cfg.snmp.configurationPath == null) != (cfg.snmp.configuration == null)
|
|
|
|
);
|
2018-03-09 20:33:09 +00:00
|
|
|
message = ''
|
|
|
|
Please ensure you have either `services.prometheus.exporters.snmp.configuration'
|
|
|
|
or `services.prometheus.exporters.snmp.configurationPath' set!
|
|
|
|
'';
|
2020-03-05 21:39:38 +00: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-29 23:34:45 +01:00
|
|
|
} {
|
2020-03-06 00:41:40 +00:00
|
|
|
assertion = cfg.mail.enable -> (
|
|
|
|
(cfg.mail.configFile == null) != (cfg.mail.configuration == null)
|
|
|
|
);
|
2019-07-29 23:34:45 +01:00
|
|
|
message = ''
|
|
|
|
Please specify either 'services.prometheus.exporters.mail.configuration'
|
|
|
|
or 'services.prometheus.exporters.mail.configFile'.
|
|
|
|
'';
|
2020-08-06 07:56:35 +01: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'
|
|
|
|
'';
|
2019-07-29 23:34:45 +01:00
|
|
|
} ];
|
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";
|
2020-08-28 22:10:58 +01:00
|
|
|
})] ++ [(mkIf config.services.prometheus.exporters.rtl_433.enable {
|
|
|
|
hardware.rtl-sdr.enable = mkDefault true;
|
2019-11-25 21:10:03 +00:00
|
|
|
})] ++ [(mkIf config.services.nginx.enable {
|
|
|
|
systemd.services.prometheus-nginx-exporter.after = [ "nginx.service" ];
|
|
|
|
systemd.services.prometheus-nginx-exporter.requires = [ "nginx.service" ];
|
2018-07-06 20:13:13 +01:00
|
|
|
})] ++ [(mkIf config.services.postfix.enable {
|
|
|
|
services.prometheus.exporters.postfix.group = mkDefault config.services.postfix.setgidGroup;
|
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
|
|
|
}
|