2016-09-08 13:30:20 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.telegraf;
|
|
|
|
|
2020-09-08 05:59:50 +00:00
|
|
|
settingsFormat = pkgs.formats.toml {};
|
|
|
|
configFile = settingsFormat.generate "config.toml" cfg.extraConfig;
|
2016-09-08 13:30:20 +00:00
|
|
|
in {
|
|
|
|
###### interface
|
|
|
|
options = {
|
|
|
|
services.telegraf = {
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "telegraf server");
|
2016-09-08 13:30:20 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.telegraf;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "pkgs.telegraf";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Which telegraf derivation to use";
|
2016-09-08 13:30:20 +00:00
|
|
|
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 16:06:03 +00:00
|
|
|
example = [ "/run/keys/telegraf.env" ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-22 07:07:35 +00:00
|
|
|
File to load as environment file. Environment variables from this file
|
|
|
|
will be interpolated into the config file using envsubst with this
|
2022-07-28 21:19:15 +00:00
|
|
|
syntax: `$ENVIRONMENT` or `''${VARIABLE}`.
|
2020-08-22 10:31:34 +00:00
|
|
|
This is useful to avoid putting secrets into the nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-09-08 13:30:20 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = {};
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Extra configuration options for telegraf";
|
2020-09-08 05:59:50 +00:00
|
|
|
type = settingsFormat.type;
|
2016-09-08 13:30:20 +00:00
|
|
|
example = {
|
2020-11-18 20:30:04 +00:00
|
|
|
outputs.influxdb = {
|
|
|
|
urls = ["http://localhost:8086"];
|
|
|
|
database = "telegraf";
|
2016-09-08 13:30:20 +00:00
|
|
|
};
|
2020-11-18 20:30:04 +00:00
|
|
|
inputs.statsd = {
|
|
|
|
service_address = ":8125";
|
|
|
|
delete_timings = true;
|
2016-09-08 13:30:20 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = mkIf config.services.telegraf.enable {
|
2020-08-22 10:31:34 +00:00
|
|
|
systemd.services.telegraf = let
|
2020-11-12 12:49:00 +00:00
|
|
|
finalConfigFile = if config.services.telegraf.environmentFiles == []
|
2020-08-22 10:31:34 +00:00
|
|
|
then configFile
|
2020-09-08 05:59:50 +00:00
|
|
|
else "/var/run/telegraf/config.toml";
|
2020-08-22 10:31:34 +00:00
|
|
|
in {
|
2016-09-08 13:30:20 +00: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 05:59:50 +00: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 13:30:20 +00:00
|
|
|
ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2020-09-08 05:59:50 +00:00
|
|
|
RuntimeDirectory = "telegraf";
|
2016-09-08 13:30:20 +00:00
|
|
|
User = "telegraf";
|
2021-04-15 18:34:10 +00:00
|
|
|
Group = "telegraf";
|
2016-09-08 13:30:20 +00:00
|
|
|
Restart = "on-failure";
|
2020-08-22 07:17:40 +00:00
|
|
|
# for ping probes
|
|
|
|
AmbientCapabilities = [ "CAP_NET_RAW" ];
|
2016-09-08 13:30:20 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-09-14 17:51:29 +00:00
|
|
|
users.users.telegraf = {
|
2016-09-08 13:30:20 +00:00
|
|
|
uid = config.ids.uids.telegraf;
|
2021-04-15 18:34:10 +00:00
|
|
|
group = "telegraf";
|
2016-09-08 13:30:20 +00:00
|
|
|
description = "telegraf daemon user";
|
2019-09-14 17:51:29 +00:00
|
|
|
};
|
2021-04-15 18:34:10 +00:00
|
|
|
|
|
|
|
users.groups.telegraf = {};
|
2016-09-08 13:30:20 +00:00
|
|
|
};
|
|
|
|
}
|