Merge pull request #49756 from andir/prometheus

nixos/prometheus: check alertmanager configuration
This commit is contained in:
Andreas Rammhold 2018-11-23 19:55:30 +01:00 committed by GitHub
commit b4d9f14d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 27 deletions

View File

@ -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 ]; } ''
pkgs.writeText "alertmanager.yml" cfg.configText ln -s ${file} $out
else mkConfigFile; amtool check-config $out
'';
alertmanagerYml = let
yml = if cfg.configText != null then
pkgs.writeText "alertmanager.yml" cfg.configText
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,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 = ''
${pkgs.prometheus-alertmanager.bin}/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";
};
};
})
];
} }

View File

@ -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/");
''; '';
} }