@@ -0,0 +1,166 @@
1+{ config, pkgs, lib, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.prometheus.pushgateway;
7+
8+ cmdlineArgs =
9+ opt "web.listen-address" cfg.web.listen-address
10+ ++ opt "web.telemetry-path" cfg.web.telemetry-path
11+ ++ opt "web.external-url" cfg.web.external-url
12+ ++ opt "web.route-prefix" cfg.web.route-prefix
13+ ++ optional cfg.persistMetrics ''--persistence.file="/var/lib/${cfg.stateDir}/metrics"''
14+ ++ opt "persistence.interval" cfg.persistence.interval
15+ ++ opt "log.level" cfg.log.level
16+ ++ opt "log.format" cfg.log.format
17+ ++ cfg.extraFlags;
18+
19+ opt = k : v : optional (v != null) ''--${k}="${v}"'';
20+
21+in {
22+ options = {
23+ services.prometheus.pushgateway = {
24+ enable = mkEnableOption "Prometheus Pushgateway";
25+
26+ package = mkOption {
27+ type = types.package;
28+ default = pkgs.prometheus-pushgateway;
29+ defaultText = "pkgs.prometheus-pushgateway";
30+ description = ''
31+ Package that should be used for the prometheus pushgateway.
32+ '';
33+ };
34+
35+ web.listen-address = mkOption {
36+ type = types.nullOr types.str;
37+ default = null;
38+ description = ''
39+ Address to listen on for the web interface, API and telemetry.
40+
41+ <literal>null</literal> will default to <literal>:9091</literal>.
42+ '';
43+ };
44+
45+ web.telemetry-path = mkOption {
46+ type = types.nullOr types.str;
47+ default = null;
48+ description = ''
49+ Path under which to expose metrics.
50+
51+ <literal>null</literal> will default to <literal>/metrics</literal>.
52+ '';
53+ };
54+
55+ web.external-url = mkOption {
56+ type = types.nullOr types.str;
57+ default = null;
58+ description = ''
59+ The URL under which Pushgateway is externally reachable.
60+ '';
61+ };
62+
63+ web.route-prefix = mkOption {
64+ type = types.nullOr types.str;
65+ default = null;
66+ description = ''
67+ Prefix for the internal routes of web endpoints.
68+
69+ Defaults to the path of
70+ <option>services.prometheus.pushgateway.web.external-url</option>.
71+ '';
72+ };
73+
74+ persistence.interval = mkOption {
75+ type = types.nullOr types.str;
76+ default = null;
77+ example = "10m";
78+ description = ''
79+ The minimum interval at which to write out the persistence file.
80+
81+ <literal>null</literal> will default to <literal>5m</literal>.
82+ '';
83+ };
84+
85+ log.level = mkOption {
86+ type = types.nullOr (types.enum ["debug" "info" "warn" "error" "fatal"]);
87+ default = null;
88+ description = ''
89+ Only log messages with the given severity or above.
90+
91+ <literal>null</literal> will default to <literal>info</literal>.
92+ '';
93+ };
94+
95+ log.format = mkOption {
96+ type = types.nullOr types.str;
97+ default = null;
98+ example = "logger:syslog?appname=bob&local=7";
99+ description = ''
100+ Set the log target and format.
101+
102+ <literal>null</literal> will default to <literal>logger:stderr</literal>.
103+ '';
104+ };
105+
106+ extraFlags = mkOption {
107+ type = types.listOf types.str;
108+ default = [];
109+ description = ''
110+ Extra commandline options when launching the Pushgateway.
111+ '';
112+ };
113+
114+ persistMetrics = mkOption {
115+ type = types.bool;
116+ default = false;
117+ description = ''
118+ Whether to persist metrics to a file.
119+
120+ When enabled metrics will be saved to a file called
121+ <literal>metrics</literal> in the directory
122+ <literal>/var/lib/pushgateway</literal>. The directory below
123+ <literal>/var/lib</literal> can be set using
124+ <option>services.prometheus.pushgateway.stateDir</option>.
125+ '';
126+ };
127+
128+ stateDir = mkOption {
129+ type = types.str;
130+ default = "pushgateway";
131+ description = ''
132+ Directory below <literal>/var/lib</literal> to store metrics.
133+
134+ This directory will be created automatically using systemd's
135+ StateDirectory mechanism when
136+ <option>services.prometheus.pushgateway.persistMetrics</option>
137+ is enabled.
138+ '';
139+ };
140+ };
141+ };
142+
143+ config = mkIf cfg.enable {
144+ assertions = [
145+ {
146+ assertion = !hasPrefix "/" cfg.stateDir;
147+ message =
148+ "The option services.prometheus.pushgateway.stateDir" +
149+ " shouldn't be an absolute directory." +
150+ " It should be a directory relative to /var/lib.";
151+ }
152+ ];
153+ systemd.services.pushgateway = {
154+ wantedBy = [ "multi-user.target" ];
155+ after = [ "network.target" ];
156+ serviceConfig = {
157+ Restart = "always";
158+ DynamicUser = true;
159+ ExecStart = "${cfg.package}/bin/pushgateway" +
160+ optionalString (length cmdlineArgs != 0) (" \\\n " +
161+ concatStringsSep " \\\n " cmdlineArgs);
162+ StateDirectory = if cfg.persistMetrics then cfg.stateDir else null;
163+ };
164+ };
165+ };
166+}