From f08a3e486ec6ac963df42a0b1f345db0feaebbb0 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 18 Feb 2021 14:39:43 +0100 Subject: [PATCH] collectd/dhcpcount: properly calculate timeout --- salt/collectd/collectd.conf | 10 +++++++++- salt/collectd/dhcpcount.rb | 13 +++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/salt/collectd/collectd.conf b/salt/collectd/collectd.conf index cb0bec2..6de4cc4 100644 --- a/salt/collectd/collectd.conf +++ b/salt/collectd/collectd.conf @@ -35,9 +35,17 @@ LoadPlugin ping {%- elif plugin == 'dhcpcount' and conf %} +{%- set timeout = 60 %} +{%- for iface, ips in salt['grains.get']('ip_interfaces').items() %} +{%- set dhcp_conf = pillar['dhcp'].get(iface) %} +{%- if dhcp_conf and dhcp_conf.get('time') and dhcp_conf.get('time') > timeout %} +{%- set timeout = dhcp_conf['time'] %} +{%- endif %} +{%- endfor %} + LoadPlugin exec - Exec "nobody" "/usr/bin/dhcpcount.rb" + Exec "nobody" "/usr/bin/dhcpcount.rb" "{{ timeout }}" {%- elif conf is mapping %} diff --git a/salt/collectd/dhcpcount.rb b/salt/collectd/dhcpcount.rb index 092b3e6..22062e8 100644 --- a/salt/collectd/dhcpcount.rb +++ b/salt/collectd/dhcpcount.rb @@ -3,6 +3,7 @@ require 'date' INTERVAL = 10 +TIMEOUT = ARGV[0].to_i hostname = `hostname`.strip STDOUT.sync = true @@ -12,22 +13,18 @@ loop do addr = nil starts = nil - ends = nil IO::readlines("/var/lib/dhcp/dhcpd.leases").each do |line| if line =~ /^lease (.+) \{/ addr = $1 starts = nil - ends = nil elsif line =~ /starts \d+ (.+?);/ - starts = DateTime.parse $1 - elsif line =~ /ends \d+ (.+?);/ - ends = DateTime.parse $1 + starts = DateTime.parse($1).to_time elsif line =~ /^\}/ - now = DateTime.now - if starts and ends and - starts <= now and ends >= now and + now = Time.now + if starts and + now >= starts and now < starts + TIMEOUT unless seen[addr] count += 1