2016-09-19 18:09:26 +02:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.prometheus.alertmanager;
|
2019-02-21 15:29:54 +01:00
|
|
|
cfg2 = config.services.prometheus2.alertmanager;
|
|
|
|
mkConfigFile = amCfg:
|
|
|
|
pkgs.writeText "alertmanager.yml" (builtins.toJSON amCfg.configuration);
|
|
|
|
|
|
|
|
mkAlertmanagerYml = amCfg: let
|
|
|
|
checkedConfig = file:
|
|
|
|
pkgs.runCommand "checked-config" { buildInputs = [ amCfg.package ]; } ''
|
|
|
|
ln -s ${file} $out
|
|
|
|
amtool check-config $out
|
|
|
|
'';
|
|
|
|
yml = if amCfg.configText != null then
|
|
|
|
pkgs.writeText "alertmanager.yml" amCfg.configText
|
|
|
|
else mkConfigFile amCfg;
|
|
|
|
in
|
|
|
|
checkedConfig yml;
|
|
|
|
|
|
|
|
mkCmdlineArgs = amCfg:
|
|
|
|
amCfg.extraFlags ++ [
|
|
|
|
"--config.file ${mkAlertmanagerYml amCfg}"
|
|
|
|
"--web.listen-address ${amCfg.listenAddress}:${toString amCfg.port}"
|
|
|
|
"--log.level ${amCfg.logLevel}"
|
|
|
|
] ++ (optional (amCfg.webExternalUrl != null)
|
|
|
|
"--web.external-url ${amCfg.webExternalUrl}"
|
|
|
|
) ++ (optional (amCfg.logFormat != null)
|
|
|
|
"--log.format ${amCfg.logFormat}"
|
|
|
|
);
|
|
|
|
amOptions = {
|
2016-09-19 18:09:26 +02:00
|
|
|
enable = mkEnableOption "Prometheus Alertmanager";
|
|
|
|
|
2018-11-04 16:57:47 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.prometheus-alertmanager;
|
|
|
|
defaultText = "pkgs.alertmanager";
|
|
|
|
description = ''
|
|
|
|
Package that should be used for alertmanager.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-19 18:09:26 +02:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "nobody";
|
|
|
|
description = ''
|
|
|
|
User name under which Alertmanager shall be run.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "nogroup";
|
|
|
|
description = ''
|
|
|
|
Group under which Alertmanager shall be run.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configuration = mkOption {
|
2018-11-04 19:27:43 +01:00
|
|
|
type = types.nullOr types.attrs;
|
|
|
|
default = null;
|
2016-09-19 18:09:26 +02:00
|
|
|
description = ''
|
|
|
|
Alertmanager configuration as nix attribute set.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-12-22 23:18:39 +01: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 18:09:26 +02: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 23:00:15 +01:00
|
|
|
type = types.str;
|
|
|
|
default = "";
|
2016-09-19 18:09:26 +02:00
|
|
|
description = ''
|
2019-02-27 00:59:11 +11: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 18:09:26 +02: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 23:19:26 +02:00
|
|
|
|
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Extra commandline options when launching the Alertmanager.
|
|
|
|
'';
|
|
|
|
};
|
2016-09-19 18:09:26 +02:00
|
|
|
};
|
2019-02-21 16:32:46 +01:00
|
|
|
mkAMConfig = amCfg: amVersion: [
|
|
|
|
(mkIf amCfg.enable {
|
|
|
|
assertions = singleton {
|
|
|
|
assertion = amCfg.configuration != null || amCfg.configText != null;
|
|
|
|
message = "Can not enable alertmanager without a configuration. "
|
|
|
|
+ "Set either the `configuration` or `configText` attribute.";
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(mkIf amCfg.enable {
|
|
|
|
networking.firewall.allowedTCPPorts = optional amCfg.openFirewall amCfg.port;
|
|
|
|
|
|
|
|
systemd.services."alertmanager${amVersion}" = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
script = ''
|
|
|
|
${amCfg.package}/bin/alertmanager \
|
|
|
|
${concatStringsSep " \\\n " cmdlineArgs}
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
User = amCfg.user;
|
|
|
|
Group = amCfg.group;
|
|
|
|
Restart = "always";
|
|
|
|
PrivateTmp = true;
|
|
|
|
WorkingDirectory = "/tmp";
|
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2019-02-21 15:29:54 +01:00
|
|
|
};
|
2019-02-21 16:32:46 +01:00
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
2019-02-21 15:29:54 +01:00
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
services.prometheus.alertmanager = amOptions;
|
|
|
|
services.prometheus2.alertmanager = amOptions;
|
2016-09-19 18:09:26 +02:00
|
|
|
};
|
|
|
|
|
2019-02-21 16:32:46 +01:00
|
|
|
config = mkMerge ((mkAMConfig cfg "") ++ (mkAMConfig cfg2 "2"));
|
2016-09-19 18:09:26 +02:00
|
|
|
}
|