diff --git a/salt-pillar/collectd/gw.sls b/salt-pillar/collectd/gw.sls new file mode 100644 index 000000000..6d4bffd --- /dev/null +++ b/salt-pillar/collectd/gw.sls @@ -0,0 +1,5 @@ +collectd: + network: client + interface: True + conntrack: True + dhcpcount: True diff --git a/salt-pillar/top.sls b/salt-pillar/top.sls index 9ea99bc..93c967a 100644 --- a/salt-pillar/top.sls +++ b/salt-pillar/top.sls @@ -14,10 +14,13 @@ base: - dhcp - bird.radv - bird.ospf + - collectd.gw 'pub-gw or serv-gw or radio-gw': - dhcp - bird.radv - bird.ospf + 'pub-gw': + - collectd.gw 'c3d2-gw or c3d2-anon or mgmt-gw': - bird.ospf 'c3d2-gw': diff --git a/salt/collectd/collectd.conf b/salt/collectd/collectd.conf index 1b9f353..4dc9c99 100644 --- a/salt/collectd/collectd.conf +++ b/salt/collectd/collectd.conf @@ -38,6 +38,12 @@ LoadPlugin ping Interval 10 +{%- elif plugin == 'dhcpcount' and conf is True %} +LoadPlugin exec + + Exec "collectd" "/usr/bin/dhcpcount.rb" + + {%- elif conf is mapping %} LoadPlugin {{ plugin }} diff --git a/salt/collectd/dhcpcount.rb b/salt/collectd/dhcpcount.rb new file mode 100644 index 000000000..3ce75e8 --- /dev/null +++ b/salt/collectd/dhcpcount.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby + +require 'date' + +hostname = `hostname`.strip + +loop do + starts = nil + ends = nil + count = 0 + + IO::readlines("/var/lib/dhcp/dhcpd.leases").each do |line| + if line =~ /^lease/ + starts = nil + ends = nil + elsif line =~ /starts \d+ (.+?);/ + starts = DateTime.parse $1 + elsif line =~ /ends \d+ (.+?);/ + ends = DateTime.parse $1 + elsif line =~ /^\}/ + now = DateTime.now + if starts and ends and + starts <= now and ends >= now + count += 1 + end + end + end + puts "PUTVAL #{hostname}/dhcpd/leases N:#{count}" + + sleep 10 +end diff --git a/salt/collectd/init.sls b/salt/collectd/init.sls index d4bea17..6bc0073 100644 --- a/salt/collectd/init.sls +++ b/salt/collectd/init.sls @@ -15,3 +15,10 @@ collectd: file.managed: - source: salt://collectd/collectd.conf - template: 'jinja' + +{%- if pillar['collectd'].get('dhcpcount') %} +/usr/bin/dhcpcount.rb: + file.managed: + - source: salt://collectd/dhcpcount.rb + - mode: 755 +{%- endif %} diff --git a/salt/top.sls b/salt/top.sls index 04b829e..f478da5 100644 --- a/salt/top.sls +++ b/salt/top.sls @@ -13,6 +13,7 @@ base: - forwarding - bird - dhcp + - collectd 'priv13-gw': - firewall.priv-stateful 'pub-gw or serv-gw': @@ -20,6 +21,8 @@ base: - forwarding - bird - dhcp + 'pub-gw': + - collectd 'c3d2-gw or c3d2-anon or mgmt-gw': - no-ssh - forwarding