This is a bash script I wrote for getting the last minutes bandwidth information from pfsense and loading it into Domoticz.
Steps:
1) Go to this url on your pfsense box: https://pfsenseip/services_snmp.php
2) Tick the box to enable snmp and set the "Read Community String" to: "public" then scroll down and bind it to the LAN interface and press save.
3) Install requirements on your Domoticz server: apt-get install snmp bc
3) Fill in everything in the script except for the "waninterfacename"
3) Run the script like this: "./update.sh list" and you will get an output looking something like this:
vtnet0
vtnet1
enc0
lo0
pflog0
pfsync0
pppoe0
ovpns1
In this case my wan interface is called pppoe0 so what is what I would enter in "waninterfacename" in the script (go ahead and fill the script with the name of your wan interface).
update.sh
Code: Select all
#!/bin/bash
# Pfsense Configuration
pfsenseip="192.168.0.1"
snmpid="public"
snmpversion="2c"
waninterfacename="pppoe0"
measuretime="60" # If you want avg mbps per minute, this would be 60
# Domoticz Configuration
domoticzserver="192.168.0.5"
domoticzport="443"
domoticzuser="username"
domoticzpass="password"
# Identify the WAN interface
interfacelist=$(snmpwalk -c "$snmpid" -v "$snmpversion" "$pfsenseip" .1.3.6.1.2.1.2.2.1.2)
wansnmpid=$(echo "$interfacelist" | grep "$waninterfacename" | awk '{ print $1 }' | grep -o '.\{1\}$')
# Get list of interfaces and display them in terminal
if [[ $1 == 'list' ]]; then
echo "$interfacelist" | awk '{ print $4 }' | sed 's/\"//g'
exit
fi
# Start work loop
while true; do
# Notify terminal
echo && echo "Interface: $waninterfacename"
echo "Calculating bandwidth per $measuretime seconds in mbps... (wait $measuretime seconds)"
# Get data
waninkb=$(snmpwalk -c "$snmpid" -v "$snmpversion" "$pfsenseip" .1.3.6.1.2.1.2.2.1.10."$wansnmpid" | awk '{ print $4 }')
wanoutkb=$(snmpwalk -c "$snmpid" -v "$snmpversion" "$pfsenseip" .1.3.6.1.2.1.2.2.1.16."$wansnmpid" | awk '{ print $4 }')
# Wait time period
sleep "$measuretime"
# Get data a second time
waninkb2=$(snmpwalk -c "$snmpid" -v "$snmpversion" "$pfsenseip" .1.3.6.1.2.1.2.2.1.10."$wansnmpid" | awk '{ print $4 }')
wanoutkb2=$(snmpwalk -c "$snmpid" -v "$snmpversion" "$pfsenseip" .1.3.6.1.2.1.2.2.1.16."$wansnmpid" | awk '{ print $4 }')
# Do math for Mbps
inbits=$(echo $((("$waninkb2"-"$waninkb") / 60 * 8)) | bc -l)
inmegabits=$(echo "scale=2; $inbits / 1000000" | bc -l)
outbits=$(echo $((("$wanoutkb2"-"$wanoutkb") / 60 * 8)) | bc -l)
outmegabits=$(echo "scale=2; $outbits / 1000000" | bc -l)
# Check data viability
if (( $(echo "$inbits > 2" |bc -l) )) && (( $(echo "$outbits > 2" |bc -l) )); then
# Notify Terminal 2
echo "In: $inmegabits Mbps"
echo "Out: $outmegabits Mbps"
# Send data to Domoticz
curl --max-time 60 -s -k "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command¶m=udevice&idx=503&svalue=${inmegabits}"
curl --max-time 60 -s -k "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command¶m=udevice&idx=502&svalue=${outmegabits}"
# Report if there was a problem
else
echo "There was a problem getting the data"
fi
done
Code: Select all
# Start pfsense monitor
/usr/bin/screen -S pfsense-monitor -d -m /root/pfsense/update.sh