/
telegraf.nix
71 lines (63 loc) · 1.88 KB
/
telegraf.nix
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 (lib.mdDoc "telegraf agent");
package = mkOption {
default = pkgs.telegraf;
defaultText = lib.literalExpression "pkgs.telegraf";
description = lib.mdDoc "Which telegraf derivation to use";
type = types.package;
};
environmentFiles = mkOption {
type = types.listOf types.path;
default = [ ];
example = [ "/run/keys/telegraf.env" ];
description = lib.mdDoc ''
File to load as environment file.
This is useful to avoid putting secrets into the nix store.
'';
};
extraConfig = mkOption {
default = { };
description = lib.mdDoc "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 = lib.mdDoc "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;
};
};
};
}