Merge pull request #49756 from andir/prometheus
nixos/prometheus: check alertmanager configuration
This commit is contained in:
commit
b4d9f14d3c
@ -5,10 +5,18 @@ with lib;
|
|||||||
let
|
let
|
||||||
cfg = config.services.prometheus.alertmanager;
|
cfg = config.services.prometheus.alertmanager;
|
||||||
mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
|
mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
|
||||||
alertmanagerYml =
|
|
||||||
if cfg.configText != null then
|
checkedConfig = file: pkgs.runCommand "checked-config" { buildInputs = [ cfg.package ]; } ''
|
||||||
|
ln -s ${file} $out
|
||||||
|
amtool check-config $out
|
||||||
|
'';
|
||||||
|
|
||||||
|
alertmanagerYml = let
|
||||||
|
yml = if cfg.configText != null then
|
||||||
pkgs.writeText "alertmanager.yml" cfg.configText
|
pkgs.writeText "alertmanager.yml" cfg.configText
|
||||||
else mkConfigFile;
|
else mkConfigFile;
|
||||||
|
in checkedConfig yml;
|
||||||
|
|
||||||
cmdlineArgs = cfg.extraFlags ++ [
|
cmdlineArgs = cfg.extraFlags ++ [
|
||||||
"--config.file ${alertmanagerYml}"
|
"--config.file ${alertmanagerYml}"
|
||||||
"--web.listen-address ${cfg.listenAddress}:${toString cfg.port}"
|
"--web.listen-address ${cfg.listenAddress}:${toString cfg.port}"
|
||||||
@ -23,6 +31,15 @@ in {
|
|||||||
services.prometheus.alertmanager = {
|
services.prometheus.alertmanager = {
|
||||||
enable = mkEnableOption "Prometheus Alertmanager";
|
enable = mkEnableOption "Prometheus Alertmanager";
|
||||||
|
|
||||||
|
package = mkOption {
|
||||||
|
type = types.package;
|
||||||
|
default = pkgs.prometheus-alertmanager;
|
||||||
|
defaultText = "pkgs.alertmanager";
|
||||||
|
description = ''
|
||||||
|
Package that should be used for alertmanager.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
user = mkOption {
|
user = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = "nobody";
|
default = "nobody";
|
||||||
@ -40,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.
|
||||||
'';
|
'';
|
||||||
@ -119,15 +136,22 @@ in {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
config = mkMerge [
|
||||||
config = mkIf cfg.enable {
|
(mkIf cfg.enable {
|
||||||
|
assertions = singleton {
|
||||||
|
assertion = cfg.configuration != null || cfg.configText != null;
|
||||||
|
message = "Can not enable alertmanager without a configuration. "
|
||||||
|
+ "Set either the `configuration` or `configText` attribute.";
|
||||||
|
};
|
||||||
|
})
|
||||||
|
(mkIf cfg.enable {
|
||||||
networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
|
networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
|
||||||
|
|
||||||
systemd.services.alertmanager = {
|
systemd.services.alertmanager = {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
after = [ "network.target" ];
|
after = [ "network.target" ];
|
||||||
script = ''
|
script = ''
|
||||||
${pkgs.prometheus-alertmanager.bin}/bin/alertmanager \
|
${cfg.package}/bin/alertmanager \
|
||||||
${concatStringsSep " \\\n " cmdlineArgs}
|
${concatStringsSep " \\\n " cmdlineArgs}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
@ -140,5 +164,6 @@ in {
|
|||||||
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
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