Skip to content

Instantly share code, notes, and snippets.

@ragingcomputer
Last active November 1, 2022 12:34
Show Gist options
  • Save ragingcomputer/bff7a6a810564e8784d5570c11f3ef7f to your computer and use it in GitHub Desktop.
Save ragingcomputer/bff7a6a810564e8784d5570c11f3ef7f to your computer and use it in GitHub Desktop.
Collect pfSense DHCP lease counts using telegraf and built-in tools

Collect pfSense DHCP lease counts using telegraf and built-in tools

Grafana Dashboard for pfSense highlighting DHCP Leases

Existing Collector

pfSense has the ability to collect DHCP lease statistics in it's Status -> Monitoring menu. That means it has some way to collect them.

searching through the source on github reveals dhcpd_gather_stats.php referenced in rrd.inc

pfSense runs this command to collect dhcpd lease counts from the lan interface: /usr/local/bin/php-cgi -q /usr/local/bin/dhcpd_gather_stats.php lan and this for the opt1 interface: /usr/local/bin/php-cgi -q /usr/local/bin/dhcpd_gather_stats.php opt1

Format Data

Sample output - N:21:42:116 - colon delimited data.

  • Column 1 is N or now, the time for rrdtool to use when inserting data
  • Column 2 is active leases
  • Column 3 is static leases
  • Column 4 is the size of the pool or range

input.exec in influx data format is most convenient way to pass this data: input.exec documentation and InfluxDB line protocol tutorial

Sample of desired output: dhcpd,hostname=firewall.domain.xyz,interface=lan active=21,static=42,range=116

dhcpd_gather_stats.php sets all outputs to NaN and updates them if it has valid data. NaN will cause the insert into InfluxDB to fail, so the first step is to replace the NaN with a 0 /usr/local/bin/php-cgi -q /usr/local/bin/dhcpd_gather_stats.php lan|sed s/nan/0/ig

This awk command captures the output from dhcpd_gather_stats into variable d, the output from hostname into variable h, splits d on colons into array a, then prints desired output awk 'BEGIN{"/usr/local/bin/php-cgi -q /usr/local/bin/dhcpd_gather_stats.php lan|sed s/nan/0/ig"|getline d;"/bin/hostname"|getline h; split(d,a,":");print "dhcpd," "hostname=" h ",interface=lan" " active=" a[2] ",static=" a[3] ",range=" a[4]}'

Add to pfSense

Escape the double quotes with a backslash \" put into inputs.exec block and paste into Additional configuration for Telegraf section

[[inputs.exec]]
   commands = [
     "awk 'BEGIN{\"/usr/local/bin/php-cgi -q /usr/local/bin/dhcpd_gather_stats.php lan|sed s/nan/0/ig\"|getline d;\"/bin/hostname\"|getline h; split(d,a,\":\");print \"dhcpd,\" \"hostname=\" h \",interface=lan\" \" active=\" a[2] \",static=\" a[3] \",range=\" a[4]}'"
   ]
   data_format = "influx"

pfSense Configuration - Additional configuration for Telegraf

Dashboard

I recommend starting with an existing dashboard like pfSense Firewall Dashboard by drcstang

Add a new panel, and use the dhcpd metric

[[inputs.exec]]
commands = [
"awk 'BEGIN{\"/usr/local/bin/php-cgi -q /usr/local/bin/dhcpd_gather_stats.php lan|sed s/nan/0/ig\"|getline d;\"/bin/hostname\"|getline h; split(d,a,\":\");print \"dhcpd,\" \"hostname=\" h \",interface=lan\" \" active=\" a[2] \",static=\" a[3] \",range=\" a[4]}'"
]
data_format = "influx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment