2016-09-08 14:30:20 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.telegraf;
|
|
|
|
|
|
|
|
configFile = pkgs.runCommand "config.toml" {
|
|
|
|
buildInputs = [ pkgs.remarshal ];
|
2018-11-08 10:59:03 +00:00
|
|
|
preferLocalBuild = true;
|
2016-09-08 14:30:20 +01:00
|
|
|
} ''
|
|
|
|
remarshal -if json -of toml \
|
|
|
|
< ${pkgs.writeText "config.json" (builtins.toJSON cfg.extraConfig)} \
|
|
|
|
> $out
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
###### interface
|
|
|
|
options = {
|
|
|
|
services.telegraf = {
|
|
|
|
enable = mkEnableOption "telegraf server";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.telegraf;
|
|
|
|
defaultText = "pkgs.telegraf";
|
|
|
|
description = "Which telegraf derivation to use";
|
|
|
|
type = types.package;
|
|
|
|
};
|
|
|
|
|
2020-08-22 11:31:34 +01:00
|
|
|
environmentFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/telegraf.env";
|
|
|
|
description = ''
|
|
|
|
File to load as environment file. Environment variables
|
|
|
|
from this file will be interpolated into the config file
|
|
|
|
using envsubst with this syntax:
|
|
|
|
<literal>$ENVIRONMENT ''${VARIABLE}</literal>
|
|
|
|
This is useful to avoid putting secrets into the nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-08 14:30:20 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = {};
|
|
|
|
description = "Extra configuration options for telegraf";
|
|
|
|
type = types.attrs;
|
|
|
|
example = {
|
|
|
|
outputs = {
|
|
|
|
influxdb = {
|
|
|
|
urls = ["http://localhost:8086"];
|
|
|
|
database = "telegraf";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
inputs = {
|
|
|
|
statsd = {
|
|
|
|
service_address = ":8125";
|
|
|
|
delete_timings = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = mkIf config.services.telegraf.enable {
|
2020-08-22 11:31:34 +01:00
|
|
|
systemd.services.telegraf = let
|
|
|
|
finalConfigFile = if config.services.telegraf.environmentFile == null
|
|
|
|
then configFile
|
|
|
|
else "/tmp/config.toml";
|
|
|
|
in {
|
2016-09-08 14:30:20 +01:00
|
|
|
description = "Telegraf Agent";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
serviceConfig = {
|
2020-08-22 11:31:34 +01:00
|
|
|
EnvironmentFile = config.services.telegraf.environmentFile;
|
|
|
|
ExecStartPre = lib.optional (config.services.telegraf.environmentFile != null)
|
|
|
|
''${pkgs.envsubst}/bin/envsubst -o /tmp/config.toml -i "${configFile}"'';
|
|
|
|
ExecStart=''${cfg.package}/bin/telegraf -config ${finalConfigFile}'';
|
2016-09-08 14:30:20 +01:00
|
|
|
ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
User = "telegraf";
|
|
|
|
Restart = "on-failure";
|
2020-08-22 11:31:34 +01:00
|
|
|
PrivateTmp = true;
|
2020-08-22 08:17:40 +01:00
|
|
|
# for ping probes
|
|
|
|
AmbientCapabilities = [ "CAP_NET_RAW" ];
|
2016-09-08 14:30:20 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users.telegraf = {
|
2016-09-08 14:30:20 +01:00
|
|
|
uid = config.ids.uids.telegraf;
|
|
|
|
description = "telegraf daemon user";
|
2019-09-14 18:51:29 +01:00
|
|
|
};
|
2016-09-08 14:30:20 +01:00
|
|
|
};
|
|
|
|
}
|