Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/nixpkgs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6845ebbff1c4
Choose a base ref
...
head repository: NixOS/nixpkgs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b4d9f14d3cc4
Choose a head ref
  • 4 commits
  • 2 files changed
  • 1 contributor

Commits on Nov 23, 2018

  1. Copy the full SHA
    d1ef00e View commit details
  2. Copy the full SHA
    b1032db View commit details
  3. 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.
    andir committed Nov 23, 2018
    Copy the full SHA
    51c3082 View commit details
  4. Merge pull request #49756 from andir/prometheus

    nixos/prometheus: check alertmanager configuration
    andir authored Nov 23, 2018
    Copy the full SHA
    b4d9f14 View commit details
Showing with 73 additions and 26 deletions.
  1. +51 −26 nixos/modules/services/monitoring/prometheus/alertmanager.nix
  2. +22 −0 nixos/tests/prometheus.nix
77 changes: 51 additions & 26 deletions nixos/modules/services/monitoring/prometheus/alertmanager.nix
Original file line number Diff line number Diff line change
@@ -5,10 +5,18 @@ with lib;
let
cfg = config.services.prometheus.alertmanager;
mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
alertmanagerYml =
if cfg.configText != null then
pkgs.writeText "alertmanager.yml" cfg.configText
else mkConfigFile;

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
else mkConfigFile;
in checkedConfig yml;

cmdlineArgs = cfg.extraFlags ++ [
"--config.file ${alertmanagerYml}"
"--web.listen-address ${cfg.listenAddress}:${toString cfg.port}"
@@ -23,6 +31,15 @@ in {
services.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 {
type = types.str;
default = "nobody";
@@ -40,8 +57,8 @@ in {
};

configuration = mkOption {
type = types.attrs;
default = {};
type = types.nullOr types.attrs;
default = null;
description = ''
Alertmanager configuration as nix attribute set.
'';
@@ -119,26 +136,34 @@ in {
};
};

config = mkMerge [
(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;

systemd.services.alertmanager = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
script = ''
${cfg.package}/bin/alertmanager \
${concatStringsSep " \\\n " cmdlineArgs}
'';

config = mkIf cfg.enable {
networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;

systemd.services.alertmanager = {
wantedBy = [ "multi-user.target" ];
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";
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Restart = "always";
PrivateTmp = true;
WorkingDirectory = "/tmp";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
};
};
};
};
})
];
}
22 changes: 22 additions & 0 deletions nixos/tests/prometheus.nix
Original file line number Diff line number Diff line change
@@ -13,6 +13,25 @@ import ./make-test.nix {
}];
}];
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->waitForOpenPort(9090);
$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/");
'';
}