2016-09-19 17:09:26 +01:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.prometheus.alertmanager;
|
|
|
|
mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
|
2018-11-04 15:58:13 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-09-04 22:19:26 +01:00
|
|
|
cmdlineArgs = cfg.extraFlags ++ [
|
2020-01-23 12:51:00 +00:00
|
|
|
"--config.file /tmp/alert-manager-substituted.yaml"
|
2018-09-04 22:19:26 +01:00
|
|
|
"--web.listen-address ${cfg.listenAddress}:${toString cfg.port}"
|
|
|
|
"--log.level ${cfg.logLevel}"
|
2020-04-13 17:39:51 +01:00
|
|
|
"--storage.path /var/lib/alertmanager"
|
|
|
|
(toString (map (peer: "--cluster.peer ${peer}:9094") cfg.clusterPeers))
|
2018-09-04 22:19:26 +01:00
|
|
|
] ++ (optional (cfg.webExternalUrl != null)
|
|
|
|
"--web.external-url ${cfg.webExternalUrl}"
|
|
|
|
) ++ (optional (cfg.logFormat != null)
|
|
|
|
"--log.format ${cfg.logFormat}"
|
|
|
|
);
|
2016-09-19 17:09:26 +01:00
|
|
|
in {
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "user" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a user setting.")
|
|
|
|
(mkRemovedOptionModule [ "services" "prometheus" "alertmanager" "group" ] "The alertmanager service is now using systemd's DynamicUser mechanism which obviates a group setting.")
|
|
|
|
(mkRemovedOptionModule [ "services" "prometheus" "alertmanagerURL" ] ''
|
|
|
|
Due to incompatibility, the alertmanagerURL option has been removed,
|
|
|
|
please use 'services.prometheus2.alertmanagers' instead.
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
2016-09-19 17:09:26 +01:00
|
|
|
options = {
|
|
|
|
services.prometheus.alertmanager = {
|
|
|
|
enable = mkEnableOption "Prometheus Alertmanager";
|
|
|
|
|
2018-11-04 15:57:47 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.prometheus-alertmanager;
|
|
|
|
defaultText = "pkgs.alertmanager";
|
|
|
|
description = ''
|
|
|
|
Package that should be used for alertmanager.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-19 17:09:26 +01:00
|
|
|
configuration = mkOption {
|
2018-11-04 18:27:43 +00:00
|
|
|
type = types.nullOr types.attrs;
|
|
|
|
default = null;
|
2016-09-19 17:09:26 +01:00
|
|
|
description = ''
|
|
|
|
Alertmanager configuration as nix attribute set.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-12-22 22:18:39 +00:00
|
|
|
configText = mkOption {
|
|
|
|
type = types.nullOr types.lines;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Alertmanager configuration as YAML text. If non-null, this option
|
|
|
|
defines the text that is written to alertmanager.yml. If null, the
|
|
|
|
contents of alertmanager.yml is generated from the structured config
|
|
|
|
options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-19 17:09:26 +01:00
|
|
|
logFormat = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
If set use a syslog logger or JSON logging.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
logLevel = mkOption {
|
|
|
|
type = types.enum ["debug" "info" "warn" "error" "fatal"];
|
|
|
|
default = "warn";
|
|
|
|
description = ''
|
|
|
|
Only log messages with the given severity or above.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
webExternalUrl = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy).
|
|
|
|
Used for generating relative and absolute links back to Alertmanager itself.
|
|
|
|
If the URL has a path portion, it will be used to prefix all HTTP endoints served by Alertmanager.
|
|
|
|
If omitted, relevant URL components will be derived automatically.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
listenAddress = mkOption {
|
2016-12-27 22:00:15 +00:00
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2016-09-19 17:09:26 +01:00
|
|
|
description = ''
|
2019-02-26 13:59:11 +00:00
|
|
|
Address to listen on for the web interface and API. Empty string will listen on all interfaces.
|
|
|
|
"localhost" will listen on 127.0.0.1 (but not ::1).
|
2016-09-19 17:09:26 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 9093;
|
|
|
|
description = ''
|
|
|
|
Port to listen on for the web interface and API.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Open port in firewall for incoming connections.
|
|
|
|
'';
|
|
|
|
};
|
2018-09-04 22:19:26 +01:00
|
|
|
|
2020-04-13 17:39:51 +01:00
|
|
|
clusterPeers = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Initial peers for HA cluster.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-09-04 22:19:26 +01:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Extra commandline options when launching the Alertmanager.
|
|
|
|
'';
|
|
|
|
};
|
2020-01-23 12:51:00 +00:00
|
|
|
|
|
|
|
environmentFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/root/alertmanager.env";
|
|
|
|
description = ''
|
2020-01-24 17:07:24 +00:00
|
|
|
File to load as environment file. Environment variables
|
|
|
|
from this file will be interpolated into the config file
|
|
|
|
using envsubst with this syntax:
|
|
|
|
<literal>$ENVIRONMENT ''${VARIABLE}</literal>
|
2020-01-23 12:51:00 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-09-19 17:09:26 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-11-04 18:27:43 +00:00
|
|
|
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" ];
|
2020-03-17 21:12:48 +00:00
|
|
|
after = [ "network-online.target" ];
|
2020-01-23 12:51:00 +00:00
|
|
|
preStart = ''
|
2020-01-27 23:32:11 +00:00
|
|
|
${lib.getBin pkgs.envsubst}/bin/envsubst -o "/tmp/alert-manager-substituted.yaml" \
|
|
|
|
-i "${alertmanagerYml}"
|
2020-01-23 12:51:00 +00:00
|
|
|
'';
|
2018-11-04 18:27:43 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Restart = "always";
|
2020-04-13 17:39:51 +01:00
|
|
|
StateDirectory = "alertmanager";
|
2020-01-23 12:51:00 +00:00
|
|
|
DynamicUser = true; # implies PrivateTmp
|
|
|
|
EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
|
2018-11-04 18:27:43 +00:00
|
|
|
WorkingDirectory = "/tmp";
|
2019-04-10 14:03:09 +01:00
|
|
|
ExecStart = "${cfg.package}/bin/alertmanager" +
|
|
|
|
optionalString (length cmdlineArgs != 0) (" \\\n " +
|
|
|
|
concatStringsSep " \\\n " cmdlineArgs);
|
2018-11-04 18:27:43 +00:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
};
|
2016-09-19 17:09:26 +01:00
|
|
|
};
|
2018-11-04 18:27:43 +00:00
|
|
|
})
|
|
|
|
];
|
2016-09-19 17:09:26 +01:00
|
|
|
}
|