nixos/prometheus: require one alertmanager configuration parameter
This commit adds an assertion that checks that either `configFile` or `configuration` is configured for alertmanager. The alertmanager config can not be an empty attributeset. The check executed with `amtool` fails before the service even has the chance to start. We should probably not allow a broken alertmanager configuration anyway. This also introduces a test for alertmanager configuration that piggy backs on the existing prometheus tests.
This commit is contained in:
parent
b1032db5a9
commit
51c3082119
@ -57,8 +57,8 @@ in {
|
|||||||
};
|
};
|
||||||
|
|
||||||
configuration = mkOption {
|
configuration = mkOption {
|
||||||
type = types.attrs;
|
type = types.nullOr types.attrs;
|
||||||
default = {};
|
default = null;
|
||||||
description = ''
|
description = ''
|
||||||
Alertmanager configuration as nix attribute set.
|
Alertmanager configuration as nix attribute set.
|
||||||
'';
|
'';
|
||||||
@ -136,26 +136,34 @@ in {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
config = mkMerge [
|
||||||
config = mkIf cfg.enable {
|
(mkIf cfg.enable {
|
||||||
networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
|
assertions = singleton {
|
||||||
|
assertion = cfg.configuration != null || cfg.configText != null;
|
||||||
systemd.services.alertmanager = {
|
message = "Can not enable alertmanager without a configuration. "
|
||||||
wantedBy = [ "multi-user.target" ];
|
+ "Set either the `configuration` or `configText` attribute.";
|
||||||
after = [ "network.target" ];
|
|
||||||
script = ''
|
|
||||||
${cfg.package}/bin/alertmanager \
|
|
||||||
${concatStringsSep " \\\n " cmdlineArgs}
|
|
||||||
'';
|
|
||||||
|
|
||||||
serviceConfig = {
|
|
||||||
User = cfg.user;
|
|
||||||
Group = cfg.group;
|
|
||||||
Restart = "always";
|
|
||||||
PrivateTmp = true;
|
|
||||||
WorkingDirectory = "/tmp";
|
|
||||||
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
||||||
};
|
};
|
||||||
};
|
})
|
||||||
};
|
(mkIf cfg.enable {
|
||||||
|
networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
|
||||||
|
|
||||||
|
systemd.services.alertmanager = {
|
||||||
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
after = [ "network.target" ];
|
||||||
|
script = ''
|
||||||
|
${cfg.package}/bin/alertmanager \
|
||||||
|
${concatStringsSep " \\\n " cmdlineArgs}
|
||||||
|
'';
|
||||||
|
|
||||||
|
serviceConfig = {
|
||||||
|
User = cfg.user;
|
||||||
|
Group = cfg.group;
|
||||||
|
Restart = "always";
|
||||||
|
PrivateTmp = true;
|
||||||
|
WorkingDirectory = "/tmp";
|
||||||
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
})
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,25 @@ import ./make-test.nix {
|
|||||||
}];
|
}];
|
||||||
}];
|
}];
|
||||||
rules = [ ''testrule = count(up{job="prometheus"})'' ];
|
rules = [ ''testrule = count(up{job="prometheus"})'' ];
|
||||||
|
|
||||||
|
# a very simple version of the alertmanager configuration just to see if
|
||||||
|
# configuration checks & service startup are working
|
||||||
|
alertmanager = {
|
||||||
|
enable = true;
|
||||||
|
listenAddress = "[::1]";
|
||||||
|
port = 9093;
|
||||||
|
configuration = {
|
||||||
|
route.receiver = "webhook";
|
||||||
|
receivers = [
|
||||||
|
{
|
||||||
|
name = "webhook";
|
||||||
|
webhook_configs = [
|
||||||
|
{ url = "http://localhost"; }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -22,5 +41,8 @@ import ./make-test.nix {
|
|||||||
$one->waitForUnit("prometheus.service");
|
$one->waitForUnit("prometheus.service");
|
||||||
$one->waitForOpenPort(9090);
|
$one->waitForOpenPort(9090);
|
||||||
$one->succeed("curl -s http://127.0.0.1:9090/metrics");
|
$one->succeed("curl -s http://127.0.0.1:9090/metrics");
|
||||||
|
$one->waitForUnit("alertmanager.service");
|
||||||
|
$one->waitForOpenPort("9093");
|
||||||
|
$one->succeed("curl -f -s http://localhost:9093/");
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user