dn42: add routecount collectd stats

This commit is contained in:
Astro 2021-03-12 22:20:35 +01:00
parent fb5751220f
commit e90c920103
2 changed files with 49 additions and 1 deletions

View File

@ -25,7 +25,17 @@ in {
statistics.enable = true;
};
};
services.resolved.enable = false;
services.collectd.plugins.exec =
let
routecount = pkgs.writeScript "run-routecount" ''
#!${pkgs.bash}/bin/bash
export PATH=${lib.makeBinPath (with pkgs; [ ruby iproute ] )}
ruby ${./routecount.rb}
'';
in ''
Exec "collectd" "${routecount}"
'';
environment.systemPackages = with pkgs; [ vim ];

View File

@ -0,0 +1,38 @@
#!/usr/bin/env ruby
INTERVAL = ENV['COLLECTD_INTERVAL'].to_i
HOSTNAME = ENV['COLLECTD_HOSTNAME']
def gather cmd
counts = {}
`#{cmd}`.lines.each do |l|
dev = nil
gw = nil
if l =~ / dev (\S+)/
dev = $1
end
if l =~ / via (\S+)/
gw = $1
end
if gw and dev
target = [gw, dev].join "%"
counts[target] = 0 unless counts.has_key? target
counts[target] += 1
end
end
counts
end
loop do
v4 = gather "ip -4 route"
v4.each do |target,count|
puts "PUTVAL \"#{HOSTNAME}/exec-routes4/routes-#{target}\" interval=#{INTERVAL} N:#{count}"
end
v6 = gather "ip -6 route"
v6.each do |target,count|
puts "PUTVAL \"#{HOSTNAME}/exec-routes6/routes-#{target}\" interval=#{INTERVAL} N:#{count}"
end
sleep INTERVAL
end