Rollback versionning of services.prometheus.{exporters, alertmanager}.

This commit is contained in:
Jean-Baptiste Giraudeau 2019-03-22 17:03:00 +01:00
parent e17b464a43
commit bfbae97cfa
No known key found for this signature in database
GPG Key ID: E96EF57FD501B961
19 changed files with 848 additions and 903 deletions

View File

@ -4,33 +4,31 @@ with lib;
let
cfg = config.services.prometheus.alertmanager;
cfg2 = config.services.prometheus2.alertmanager;
mkConfigFile = amCfg:
pkgs.writeText "alertmanager.yml" (builtins.toJSON amCfg.configuration);
mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
mkAlertmanagerYml = amCfg: let
checkedConfig = file:
pkgs.runCommand "checked-config" { buildInputs = [ amCfg.package ]; } ''
checkedConfig = file: pkgs.runCommand "checked-config" { buildInputs = [ cfg.package ]; } ''
ln -s ${file} $out
amtool check-config $out
'';
yml = if amCfg.configText != null then
pkgs.writeText "alertmanager.yml" amCfg.configText
else mkConfigFile amCfg;
in
checkedConfig yml;
mkCmdlineArgs = amCfg:
amCfg.extraFlags ++ [
"--config.file ${mkAlertmanagerYml amCfg}"
"--web.listen-address ${amCfg.listenAddress}:${toString amCfg.port}"
"--log.level ${amCfg.logLevel}"
] ++ (optional (amCfg.webExternalUrl != null)
"--web.external-url ${amCfg.webExternalUrl}"
) ++ (optional (amCfg.logFormat != null)
"--log.format ${amCfg.logFormat}"
alertmanagerYml = let
yml = if cfg.configText != null then
pkgs.writeText "alertmanager.yml" cfg.configText
else mkConfigFile;
in checkedConfig yml;
cmdlineArgs = cfg.extraFlags ++ [
"--config.file ${alertmanagerYml}"
"--web.listen-address ${cfg.listenAddress}:${toString cfg.port}"
"--log.level ${cfg.logLevel}"
] ++ (optional (cfg.webExternalUrl != null)
"--web.external-url ${cfg.webExternalUrl}"
) ++ (optional (cfg.logFormat != null)
"--log.format ${cfg.logFormat}"
);
amOptions = {
in {
options = {
services.prometheus.alertmanager = {
enable = mkEnableOption "Prometheus Alertmanager";
package = mkOption {
@ -137,27 +135,30 @@ let
'';
};
};
mkAMConfig = amCfg: amVersion: [
(mkIf amCfg.enable {
};
config = mkMerge [
(mkIf cfg.enable {
assertions = singleton {
assertion = amCfg.configuration != null || amCfg.configText != null;
assertion = cfg.configuration != null || cfg.configText != null;
message = "Can not enable alertmanager without a configuration. "
+ "Set either the `configuration` or `configText` attribute.";
};
})
(mkIf amCfg.enable {
networking.firewall.allowedTCPPorts = optional amCfg.openFirewall amCfg.port;
(mkIf cfg.enable {
networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
systemd.services."alertmanager${amVersion}" = {
systemd.services.alertmanager = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
script = ''
${amCfg.package}/bin/alertmanager \
${concatStringsSep " \\\n " (mkCmdlineArgs amCfg)}
${cfg.package}/bin/alertmanager \
${concatStringsSep " \\\n " cmdlineArgs}
'';
serviceConfig = {
User = amCfg.user;
Group = amCfg.group;
User = cfg.user;
Group = cfg.group;
Restart = "always";
PrivateTmp = true;
WorkingDirectory = "/tmp";
@ -166,11 +167,4 @@ let
};
})
];
in {
options = {
services.prometheus.alertmanager = amOptions;
services.prometheus2.alertmanager = amOptions;
};
config = mkMerge ((mkAMConfig cfg "") ++ (mkAMConfig cfg2 "2"));
}

View File

@ -7,8 +7,6 @@ let
cfg2 = config.services.prometheus2;
promUser = "prometheus";
promGroup = "prometheus";
prom2User = "prometheus2";
prom2Group = "prometheus2";
# Get a submodule without any embedded metadata:
_filter = x: filterAttrs (k: v: k != "_module") x;
@ -693,11 +691,11 @@ in {
};
})
(mkIf cfg2.enable {
users.groups.${prom2Group}.gid = config.ids.gids.prometheus2;
users.users.${prom2User} = {
users.groups.${promGroup}.gid = config.ids.gids.prometheus2;
users.users.${promUser} = {
description = "Prometheus2 daemon user";
uid = config.ids.uids.prometheus2;
group = prom2Group;
group = promGroup;
home = cfg2.dataDir;
createHome = true;
};
@ -710,7 +708,7 @@ in {
${concatStringsSep " \\\n " cmdlineArgs2}
'';
serviceConfig = {
User = prom2User;
User = promUser;
Restart = "always";
WorkingDirectory = cfg2.dataDir;
};

View File

@ -4,10 +4,8 @@ with lib;
let
cfg = config.services.prometheus.exporters;
cfg2 = config.services.prometheus2.exporters;
# each attribute in `exporterOpts` is a function that when executed
# with `cfg` or `cfg2` as parameter is expected to have specified:
# 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
@ -110,16 +108,11 @@ let
};
};
mkSubModules = exCfg:
(foldl' (a: b: a//b) {}
(mapAttrsToList (name: confGen:
let
conf = (confGen exCfg);
in
mkSubModule {
mkSubModules = (foldl' (a: b: a//b) {}
(mapAttrsToList (name: opts: mkSubModule {
inherit name;
inherit (conf) port serviceOpts;
extraOpts = conf.extraOpts or {};
inherit (opts) port serviceOpts;
extraOpts = opts.extraOpts or {};
}) exporterOpts)
);
@ -140,36 +133,11 @@ let
serviceConfig.Group = conf.group;
});
};
mkExportersConfig = exCfg: promVersion:
([{
assertions = [{
assertion = (exCfg.snmp.configurationPath == null) != (exCfg.snmp.configuration == null);
message = ''
Please ensure you have either `services.prometheus.exporters.snmp.configuration'
or `services.prometheus${promVersion}.exporters.snmp.configurationPath' set!
'';
}];
}] ++ [(mkIf config.services.minio.enable {
services."prometheus${promVersion}".exporters.minio = {
minioAddress = mkDefault "http://localhost:9000";
minioAccessKey = mkDefault config.services.minio.accessKey;
minioAccessSecret = mkDefault config.services.minio.secretKey;
};
})] ++ (mapAttrsToList (name: confGen:
let
conf = (confGen exCfg);
in
mkExporterConf {
inherit name;
inherit (conf) serviceOpts;
conf = exCfg.${name};
}) exporterOpts)
);
in
{
options.services.prometheus.exporters = mkOption {
type = types.submodule {
options = (mkSubModules cfg);
options = (mkSubModules);
};
description = "Prometheus exporter configuration";
default = {};
@ -184,24 +152,25 @@ in
'';
};
options.services.prometheus2.exporters = mkOption {
type = types.submodule {
options = (mkSubModules cfg2);
};
description = "Prometheus 2 exporter configuration";
default = {};
example = literalExample ''
{
node = {
enable = true;
enabledCollectors = [ "systemd" ];
};
varnish.enable = true;
}
config = mkMerge ([{
assertions = [{
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!
'';
};
config = mkMerge ((mkExportersConfig cfg "") ++ (mkExportersConfig cfg2 "2"));
}];
}] ++ [(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;
})] ++ (mapAttrsToList (name: conf:
mkExporterConf {
inherit name;
inherit (conf) serviceOpts;
conf = cfg.${name};
}) exporterOpts)
);
meta = {
doc = ./exporters.xml;

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.bind;
in
{
let
cfg = config.services.prometheus.exporters.bind;
in
{
port = 9119;
extraOpts = {
bindURI = mkOption {
@ -53,4 +52,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.blackbox;
in
{
let
cfg = config.services.prometheus.exporters.blackbox;
in
{
port = 9115;
extraOpts = {
configFile = mkOption {
@ -29,4 +28,4 @@ baseCfg:
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.collectd;
in
{
let
cfg = config.services.prometheus.exporters.collectd;
in
{
port = 9103;
extraOpts = {
collectdBinary = {
@ -76,4 +75,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.dnsmasq;
in
{
let
cfg = config.services.prometheus.exporters.dnsmasq;
in
{
port = 9153;
extraOpts = {
dnsmasqListenAddress = mkOption {
@ -37,4 +36,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.dovecot;
in
{
let
cfg = config.services.prometheus.exporters.dovecot;
in
{
port = 9166;
extraOpts = {
telemetryPath = mkOption {
@ -70,4 +69,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.fritzbox;
in
{
let
cfg = config.services.prometheus.exporters.fritzbox;
in
{
port = 9133;
extraOpts = {
gatewayAddress = mkOption {
@ -37,4 +36,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.json;
in
{
let
cfg = config.services.prometheus.exporters.json;
in
{
port = 7979;
extraOpts = {
url = mkOption {
@ -34,4 +33,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.minio;
in
{
let
cfg = config.services.prometheus.exporters.minio;
in
{
port = 9290;
extraOpts = {
minioAddress = mkOption {
@ -63,4 +62,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.nginx;
in
{
let
cfg = config.services.prometheus.exporters.nginx;
in
{
port = 9113;
extraOpts = {
scrapeUri = mkOption {
@ -45,4 +44,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.node;
in
{
let
cfg = config.services.prometheus.exporters.node;
in
{
port = 9100;
extraOpts = {
enabledCollectors = mkOption {
@ -38,4 +37,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.postfix;
in
{
let
cfg = config.services.prometheus.exporters.postfix;
in
{
port = 9154;
extraOpts = {
telemetryPath = mkOption {
@ -79,4 +78,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.snmp;
in
{
let
cfg = config.services.prometheus.exporters.snmp;
in
{
port = 9116;
extraOpts = {
configurationPath = mkOption {
@ -61,12 +60,12 @@ baseCfg:
DynamicUser = true;
ExecStart = ''
${pkgs.prometheus-snmp-exporter.bin}/bin/snmp_exporter \
--config.file ${configFile} \
--log.format ${cfg.logFormat} \
--log.level ${cfg.logLevel} \
--web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
--config.file=${configFile} \
--log.format=${cfg.logFormat} \
--log.level=${cfg.logLevel} \
--web.listen-address=${cfg.listenAddress}:${toString cfg.port} \
${concatStringsSep " \\\n " cfg.extraFlags}
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.surfboard;
in
{
let
cfg = config.services.prometheus.exporters.surfboard;
in
{
port = 9239;
extraOpts = {
modemAddress = mkOption {
@ -30,4 +29,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.tor;
in
{
let
cfg = config.services.prometheus.exporters.tor;
in
{
port = 9130;
extraOpts = {
torControlAddress = mkOption {
@ -43,4 +42,4 @@ baseCfg:
# dummy $HOME. https://bugs.python.org/issue10496
environment = { HOME = "/var/empty"; };
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.unifi;
in
{
let
cfg = config.services.prometheus.exporters.unifi;
in
{
port = 9130;
extraOpts = {
unifiAddress = mkOption {
@ -65,4 +64,4 @@ baseCfg:
'';
};
};
}
}

View File

@ -2,11 +2,10 @@
with lib;
baseCfg:
let
cfg = baseCfg.varnish;
in
{
let
cfg = config.services.prometheus.exporters.varnish;
in
{
port = 9131;
extraOpts = {
noExit = mkOption {
@ -86,4 +85,4 @@ baseCfg:
'';
};
};
}
}