network/nix/nixos-module/collectd/default.nix

87 lines
2.6 KiB
Nix

{ 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
'';
hasStarlink =
builtins.any ({ upstream, ... }:
if upstream == null
then false
else upstream.provider == "starlink"
) (builtins.attrValues config.site.hosts.${hostName}.interfaces);
in
{
services.collectd = {
enable = true;
buildMinimalPackage = true;
extraConfig = ''
TypesDB "${upstreamTypesDb}" "${customTypesDb}"
'';
plugins = lib.mkMerge [ {
interface = "";
conntrack = "";
# TODO: dhcpcount
} (lib.optionalAttrs (hostName == "stats") {
network = ''
Listen "::" "${toString networkPort}"
Forward true
Server "${config.site.net.serv.hosts4.spaceapi}" "${toString networkPort}"
Server "${config.site.net.serv.hosts4.grafana}" "${toString networkPort}"
'';
}) (lib.optionalAttrs (hostName != "stats") {
network = ''
Server "${config.site.net.serv.hosts6.dn42.stats}" "${toString networkPort}"
'';
}) (lib.optionalAttrs (hostRole == "server") {
irq = "";
cpu = "";
load = "";
memory = "";
swap = "";
entropy = "";
disk = "";
df = "";
processes = "";
hddtemp = "";
sensors = "";
thermal = "";
}) (lib.optionalAttrs config.services.dhcpd4.enable {
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 "collectd" "${pkgs.ruby}/bin/ruby" "${./dhcpcount.rb}" "${toString maxTimeout}"
'';
}) (lib.optionalAttrs config.services.unbound.enable {
exec = ''
Exec "collectd" "${pkgs.ruby}/bin/ruby" "${./unbound.rb}"
'';
}) (lib.optionalAttrs hasStarlink {
exec = ''
Exec "collectd" "${self.packages.${system}.starlink-stats}/bin/starlink-stats" "192.168.100.1:9200"
'';
}) ];
};
}