2021-05-23 23:16:28 +02:00
|
|
|
{ hostName, self, config, lib, pkgs, ... }:
|
2021-04-07 00:01:21 +02:00
|
|
|
|
|
|
|
let
|
2021-05-23 23:16:28 +02:00
|
|
|
inherit (pkgs.targetPlatform) system;
|
2021-04-07 00:01:21 +02:00
|
|
|
hostRole = config.site.hosts.${hostName}.role;
|
|
|
|
networkPort = 25826;
|
2021-04-07 23:58:05 +02:00
|
|
|
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" ''
|
2021-04-07 22:42:02 +02:00
|
|
|
stations value:GAUGE:0:U
|
|
|
|
'';
|
2021-05-28 02:02:21 +02:00
|
|
|
inherit (config.services.collectd) user;
|
|
|
|
execUser =
|
|
|
|
if user == "root"
|
|
|
|
then "nobody"
|
|
|
|
else user;
|
2021-05-27 03:25:45 +02:00
|
|
|
isUpstream =
|
|
|
|
builtins.match "upstream.*" hostName != null ||
|
|
|
|
builtins.match "anon.*" hostName != null;
|
2021-05-23 23:16:28 +02:00
|
|
|
hasStarlink =
|
|
|
|
builtins.any ({ upstream, ... }:
|
|
|
|
if upstream == null
|
|
|
|
then false
|
|
|
|
else upstream.provider == "starlink"
|
|
|
|
) (builtins.attrValues config.site.hosts.${hostName}.interfaces);
|
2021-04-07 00:01:21 +02:00
|
|
|
in
|
|
|
|
{
|
2021-05-27 03:25:45 +02:00
|
|
|
services.collectd = lib.mkMerge [ {
|
2021-04-07 00:01:21 +02:00
|
|
|
enable = true;
|
|
|
|
buildMinimalPackage = true;
|
|
|
|
|
2021-04-07 22:42:02 +02:00
|
|
|
extraConfig = ''
|
2021-04-07 23:59:58 +02:00
|
|
|
TypesDB "${upstreamTypesDb}" "${customTypesDb}"
|
2021-04-07 22:42:02 +02:00
|
|
|
'';
|
|
|
|
|
2021-05-27 03:25:45 +02:00
|
|
|
plugins = {
|
2021-04-07 00:01:21 +02:00
|
|
|
interface = "";
|
|
|
|
conntrack = "";
|
2021-05-27 03:25:45 +02:00
|
|
|
};
|
|
|
|
} (lib.optionalAttrs (hostName == "stats") {
|
|
|
|
plugins.network = ''
|
2021-04-07 00:01:21 +02:00
|
|
|
Listen "::" "${toString networkPort}"
|
|
|
|
Forward true
|
|
|
|
Server "${config.site.net.serv.hosts4.spaceapi}" "${toString networkPort}"
|
|
|
|
Server "${config.site.net.serv.hosts4.grafana}" "${toString networkPort}"
|
2022-11-27 01:08:19 +01:00
|
|
|
Server "${config.site.net.serv.hosts4.prometheus}" "${toString networkPort}"
|
2021-04-07 00:01:21 +02:00
|
|
|
'';
|
2022-07-16 01:28:43 +02:00
|
|
|
plugins.mqtt = ''
|
|
|
|
<Publish "broker">
|
2022-07-16 01:32:27 +02:00
|
|
|
Host "${config.site.mqttServer.host}"
|
|
|
|
User "${config.site.mqttServer.user}"
|
|
|
|
Password "${config.site.mqttServer.password}"
|
2022-07-16 01:28:43 +02:00
|
|
|
ClientId "collectd-${hostName}"
|
|
|
|
</Publish>
|
|
|
|
'';
|
2021-05-27 03:25:45 +02:00
|
|
|
}) (lib.optionalAttrs (hostName != "stats") {
|
|
|
|
plugins.network = ''
|
2021-04-07 00:01:21 +02:00
|
|
|
Server "${config.site.net.serv.hosts6.dn42.stats}" "${toString networkPort}"
|
|
|
|
'';
|
2021-05-27 03:25:45 +02:00
|
|
|
}) (lib.optionalAttrs (hostRole == "server") {
|
|
|
|
plugins = {
|
2021-04-07 00:01:21 +02:00
|
|
|
irq = "";
|
|
|
|
cpu = "";
|
|
|
|
load = "";
|
|
|
|
memory = "";
|
|
|
|
swap = "";
|
|
|
|
entropy = "";
|
|
|
|
disk = "";
|
|
|
|
df = "";
|
|
|
|
processes = "";
|
|
|
|
hddtemp = "";
|
|
|
|
sensors = "";
|
|
|
|
thermal = "";
|
2021-05-27 03:25:45 +02:00
|
|
|
};
|
|
|
|
}) (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 ''
|
2022-08-23 03:05:42 +02:00
|
|
|
Exec "${execUser}" "/run/wrappers/bin/collectd-dhcpcount" "${toString maxTimeout}"
|
2022-08-23 02:59:25 +02:00
|
|
|
'';
|
2021-05-27 03:25:45 +02:00
|
|
|
}) (lib.optionalAttrs config.services.unbound.enable {
|
|
|
|
plugins.exec = ''
|
2021-05-28 02:02:21 +02:00
|
|
|
Exec "${execUser}" "${pkgs.ruby}/bin/ruby" "${./unbound.rb}"
|
2021-04-15 00:23:27 +02:00
|
|
|
'';
|
2021-05-27 03:25:45 +02:00
|
|
|
}) (lib.optionalAttrs hasStarlink {
|
|
|
|
plugins.exec = ''
|
2021-05-27 03:47:14 +02:00
|
|
|
Exec "nobody" "${self.packages.${system}.starlink-stats}/bin/starlink-stats" "192.168.100.1:9200"
|
2021-05-23 23:16:28 +02:00
|
|
|
'';
|
2021-05-27 03:25:45 +02:00
|
|
|
}) ];
|
2022-06-20 01:20:26 +02:00
|
|
|
|
|
|
|
|
2022-06-24 00:05:54 +02:00
|
|
|
systemd.services.collectd = lib.mkIf config.services.dhcpd4.enable {
|
2022-06-24 00:04:41 +02:00
|
|
|
after = [ "dhcpd4.service" ];
|
2022-08-23 02:59:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
security.wrappers = lib.mkIf config.services.dhcpd4.enable {
|
|
|
|
collectd-dhcpcount =
|
|
|
|
let
|
|
|
|
dhcpcount = pkgs.runCommand "dhcpcount" {
|
|
|
|
src = ./dhcpcount.rb;
|
|
|
|
buildInputs = [ pkgs.ruby ];
|
|
|
|
} ''
|
|
|
|
cp $src dhcpcount.rb
|
|
|
|
patchShebangs dhcpcount.rb
|
|
|
|
mkdir -p $out/bin
|
|
|
|
cp dhcpcount.rb $out/bin/dhcpcount
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
source = "${dhcpcount}/bin/dhcpcount";
|
|
|
|
};
|
2022-06-24 00:04:41 +02:00
|
|
|
};
|
2021-04-07 00:01:21 +02:00
|
|
|
}
|