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,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.bind;
cfg = config.services.prometheus.exporters.bind;
in
{
port = 9119;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.blackbox;
cfg = config.services.prometheus.exporters.blackbox;
in
{
port = 9115;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.collectd;
cfg = config.services.prometheus.exporters.collectd;
in
{
port = 9103;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.dnsmasq;
cfg = config.services.prometheus.exporters.dnsmasq;
in
{
port = 9153;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.dovecot;
cfg = config.services.prometheus.exporters.dovecot;
in
{
port = 9166;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.fritzbox;
cfg = config.services.prometheus.exporters.fritzbox;
in
{
port = 9133;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.json;
cfg = config.services.prometheus.exporters.json;
in
{
port = 7979;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.minio;
cfg = config.services.prometheus.exporters.minio;
in
{
port = 9290;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.nginx;
cfg = config.services.prometheus.exporters.nginx;
in
{
port = 9113;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.node;
cfg = config.services.prometheus.exporters.node;
in
{
port = 9100;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.postfix;
cfg = config.services.prometheus.exporters.postfix;
in
{
port = 9154;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.snmp;
cfg = config.services.prometheus.exporters.snmp;
in
{
port = 9116;
@ -61,10 +60,10 @@ 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,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.surfboard;
cfg = config.services.prometheus.exporters.surfboard;
in
{
port = 9239;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.tor;
cfg = config.services.prometheus.exporters.tor;
in
{
port = 9130;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.unifi;
cfg = config.services.prometheus.exporters.unifi;
in
{
port = 9130;

View File

@ -2,9 +2,8 @@
with lib;
baseCfg:
let
cfg = baseCfg.varnish;
cfg = config.services.prometheus.exporters.varnish;
in
{
port = 9131;