Post ARW95F9DVEcVxDAaVE by nihl@p.umbriel.fr
(DIR) More posts by nihl@p.umbriel.fr
(DIR) Post #ARW8RrEQUJnGUCpGaW by izaya@social.shadowkat.net
2023-01-11T00:54:41.368101Z
0 likes, 0 repeats
today I wrote a script to count the number of DHCP leases currently active on my network and output it for collectd
(DIR) Post #ARW95F9DVEcVxDAaVE by nihl@p.umbriel.fr
2023-01-11T00:57:18.799380Z
0 likes, 0 repeats
@izaya Are you parsing the leases file on the DHCP server?
(DIR) Post #ARW95FjjJTpLmSFkPo by izaya@social.shadowkat.net
2023-01-11T00:59:34.025942Z
0 likes, 0 repeats
@nihl yeah the script is super simple, though rather brittle and assumes you’re using /24 networks inside a /16 subnet#!/usr/bin/env lualocal hostname = os.getenv("COLLECTD_HOSTNAME") or os.getenv("HOSTNAME")local interval = os.getenv("COLLECTD_INTERVAL") or 10local totalCount = 0local subnetCounts = {}local f = io.open("/var/lib/misc/dnsmasq.leases","rb")for line in f:lines() do totalCount = totalCount + 1 local subnet = tonumber(line:match("10%.0%.(%d+)%.%d+")) subnetCounts[subnet] = (subnetCounts[subnet] or 0) + 1endprint(string.format('PUTVAL "%s/dhcp/gauge-dhcp_leases/total" interval=%d N:%i',hostname,interval,totalCount))for k,v in pairs(subnetCounts) do print(string.format('PUTVAL "%s/dhcp/gauge-dhcp_leases/%i" interval=%d N:%i',hostname,k,interval,v))end