summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/prometheus/exporters/script.nix
blob: a805a0ad335d2a4271ac4a90720eac9bb32b8164 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{ config, lib, pkgs, options }:

with lib;

let
  cfg = config.services.prometheus.exporters.script;
  configFile = pkgs.writeText "script-exporter.yaml" (builtins.toJSON cfg.settings);
in
{
  port = 9172;
  extraOpts = {
    settings.scripts = mkOption {
      type = with types; listOf (submodule {
        options = {
          name = mkOption {
            type = str;
            example = "sleep";
            description = "Name of the script.";
          };
          script = mkOption {
            type = str;
            example = "sleep 5";
            description = "Shell script to execute when metrics are requested.";
          };
          timeout = mkOption {
            type = nullOr int;
            default = null;
            example = 60;
            description = "Optional timeout for the script in seconds.";
          };
        };
      });
      example = literalExpression ''
        {
          scripts = [
            { name = "sleep"; script = "sleep 5"; }
          ];
        }
      '';
      description = ''
        All settings expressed as an Nix attrset.

        Check the official documentation for the corresponding YAML
        settings that can all be used here: <link xlink:href="https://github.com/adhocteam/script_exporter#sample-configuration" />
      '';
    };
  };
  serviceOpts = {
    serviceConfig = {
      ExecStart = ''
        ${pkgs.prometheus-script-exporter}/bin/script_exporter \
          --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
          --config.file ${configFile} \
          ${concatStringsSep " \\\n  " cfg.extraFlags}
      '';
      NoNewPrivileges = true;
      ProtectHome = true;
      ProtectSystem = "strict";
      ProtectKernelTunables = true;
      ProtectKernelModules = true;
      ProtectControlGroups = true;
    };
  };
}