1{ config, lib, pkgs, options }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.exporters.postfix;
7in
8{
9 port = 9154;
10 extraOpts = {
11 group = mkOption {
12 type = types.str;
13 description = lib.mdDoc ''
14 Group under which the postfix exporter shall be run.
15 It should match the group that is allowed to access the
16 `showq` socket in the `queue/public/` directory.
17 Defaults to `services.postfix.setgidGroup` when postfix is enabled.
18 '';
19 };
20 telemetryPath = mkOption {
21 type = types.str;
22 default = "/metrics";
23 description = lib.mdDoc ''
24 Path under which to expose metrics.
25 '';
26 };
27 logfilePath = mkOption {
28 type = types.path;
29 default = "/var/log/postfix_exporter_input.log";
30 example = "/var/log/mail.log";
31 description = lib.mdDoc ''
32 Path where Postfix writes log entries.
33 This file will be truncated by this exporter!
34 '';
35 };
36 showqPath = mkOption {
37 type = types.path;
38 default = "/var/lib/postfix/queue/public/showq";
39 example = "/var/spool/postfix/public/showq";
40 description = lib.mdDoc ''
41 Path where Postfix places its showq socket.
42 '';
43 };
44 systemd = {
45 enable = mkOption {
46 type = types.bool;
47 default = true;
48 description = lib.mdDoc ''
49 Whether to enable reading metrics from the systemd journal instead of from a logfile
50 '';
51 };
52 unit = mkOption {
53 type = types.str;
54 default = "postfix.service";
55 description = lib.mdDoc ''
56 Name of the postfix systemd unit.
57 '';
58 };
59 slice = mkOption {
60 type = types.nullOr types.str;
61 default = null;
62 description = lib.mdDoc ''
63 Name of the postfix systemd slice.
64 This overrides the {option}`systemd.unit`.
65 '';
66 };
67 journalPath = mkOption {
68 type = types.nullOr types.path;
69 default = null;
70 description = lib.mdDoc ''
71 Path to the systemd journal.
72 '';
73 };
74 };
75 };
76 serviceOpts = {
77 after = mkIf cfg.systemd.enable [ cfg.systemd.unit ];
78 serviceConfig = {
79 DynamicUser = false;
80 # By default, each prometheus exporter only gets AF_INET & AF_INET6,
81 # but AF_UNIX is needed to read from the `showq`-socket.
82 RestrictAddressFamilies = [ "AF_UNIX" ];
83 SupplementaryGroups = mkIf cfg.systemd.enable [ "systemd-journal" ];
84 ExecStart = ''
85 ${pkgs.prometheus-postfix-exporter}/bin/postfix_exporter \
86 --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
87 --web.telemetry-path ${cfg.telemetryPath} \
88 --postfix.showq_path ${escapeShellArg cfg.showqPath} \
89 ${concatStringsSep " \\\n " (cfg.extraFlags
90 ++ optional cfg.systemd.enable "--systemd.enable"
91 ++ optional cfg.systemd.enable (if cfg.systemd.slice != null
92 then "--systemd.slice ${cfg.systemd.slice}"
93 else "--systemd.unit ${cfg.systemd.unit}")
94 ++ optional (cfg.systemd.enable && (cfg.systemd.journalPath != null))
95 "--systemd.journal_path ${escapeShellArg cfg.systemd.journalPath}"
96 ++ optional (!cfg.systemd.enable) "--postfix.logfile_path ${escapeShellArg cfg.logfilePath}")}
97 '';
98 };
99 };
100}