about summary refs log tree commit diff
path: root/nixos/modules/services/networking/mtr-exporter.nix
blob: 38bc0401a7e6535f32b33f19dd118f1c99e51a9a (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{ config, lib, pkgs, ... }:

let
  inherit (lib)
    maintainers types literalExpression
    escapeShellArg escapeShellArgs
    mkEnableOption mkOption mkRemovedOptionModule mkIf mdDoc
    mkPackageOption optionalString concatMapStrings concatStringsSep;

  cfg = config.services.mtr-exporter;

  jobsConfig = pkgs.writeText "mtr-exporter.conf" (concatMapStrings (job: ''
    ${job.name} -- ${job.schedule} -- ${concatStringsSep " " job.flags} ${job.address}
  '') cfg.jobs);
in {
  imports = [
    (mkRemovedOptionModule [ "services" "mtr-exporter" "target" ] "Use services.mtr-exporter.jobs instead.")
    (mkRemovedOptionModule [ "services" "mtr-exporter" "mtrFlags" ] "Use services.mtr-exporter.jobs.<job>.flags instead.")
  ];

  options = {
    services = {
      mtr-exporter = {
        enable = mkEnableOption (mdDoc "a Prometheus exporter for MTR");

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

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

        extraFlags = mkOption {
          type = types.listOf types.str;
          default = [];
          example = ["-flag.deprecatedMetrics"];
          description = mdDoc ''
            Extra command line options to pass to MTR exporter.
          '';
        };

        package = mkPackageOption pkgs "mtr-exporter" { };

        mtrPackage = mkPackageOption pkgs "mtr" { };

        jobs = mkOption {
          description = mdDoc "List of MTR jobs. Will be added to /etc/mtr-exporter.conf";
          type = types.nonEmptyListOf (types.submodule {
            options = {
              name = mkOption {
                type = types.str;
                description = mdDoc "Name of ICMP pinging job.";
              };

              address = mkOption {
                type = types.str;
                example = "host.example.org:1234";
                description = mdDoc "Target address for MTR client.";
              };

              schedule = mkOption {
                type = types.str;
                default = "@every 60s";
                example = "@hourly";
                description = mdDoc "Schedule of MTR checks. Also accepts Cron format.";
              };

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

  config = mkIf cfg.enable {
    environment.etc."mtr-exporter.conf" = {
      source = jobsConfig;
    };

    systemd.services.mtr-exporter = {
      wantedBy = [ "multi-user.target" ];
      requires = [ "network.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ExecStart = ''
          ${cfg.package}/bin/mtr-exporter \
            -mtr '${cfg.mtrPackage}/bin/mtr' \
            -bind ${escapeShellArg "${cfg.address}:${toString cfg.port}"} \
            -jobs '${jobsConfig}' \
            ${escapeShellArgs cfg.extraFlags}
        '';
        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 ];
}