nixos/telegraf: switch to setting types
This allows to split up configuration into multiple modules
This commit is contained in:
parent
157d7354d6
commit
8edc4619ab
|
@ -5,14 +5,8 @@ with lib;
|
||||||
let
|
let
|
||||||
cfg = config.services.telegraf;
|
cfg = config.services.telegraf;
|
||||||
|
|
||||||
configFile = pkgs.runCommand "config.toml" {
|
settingsFormat = pkgs.formats.toml {};
|
||||||
buildInputs = [ pkgs.remarshal ];
|
configFile = settingsFormat.generate "config.toml" cfg.extraConfig;
|
||||||
preferLocalBuild = true;
|
|
||||||
} ''
|
|
||||||
remarshal -if json -of toml \
|
|
||||||
< ${pkgs.writeText "config.json" (builtins.toJSON cfg.extraConfig)} \
|
|
||||||
> $out
|
|
||||||
'';
|
|
||||||
in {
|
in {
|
||||||
###### interface
|
###### interface
|
||||||
options = {
|
options = {
|
||||||
|
@ -42,7 +36,7 @@ in {
|
||||||
extraConfig = mkOption {
|
extraConfig = mkOption {
|
||||||
default = {};
|
default = {};
|
||||||
description = "Extra configuration options for telegraf";
|
description = "Extra configuration options for telegraf";
|
||||||
type = types.attrs;
|
type = settingsFormat.type;
|
||||||
example = {
|
example = {
|
||||||
outputs = {
|
outputs = {
|
||||||
influxdb = {
|
influxdb = {
|
||||||
|
@ -67,7 +61,7 @@ in {
|
||||||
systemd.services.telegraf = let
|
systemd.services.telegraf = let
|
||||||
finalConfigFile = if config.services.telegraf.environmentFile == null
|
finalConfigFile = if config.services.telegraf.environmentFile == null
|
||||||
then configFile
|
then configFile
|
||||||
else "/tmp/config.toml";
|
else "/var/run/telegraf/config.toml";
|
||||||
in {
|
in {
|
||||||
description = "Telegraf Agent";
|
description = "Telegraf Agent";
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
@ -75,12 +69,15 @@ in {
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
EnvironmentFile = config.services.telegraf.environmentFile;
|
EnvironmentFile = config.services.telegraf.environmentFile;
|
||||||
ExecStartPre = lib.optional (config.services.telegraf.environmentFile != null)
|
ExecStartPre = lib.optional (config.services.telegraf.environmentFile != null)
|
||||||
''${pkgs.envsubst}/bin/envsubst -o /tmp/config.toml -i "${configFile}"'';
|
(pkgs.writeShellScript "pre-start" ''
|
||||||
|
umask 077
|
||||||
|
${pkgs.envsubst}/bin/envsubst -i "${configFile}" > /var/run/telegraf/config.toml
|
||||||
|
'');
|
||||||
ExecStart=''${cfg.package}/bin/telegraf -config ${finalConfigFile}'';
|
ExecStart=''${cfg.package}/bin/telegraf -config ${finalConfigFile}'';
|
||||||
ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
ExecReload="${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
||||||
|
RuntimeDirectory = "telegraf";
|
||||||
User = "telegraf";
|
User = "telegraf";
|
||||||
Restart = "on-failure";
|
Restart = "on-failure";
|
||||||
PrivateTmp = true;
|
|
||||||
# for ping probes
|
# for ping probes
|
||||||
AmbientCapabilities = [ "CAP_NET_RAW" ];
|
AmbientCapabilities = [ "CAP_NET_RAW" ];
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,12 +6,15 @@ import ./make-test-python.nix ({ pkgs, ...} : {
|
||||||
|
|
||||||
machine = { ... }: {
|
machine = { ... }: {
|
||||||
services.telegraf.enable = true;
|
services.telegraf.enable = true;
|
||||||
|
services.telegraf.environmentFile = pkgs.writeText "secrets" ''
|
||||||
|
SECRET=example
|
||||||
|
'';
|
||||||
services.telegraf.extraConfig = {
|
services.telegraf.extraConfig = {
|
||||||
agent.interval = "1s";
|
agent.interval = "1s";
|
||||||
agent.flush_interval = "1s";
|
agent.flush_interval = "1s";
|
||||||
inputs.exec = {
|
inputs.exec = {
|
||||||
commands = [
|
commands = [
|
||||||
"${pkgs.runtimeShell} -c 'echo example,tag=a i=42i'"
|
"${pkgs.runtimeShell} -c 'echo $SECRET,tag=a i=42i'"
|
||||||
];
|
];
|
||||||
timeout = "5s";
|
timeout = "5s";
|
||||||
data_format = "influx";
|
data_format = "influx";
|
||||||
|
|
Loading…
Reference in a new issue