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
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]}'
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"
I recommend starting with an existing dashboard like pfSense Firewall Dashboard by drcstang
Hi, my name is Marcos, I need help with this project. I have a pfsense and I wanted to show this pfsense DHCP lease count on my Grafana dashboard. How should I call the information in Grafana? Could you give me more details so I can implement it here in my project? Thank you very much.