1{ config, pkgs, lib, ... }:
2
3# Shamelessly cribbed from nginx-exporter.nix. ~ C.
4with lib;
5
6let
7 cfg = config.services.prometheus.varnishExporter;
8in {
9 options = {
10 services.prometheus.varnishExporter = {
11 enable = mkEnableOption "prometheus Varnish exporter";
12
13 port = mkOption {
14 type = types.int;
15 default = 9131;
16 description = ''
17 Port to listen on.
18 '';
19 };
20
21 extraFlags = mkOption {
22 type = types.listOf types.str;
23 default = [];
24 description = ''
25 Extra commandline options when launching the Varnish exporter.
26 '';
27 };
28
29 openFirewall = mkOption {
30 type = types.bool;
31 default = false;
32 description = ''
33 Open port in firewall for incoming connections.
34 '';
35 };
36 };
37 };
38
39 config = mkIf cfg.enable {
40 networking.firewall.allowedTCPPorts = optional cfg.openFirewall cfg.port;
41
42 systemd.services.prometheus-varnish-exporter = {
43 description = "Prometheus exporter for Varnish metrics";
44 unitConfig.Documentation = "https://github.com/jonnenauha/prometheus_varnish_exporter";
45 wantedBy = [ "multi-user.target" ];
46 path = [ pkgs.varnish ];
47 script = ''
48 exec ${pkgs.prometheus-varnish-exporter}/bin/prometheus_varnish_exporter \
49 -web.listen-address :${toString cfg.port} \
50 ${concatStringsSep " \\\n " cfg.extraFlags}
51 '';
52 serviceConfig = {
53 User = "nobody";
54 Restart = "always";
55 PrivateTmp = true;
56 WorkingDirectory = /tmp;
57 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
58 };
59 };
60 };
61}