2016-09-08 14:30:20 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.telegraf;
|
|
|
|
|
2020-09-08 06:59:50 +01:00
|
|
|
settingsFormat = pkgs.formats.toml {};
|
|
|
|
configFile = settingsFormat.generate "config.toml" cfg.extraConfig;
|
2016-09-08 14:30:20 +01:00
|
|
|
in {
|
|
|
|
###### interface
|
|
|
|
options = {
|
|
|
|
services.telegraf = {
|
|
|
|
enable = mkEnableOption "telegraf server";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.telegraf;
|
2021-10-03 17:06:03 +01:00
|
|
|
defaultText = literalExpression "pkgs.telegraf";
|
2016-09-08 14:30:20 +01:00
|
|
|
description = "Which telegraf derivation to use";
|
|
|
|
type = types.package;
|
|
|
|
};
|
|
|
|
|
2020-11-12 12:49:00 +00:00
|
|
|
environmentFiles = mkOption {
|
2020-11-18 20:29:12 +00:00
|
|
|
type = types.listOf types.path;
|
2020-11-12 12:49:00 +00:00
|
|
|
default = [];
|
2021-10-03 17:06:03 +01:00
|
|
|
example = [ "/run/keys/telegraf.env" ];
|
2020-08-22 11:31:34 +01:00
|
|
|
description = ''
|
2021-06-22 08:07:35 +01:00
|
|
|
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</literal> or <literal>''${VARIABLE}</literal>.
|
2020-08-22 11:31:34 +01:00
|
|
|
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";
|
2020-09-08 06:59:50 +01:00
|
|
|
type = settingsFormat.type;
|
2016-09-08 14:30:20 +01:00
|
|
|
example = {
|
2020-11-18 20:30:04 +00:00
|
|
|
outputs.influxdb = {
|
|
|
|
urls = ["http://localhost:8086"];
|
|
|
|
database = "telegraf";
|
2016-09-08 14:30:20 +01:00
|
|
|
};
|
2020-11-18 20:30:04 +00:00
|
|
|
inputs.statsd = {
|
|
|
|
service_address = ":8125";
|
|
|
|
delete_timings = true;
|
2016-09-08 14:30:20 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = mkIf config.services.telegraf.enable {
|
2020-08-22 11:31:34 +01:00
|
|
|
systemd.services.telegraf = let
|
2020-11-12 12:49:00 +00:00
|
|
|
finalConfigFile = if config.services.telegraf.environmentFiles == []
|
2020-08-22 11:31:34 +01:00
|
|
|
then configFile
|
2020-09-08 06:59:50 +01:00
|
|
|
else "/var/run/telegraf/config.toml";
|
2020-08-22 11:31:34 +01:00
|
|
|
in {
|
2016-09-08 14:30:20 +01:00
|
|
|
description = "Telegraf Agent";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
serviceConfig = {
|
2020-11-12 12:49:00 +00:00
|
|
|
EnvironmentFile = config.services.telegraf.environmentFiles;
|
|
|
|
ExecStartPre = lib.optional (config.services.telegraf.environmentFiles != [])
|
2020-09-08 06:59:50 +01:00
|
|
|
(pkgs.writeShellScript "pre-start" ''
|
|
|
|
umask 077
|
|
|
|
${pkgs.envsubst}/bin/envsubst -i "${configFile}" > /var/run/telegraf/config.toml
|
|
|
|
'');
|
2021-01-24 09:19:10 +00:00
|
|
|
ExecStart="${cfg.package}/bin/telegraf -config ${finalConfigFile}";
|
2016-09-08 14:30:20 +01:00
|
|
|
ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2020-09-08 06:59:50 +01:00
|
|
|
RuntimeDirectory = "telegraf";
|
2016-09-08 14:30:20 +01:00
|
|
|
User = "telegraf";
|
2021-04-15 19:34:10 +01:00
|
|
|
Group = "telegraf";
|
2016-09-08 14:30:20 +01:00
|
|
|
Restart = "on-failure";
|
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;
|
2021-04-15 19:34:10 +01:00
|
|
|
group = "telegraf";
|
2016-09-08 14:30:20 +01:00
|
|
|
description = "telegraf daemon user";
|
2019-09-14 18:51:29 +01:00
|
|
|
};
|
2021-04-15 19:34:10 +01:00
|
|
|
|
|
|
|
users.groups.telegraf = {};
|
2016-09-08 14:30:20 +01:00
|
|
|
};
|
|
|
|
}
|