{ hostName, self, config, lib, pkgs, ... }: let inherit (pkgs.targetPlatform) system; hostRole = config.site.hosts.${hostName}.role; networkPort = 25826; upstreamTypesDb = pkgs.stdenv.mkDerivation { name = "types.db"; src = config.services.collectd.package.src; phases = [ "unpackPhase" "installPhase" ]; installPhase = "cp src/types.db $out"; }; customTypesDb = builtins.toFile "types.db" '' stations value:GAUGE:0:U ''; inherit (config.services.collectd) user; execUser = if user == "root" then "nobody" else user; isUpstream = builtins.match "upstream.*" hostName != null || builtins.match "anon.*" hostName != null; hasStarlink = builtins.any ({ upstream, ... }: if upstream == null then false else upstream.provider == "starlink" ) (builtins.attrValues config.site.hosts.${hostName}.interfaces); in { services.collectd = lib.mkMerge [ { enable = true; buildMinimalPackage = true; extraConfig = '' TypesDB "${upstreamTypesDb}" "${customTypesDb}" ''; plugins = { interface = ""; conntrack = ""; }; } (lib.optionalAttrs (hostName == "stats") { plugins.network = '' Listen "::" "${toString networkPort}" Forward true Server "${config.site.net.serv.hosts4.spaceapi}" "${toString networkPort}" Server "${config.site.net.serv.hosts4.grafana}" "${toString networkPort}" ''; plugins.mqtt = '' Host "${config.site.mqttServer.host}" User "${config.site.mqttServer.user}" Password "${config.site.mqttServer.password}" ClientId "collectd-${hostName}" ''; }) (lib.optionalAttrs (hostName != "stats") { plugins.network = '' Server "${config.site.net.serv.hosts6.dn42.stats}" "${toString networkPort}" ''; }) (lib.optionalAttrs (hostRole == "server") { plugins = { irq = ""; cpu = ""; load = ""; memory = ""; swap = ""; entropy = ""; disk = ""; df = ""; processes = ""; hddtemp = ""; sensors = ""; thermal = ""; }; }) (lib.optionalAttrs isUpstream { user = "root"; plugins.ping = '' Interval 10 Timeout 1 Size 0 MaxMissed 100 Host "8.8.8.8" Host "2001:4860:4860::8888" Host "ip5886d7b6.static.kabel-deutschland.de" Host "r.njalla.net" Host "inbert.c3d2.de" Host "heise.de" ''; }) (lib.optionalAttrs config.services.dhcpd4.enable { plugins.exec = let maxTimeout = builtins.foldl' (maxTimeout: net: let dhcpConf = config.site.net.${net}.dhcp; in if dhcpConf != null && dhcpConf.server == hostName && dhcpConf.time > maxTimeout then dhcpConf.time else maxTimeout ) 180 (builtins.attrNames config.site.net); in '' Exec "${execUser}" "${pkgs.ruby}/bin/ruby" "${./dhcpcount.rb}" "${toString maxTimeout}" ''; }) (lib.optionalAttrs config.services.unbound.enable { plugins.exec = '' Exec "${execUser}" "${pkgs.ruby}/bin/ruby" "${./unbound.rb}" ''; }) (lib.optionalAttrs hasStarlink { plugins.exec = '' Exec "nobody" "${self.packages.${system}.starlink-stats}/bin/starlink-stats" "192.168.100.1:9200" ''; }) ]; systemd.services.collectd = lib.mkIf config.services.dhcpd4.enable { after = [ "dhcpd4.service" ]; serviceConfig.StateDirectory = "dhcpd4"; }; }