summaryrefslogtreecommitdiff
path: root/modules/services/monitoring/telegraf.nix
blob: f40e01302537b6185c905c0c72fbae6bf950e469 (plain)
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
{ config, lib, pkgs, ... }:

let
  inherit (lib) mkEnableOption mkOption types mkIf;

  cfg = config.services.telegraf;

  settingsFormat = pkgs.formats.toml { };
  configFile = settingsFormat.generate "config.toml" cfg.extraConfig;
in {
  options = {
    services.telegraf = {
      enable = mkEnableOption "telegraf agent";

      package = mkOption {
        default = pkgs.telegraf;
        defaultText = lib.literalExpression "pkgs.telegraf";
        description = "Which telegraf derivation to use";
        type = types.package;
      };

      environmentFiles = mkOption {
        type = types.listOf types.path;
        default = [ ];
        example = [ "/run/keys/telegraf.env" ];
        description = ''
          File to load as environment file.
          This is useful to avoid putting secrets into the nix store.
        '';
      };

      extraConfig = mkOption {
        default = { };
        description = "Extra configuration options for telegraf";
        type = settingsFormat.type;
        example = {
          outputs.influxdb = {
            urls = [ "http://localhost:8086" ];
            database = "telegraf";
          };
          inputs.statsd = {
            service_address = ":8125";
            delete_timings = true;
          };
        };
      };

      configUrl = mkOption {
        default = null;
        description = "Url to fetch config from";
        type = types.nullOr types.str;
      };
    };
  };

  config = mkIf cfg.enable {
    launchd.daemons.telegraf = {
      script = ''
        ${lib.concatStringsSep "\n"
        (map (file: "source ${file}") cfg.environmentFiles)}
        ${cfg.package}/bin/telegraf --config ${
          if cfg.configUrl == null then configFile else cfg.configUrl
        }
      '';
      serviceConfig = {
        KeepAlive = true;
        RunAtLoad = true;
      };
    };
  };
}