1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.alertmanager;
7 mkConfigFile = pkgs.writeText "alertmanager.yml" (builtins.toJSON cfg.configuration);
8 alertmanagerYml =
9 if cfg.configText != null then
10 pkgs.writeText "alertmanager.yml" cfg.configText
11 else mkConfigFile;
12in {
13 options = {
14 services.prometheus.alertmanager = {
15 enable = mkEnableOption "Prometheus Alertmanager";
16
17 user = mkOption {
18 type = types.str;
19 default = "nobody";
20 description = ''
21 User name under which Alertmanager shall be run.
22 '';
23 };
24
25 group = mkOption {
26 type = types.str;
27 default = "nogroup";
28 description = ''
29 Group under which Alertmanager shall be run.
30 '';
31 };
32
33 configuration = mkOption {
34 type = types.attrs;
35 default = {};
36 description = ''
37 Alertmanager configuration as nix attribute set.
38 '';
39 };
40
41 configText = mkOption {
42 type = types.nullOr types.lines;
43 default = null;
44 description = ''
45 Alertmanager configuration as YAML text. If non-null, this option
46 defines the text that is written to alertmanager.yml. If null, the
47 contents of alertmanager.yml is generated from the structured config
48 options.
49 '';
50 };
51
52 logFormat = mkOption {
53 type = types.nullOr types.str;
54 default = null;
55 description = ''
56 If set use a syslog logger or JSON logging.
57 '';
58 };
59
60 logLevel = mkOption {
61 type = types.enum ["debug" "info" "warn" "error" "fatal"];
62 default = "warn";
63 description = ''
64 Only log messages with the given severity or above.
65 '';
66 };
67
68 webExternalUrl = mkOption {
69 type = types.nullOr types.str;
70 default = null;
71 description = ''
72 The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy).
73 Used for generating relative and absolute links back to Alertmanager itself.
74 If the URL has a path portion, it will be used to prefix all HTTP endoints served by Alertmanager.
75 If omitted, relevant URL components will be derived automatically.
76 '';
77 };
78
79 listenAddress = mkOption {
80 type = types.str;
81 default = "";
82 description = ''
83 Address to listen on for the web interface and API.
84 '';
85 };
86
87 port = mkOption {
88 type = types.int;
89 default = 9093;
90 description = ''
91 Port to listen on for the web interface and API.
92 '';
93 };
94
95 openFirewall = mkOption {
96 type = types.bool;
97 default = false;
98 description = ''
99 Open port in firewall for incoming connections.
100 '';
101 };
102 };
103 };
104
105
106 config = mkIf cfg.enable {
107 networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
108
109 systemd.services.alertmanager = {
110 wantedBy = [ "multi-user.target" ];
111 after = [ "network.target" ];
112 script = ''
113 ${pkgs.prometheus-alertmanager.bin}/bin/alertmanager \
114 -config.file ${alertmanagerYml} \
115 -web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
116 -log.level ${cfg.logLevel} \
117 ${optionalString (cfg.webExternalUrl != null) ''-web.external-url ${cfg.webExternalUrl} \''}
118 ${optionalString (cfg.logFormat != null) "-log.format ${cfg.logFormat}"}
119 '';
120
121 serviceConfig = {
122 User = cfg.user;
123 Group = cfg.group;
124 Restart = "always";
125 PrivateTmp = true;
126 WorkingDirectory = "/tmp";
127 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
128 };
129 };
130 };
131}