collectd: fix dhcpcount

This commit is contained in:
Astro 2020-05-15 18:36:48 +02:00
parent 5162a6b985
commit 4b90cde92a
1 changed files with 29 additions and 21 deletions

View File

@ -4,14 +4,22 @@ require 'date'
INTERVAL = 10 INTERVAL = 10
hostname = `hostname`.strip hostname = `hostname`.strip
STDOUT.sync = true
loop do loop do
starts = nil seen = {}
ends = nil
count = 0 count = 0
starts = nil
ends = nil
ignore = false
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
ignore = seen[addr]
seen[addr] = true
starts = nil starts = nil
ends = nil ends = nil
elsif line =~ /starts \d+ (.+?);/ elsif line =~ /starts \d+ (.+?);/
@ -20,8 +28,8 @@ loop do
ends = DateTime.parse $1 ends = DateTime.parse $1
elsif line =~ /^\}/ elsif line =~ /^\}/
now = DateTime.now now = DateTime.now
if starts and ends and if (not ignore) and starts and ends and
starts <= now and ends >= now starts <= now and ends >= now and
count += 1 count += 1
end end
end end