{ config, lib, pkgs, ... }: let cfg = config.syscfg.monitoring.telegraf; hasCollector = name: builtins.elem name cfg.collectors; baseConfig = { agent = { interval = "10s"; round_interval = true; metric_batch_size = 1000; metric_buffer_limit = 10000; flush_interval = "10s"; hostname = config.syscfg.hostname; omit_hostname = false; }; global_tags = { host = config.syscfg.hostname; }; }; inputsConfig = lib.mkMerge [ (lib.mkIf (hasCollector "cpu") { inputs.cpu = { percpu = true; totalcpu = true; collect_cpu_time = false; report_active = false; }; }) (lib.mkIf (hasCollector "mem") { inputs.mem = { }; }) (lib.mkIf (hasCollector "swap") { inputs.swap = { }; }) (lib.mkIf (hasCollector "system") { inputs.system = { }; }) (lib.mkIf (hasCollector "disk") { inputs.disk = { ignore_fs = [ "tmpfs" "devtmpfs" "devfs" "overlay" "squashfs" ]; }; }) (lib.mkIf (hasCollector "diskio") { inputs.diskio = { }; }) (lib.mkIf (hasCollector "kernel") { inputs.kernel = { }; }) (lib.mkIf (hasCollector "net") { inputs.net = { }; }) (lib.mkIf (hasCollector "netstat") { inputs.netstat = { }; }) (lib.mkIf (hasCollector "processes") { inputs.processes = { }; }) (lib.mkIf (hasCollector "temp") { inputs.temp = { }; }) (lib.mkIf (hasCollector "mdstat") { inputs.mdstat = { }; }) (lib.mkIf (hasCollector "smart") { inputs.smart = { use_sudo = true; attributes = true; }; }) (lib.mkIf (hasCollector "docker") { inputs.docker = { endpoint = "unix:///var/run/docker.sock"; timeout = "5s"; perdevice_include = ["blkio" "cpu" "network"]; total_include = []; }; }) (lib.mkIf (hasCollector "ping") { inputs.ping = { urls = [ "1.1.1.1" ]; count = 4; interval = "60s"; timeout = 5.0; binary = "${pkgs.iputils}/bin/ping"; }; }) ]; outputsConfig = lib.mkMerge [{ outputs.influxdb_v3 = { urls = cfg.outputs; token = "$INFLUX_TOKEN";#config.sops.secrets.telegraf.path; database = "telegraf"; }; } ]; in { config = lib.mkIf cfg.enable { services.telegraf = { enable = true; environmentFiles = [ config.sops.secrets.telegraf.path ]; extraConfig = lib.mkMerge [ baseConfig inputsConfig outputsConfig cfg.extraConfig ]; }; users.users.telegraf.extraGroups = ["podman"]; systemd.services.telegraf = { path = lib.optionals (hasCollector "smart") [ pkgs.smartmontools ]; serviceConfig.SupplementaryGroups = ["podman"]; }; security.sudo.extraRules = lib.optionals (hasCollector "smart") [{ users = [ "telegraf" ]; commands = [{ command = "${pkgs.smartmontools}/bin/smartctl"; options = [ "NOPASSWD" ]; }]; }]; }; }