From f4d5a76a811df99d26706336dda940556c4491f5 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Thu, 27 Apr 2023 10:30:29 +0200 Subject: [PATCH] netdata module: add automatic claim Allow machines to auto-register themselves by passing the claim token. --- nixos/modules/services/monitoring/netdata.nix | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/monitoring/netdata.nix b/nixos/modules/services/monitoring/netdata.nix index 92c870bb23f1..60dfcb769350 100644 --- a/nixos/modules/services/monitoring/netdata.nix +++ b/nixos/modules/services/monitoring/netdata.nix @@ -159,6 +159,15 @@ in { ''; }; + claim_token_file = mkOption { + type = types.nullOr types.path; + default = null; + description = lib.mdDoc '' + If set, automatically registers the agent using the given claim token + file. + ''; + }; + enableAnalyticsReporting = mkOption { type = types.bool; default = false; @@ -246,7 +255,25 @@ in { PrivateTmp = true; ProtectControlGroups = true; PrivateMounts = true; - }; + } // (lib.optionalAttrs (cfg.claim_token_file != null) { + LoadCredential = [ + "netdata_claim_token:${cfg.claim_token_file}" + ]; + + ExecStartPre = pkgs.writeShellScript "netdata-claim" '' + set -euo pipefail + + if [[ -f /var/lib/netdata/cloud.d/claimed_id ]]; then + # Already registered + exit + fi + + exec ${cfg.package}/bin/netdata-claim.sh \ + -token="$(< "$CREDENTIALS_DIRECTORY/netdata_claim_token")" \ + -url=https://app.netdata.cloud \ + -daemon-not-running + ''; + }); }; systemd.enableCgroupAccounting = true;