nix/nixos-module/collectd: add dhcpcount.rb

This commit is contained in:
Astro 2021-04-12 23:32:21 +02:00
parent 4ef98c986c
commit 1a8b4811f7
3 changed files with 56 additions and 1 deletions

View File

@ -50,6 +50,22 @@ in
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}"
'';
}) ];
};
}

View File

@ -0,0 +1,39 @@
#!/usr/bin/env ruby
require 'date'
INTERVAL = 10
TIMEOUT = ARGV[0].to_i
hostname = `hostname`.strip
STDOUT.sync = true
loop do
seen = {}
count = 0
addr = nil
starts = nil
IO::readlines("/var/lib/dhcp/dhcpd.leases").each do |line|
if line =~ /^lease (.+) \{/
addr = $1
starts = nil
elsif line =~ /starts \d+ (.+?);/
starts = DateTime.parse($1).to_time
elsif line =~ /^\}/
now = Time.now
if starts and
now >= starts and now < starts + TIMEOUT
unless seen[addr]
count += 1
seen[addr] = true
end
end
end
end
puts "PUTVAL \"#{hostname}/exec-dhcpd/current_sessions-leases\" interval=#{INTERVAL} N:#{count}"
sleep INTERVAL
end

View File

@ -13,7 +13,7 @@ in {
../lib/config/options.nix
./defaults.nix
./network.nix
./collectd.nix
./collectd
] ++
optionals (hostConfig.role == "server") [
./server/lxc-containers.nix