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 = {
|
|
|
|
enable = mkEnableOption "telegraf server";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.telegraf;
|
|
|
|
defaultText = "pkgs.telegraf";
|
|
|
|
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 = [];
|
2020-08-22 10:31:34 +00:00
|
|
|
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 13:30:20 +00:00
|
|
|
extraConfig = mkOption {
|
|
|
|
default = {};
|
|
|
|
description = "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
|
|
|
|
'');
|
2020-08-22 10:31:34 +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";
|
|
|
|
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;
|
|
|
|
description = "telegraf daemon user";
|
2019-09-14 17:51:29 +00:00
|
|
|
};
|
2016-09-08 13:30:20 +00:00
|
|
|
};
|
|
|
|
}
|