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> </Plugin>
{%- elif plugin == 'dhcpcount' and conf %} {%- 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 LoadPlugin exec
<Plugin exec> <Plugin exec>
Exec "nobody" "/usr/bin/dhcpcount.rb" Exec "nobody" "/usr/bin/dhcpcount.rb" "{{ timeout }}"
</Plugin> </Plugin>
{%- elif conf is mapping %} {%- elif conf is mapping %}

View File

@ -3,6 +3,7 @@
require 'date' require 'date'
INTERVAL = 10 INTERVAL = 10
TIMEOUT = ARGV[0].to_i
hostname = `hostname`.strip hostname = `hostname`.strip
STDOUT.sync = true STDOUT.sync = true
@ -12,22 +13,18 @@ loop do
addr = nil addr = nil
starts = nil starts = nil
ends = nil
IO::readlines("/var/lib/dhcp/dhcpd.leases").each do |line| IO::readlines("/var/lib/dhcp/dhcpd.leases").each do |line|
if line =~ /^lease (.+) \{/ if line =~ /^lease (.+) \{/
addr = $1 addr = $1
starts = nil starts = nil
ends = nil
elsif line =~ /starts \d+ (.+?);/ elsif line =~ /starts \d+ (.+?);/
starts = DateTime.parse $1 starts = DateTime.parse($1).to_time
elsif line =~ /ends \d+ (.+?);/
ends = DateTime.parse $1
elsif line =~ /^\}/ elsif line =~ /^\}/
now = DateTime.now now = Time.now
if starts and ends and if starts and
starts <= now and ends >= now and now >= starts and now < starts + TIMEOUT
unless seen[addr] unless seen[addr]
count += 1 count += 1