1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.telegraf;
7
8 configFile = pkgs.runCommand "config.toml" {
9 buildInputs = [ pkgs.remarshal ];
10 } ''
11 remarshal -if json -of toml \
12 < ${pkgs.writeText "config.json" (builtins.toJSON cfg.extraConfig)} \
13 > $out
14 '';
15in {
16 ###### interface
17 options = {
18 services.telegraf = {
19 enable = mkEnableOption "telegraf server";
20
21 package = mkOption {
22 default = pkgs.telegraf;
23 defaultText = "pkgs.telegraf";
24 description = "Which telegraf derivation to use";
25 type = types.package;
26 };
27
28 extraConfig = mkOption {
29 default = {};
30 description = "Extra configuration options for telegraf";
31 type = types.attrs;
32 example = {
33 outputs = {
34 influxdb = {
35 urls = ["http://localhost:8086"];
36 database = "telegraf";
37 };
38 };
39 inputs = {
40 statsd = {
41 service_address = ":8125";
42 delete_timings = true;
43 };
44 };
45 };
46 };
47 };
48 };
49
50
51 ###### implementation
52 config = mkIf config.services.telegraf.enable {
53 systemd.services.telegraf = {
54 description = "Telegraf Agent";
55 wantedBy = [ "multi-user.target" ];
56 after = [ "network-online.target" ];
57 serviceConfig = {
58 ExecStart=''${cfg.package}/bin/telegraf -config "${configFile}"'';
59 ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
60 User = "telegraf";
61 Restart = "on-failure";
62 };
63 };
64
65 users.extraUsers = [{
66 name = "telegraf";
67 uid = config.ids.uids.telegraf;
68 description = "telegraf daemon user";
69 }];
70 };
71}