summary refs log tree commit diff
path: root/nixos/modules/services/networking/mtr-exporter.nix
blob: ca261074ebde758b18bd506f13c2f96bf595dbad (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{ config, lib, pkgs, ... }:

let
  inherit (lib)
    maintainers types mkEnableOption mkOption mkIf
    literalExpression escapeShellArg escapeShellArgs;
  cfg = config.services.mtr-exporter;
in {
  options = {
    services = {
      mtr-exporter = {
        enable = mkEnableOption "a Prometheus exporter for MTR";

        target = mkOption {
          type = types.str;
          example = "example.org";
          description = "Target to check using MTR.";
        };

        interval = mkOption {
          type = types.int;
          default = 60;
          description = "Interval between MTR checks in seconds.";
        };

        port = mkOption {
          type = types.port;
          default = 8080;
          description = "Listen port for MTR exporter.";
        };

        address = mkOption {
          type = types.str;
          default = "127.0.0.1";
          description = "Listen address for MTR exporter.";
        };

        mtrFlags = mkOption {
          type = with types; listOf str;
          default = [];
          example = ["-G1"];
          description = "Additional flags to pass to MTR.";
        };
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services.mtr-exporter = {
      script = ''
        exec ${pkgs.mtr-exporter}/bin/mtr-exporter \
          -mtr ${pkgs.mtr}/bin/mtr \
          -schedule '@every ${toString cfg.interval}s' \
          -bind ${escapeShellArg cfg.address}:${toString cfg.port} \
          -- \
          ${escapeShellArgs (cfg.mtrFlags ++ [ cfg.target ])}
      '';
      wantedBy = [ "multi-user.target" ];
      requires = [ "network.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        Restart = "on-failure";
        # Hardening
        CapabilityBoundingSet = [ "" ];
        DynamicUser = true;
        LockPersonality = true;
        ProcSubset = "pid";
        PrivateDevices = true;
        PrivateUsers = true;
        PrivateTmp = true;
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        RestrictNamespaces = true;
        RestrictRealtime = true;
      };
    };
  };

  meta.maintainers = with maintainers; [ jakubgs ];
}