Skip to content

Instantly share code, notes, and snippets.

@pletch
Last active September 9, 2020 16:43
Show Gist options
  • Save pletch/34d9e5a2d5ace9e51af88d117b123629 to your computer and use it in GitHub Desktop.
Save pletch/34d9e5a2d5ace9e51af88d117b123629 to your computer and use it in GitHub Desktop.
Scrape pf QOS metrics for forwarding to InfluxDB
#!/bin/sh
#
# Shell script: get_pfqueue_data.sh
# Date: 21-Dec-2016
#
# Script to scrape queue metrics from pfctl
# and output in influxdb line protocol with interface name &
# q name as tags and q bytes & q drops as data.
#
# Copy this script to a directory on your pfsense machine and ensure
# it is executable. It requires the privileges of a user with permission
# to read /dev/pf.
#
# This script should be invoked with the target influxdb measurement name
# as the single argument (e.g. 'get_pfqueue_data.sh pf_metrics' where
# pf_metrics is the measurement name)and is meant to be used in
# combination with telegraf inputs.exec plugin to forward stats to
# the influx server. The influxdb database name and retention policy
# are configured within telegraf.
#
# Suggest using telegraf output filtering to only forward data for queues
# for which metrics are wanted.
if test X"$1" = 'X'
then
echo "Error! No measurement name provided"
exit
fi
pfctl_output=$(pfctl -s queue -v)
dt=$(date +%s)
dt=$(($dt*1000000000))
qdrops() {
drops=$(echo $pfctl_output | grep -o -E 'dropped pkts: *[0-9]*' | sed -n -e 's/^.*pkts:[ \t]* //p' | sed -n "$4"p)
bytes=$(echo $pfctl_output | grep -o -E '\[ pkts: *[0-9]* *bytes: *[0-9]*' | sed -n -e 's/^.*bytes:[ \t]* //p' | sed -n "$4"p)
echo $1',interface='$2',queue='$3' drops='$drops'i,bytes='$bytes'i '$dt
}
index=1
for q in $(echo $pfctl_output | grep -o -E '[A-Za-z0-9_]* on' | cut -d ' ' -f1)
do
r=$(echo $q | grep -o -E 'root')
if test X"$r" = 'Xroot'
then
iface=$(echo $q | cut -d"_" -f2)
index=$(($index+1))
else
qdrops $1 $iface $q $index
index=$(($index+1))
fi
done
@ackstorm23
Copy link

I used this, but I threw in the following minor patch to also collect current qdepth

37c37,38
< echo $1',interface='$2',queue='$3' drops='$drops'i,bytes='$bytes'i '$dt

qdepth=$(echo $pfctl_output | grep -o -E '[ qlength: [0-9]' | sed -n -e 's/^.qlength:[ \t] //p' | sed -n "$4"p)
echo $1',interface='$2',queue='$3' drops='$drops'i,bytes='$bytes'i,qdepth='$qdepth'i '$dt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment