Skip to content

Instantly share code, notes, and snippets.

@charlie-haley
Last active March 4, 2024 13:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save charlie-haley/aa9d16dc281f10eaed956637ce722297 to your computer and use it in GitHub Desktop.
Save charlie-haley/aa9d16dc281f10eaed956637ce722297 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/bash
# pkg install bash
# add to shellcmd as "/root/push_ip_metrics.sh &" via Services/shellcmd
pushgatewayip="192.168.1.2:9091"
network="192.168.1.0/24"
interface="bce1"
interval="1"
rate -i "${interface}" -r "${interval}" -e -n -Ab -a 255 -c "${network}" -d | while read -r line; do
host=`echo "${line}" | cut -d':' -f1`
downloadrate=`echo "${line}" | cut -d':' -f4`
uploadrate=`echo "${line}" | cut -d':' -f5`
if [ "${line}" == "- - - - - - - - - - - - - - - - - - -" ]; then
continue
fi
if [ ! -z "${downloadrate}" -a "${downloadrate}" != " " ]; then
echo "download_rate_bits ${downloadrate}" | curl --data-binary @- "${pushgatewayip}/metrics/job/pfsense/instance/${host}"
fi
if [ ! -z "${uploadrate}" -a "$uploadrate" != " " ]; then
echo "upload_rate_bits ${uploadrate}" | curl --data-binary @- "${pushgatewayip}/metrics/job/pfsense/instance/${host}"
fi
done
@benisai
Copy link

benisai commented Oct 8, 2021

How do I use this to get metrics to Grafana? This looks great, but I dont know where to start. Can you help me out?

@charlie-haley
Copy link
Author

How do I use this to get metrics to Grafana? This looks great, but I dont know where to start. Can you help me out?

You'll have to host Prometheus and Pushgateway then configure Prometheus as a data source in Grafana.

Configure the variables at the top of the script (pushgatewayip can also be a hostname), you can also adjust the interval if you don't want the script to be as aggressive. Then you can just run the script on your pfsense box, I just put mine inside /usr/local/etc/rc.d/ and made it executable with chmod, some more docs on that here

@benisai
Copy link

benisai commented Jan 18, 2022

Anyway to have the IP resolve to a hostname and then push to Prometheus? It would be nice to have the client hostname instead of the IP show in grafana.

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