collectd/dhcpcount: properly calculate timeout

This commit is contained in:
Astro 2021-02-18 14:39:43 +01:00
parent 699adb37bb
commit f08a3e486e
2 changed files with 14 additions and 9 deletions

View File

@ -35,9 +35,17 @@ LoadPlugin ping
</Plugin>
{%- 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
<Plugin exec>
Exec "nobody" "/usr/bin/dhcpcount.rb"
Exec "nobody" "/usr/bin/dhcpcount.rb" "{{ timeout }}"
</Plugin>
{%- elif conf is mapping %}

View File

@ -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