nix/nixos-module/collectd: add dhcpcount.rb
This commit is contained in:
parent
4ef98c986c
commit
1a8b4811f7
|
@ -50,6 +50,22 @@ in
|
||||||
hddtemp = "";
|
hddtemp = "";
|
||||||
sensors = "";
|
sensors = "";
|
||||||
thermal = "";
|
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}"
|
||||||
|
'';
|
||||||
}) ];
|
}) ];
|
||||||
};
|
};
|
||||||
}
|
}
|
39
nix/nixos-module/collectd/dhcpcount.rb
Normal file
39
nix/nixos-module/collectd/dhcpcount.rb
Normal 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
|
|
@ -13,7 +13,7 @@ in {
|
||||||
../lib/config/options.nix
|
../lib/config/options.nix
|
||||||
./defaults.nix
|
./defaults.nix
|
||||||
./network.nix
|
./network.nix
|
||||||
./collectd.nix
|
./collectd
|
||||||
] ++
|
] ++
|
||||||
optionals (hostConfig.role == "server") [
|
optionals (hostConfig.role == "server") [
|
||||||
./server/lxc-containers.nix
|
./server/lxc-containers.nix
|
||||||
|
|
Loading…
Reference in New Issue
Block a user