pfSense Internet Usage and Bandwidth

All kinds of 'OS' scripts
ben53252642
Posts: 166
Joined: Saturday 02 July 2016 5:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

pfSense Internet Usage and Bandwidth

Postby ben53252642 » Saturday 25 February 2017 2:45

Hey Folks,

This is a Linux bash script I wrote for getting the monthly internet usage and last minutes bandwidth into Domoticz (updates every minute).

Steps for pfSense version "2.3.3-RELEASE":

1) Make sure you have the following packages installed (you can look in "Package Manager" under the "System" tab up top):
"Status_Traffic_Totals"
"darkstat"

2) Configure Darkstat at this address: https://pfsenseip/pkg_edit.php?xml=darkstat.xml
Tick enable, set capture interface to WAN and Web Interface Binding to LAN, set Local Network to LAN

3) Configure Traffic Totals at this address: https://pfsenseip/status_traffic_totals.php
Click, "Display Advanced", make sure only WAN is highlighted next to Options then press "Save As Defaults", make sure Graphing is enabled.

4) Configure the options in the BASH script and run it on your Domoticz server.

Code: Select all

#!/bin/bash
while true; do

# Requirements (for Debian / Ubuntu and Raspbian based systems): apt-get install jq bc libxml2-utils

# pfsense configuration
pfsenseipaddress="IPADDRESS"
username="USERNAME"
password="PASSWORD"

# Domoticz Configuration
domoticzserver="IPADDRESS"
domoticzport="PORT"
domoticzuser="USERNAME"
domoticzpass="PASSWORD"

# Get Monthly Internet Usage Data
login1=$(curl -Ss --insecure --cookie-jar /tmp/pfsensecookie "https://${pfsenseipaddress}" | sed -n 's/.*name=.__csrf_magic. value="\([^"]*\)".*/\1/p')
login2=$(curl -Ss --insecure --cookie-jar /tmp/pfsensecookie --cookie /tmp/pfsensecookie --location --data "login=Login&usernamefld=$username&passwordfld=$password&__csrf_magic=$login1" "https://${pfsenseipaddress}")
trafficdata=$(curl -s --insecure --cookie-jar /tmp/pfsensecookie --cookie /tmp/pfsensecookie "__csrf_magic=$login2" "https://${pfsenseipaddress}/vnstat_fetch_json.php" | jq '.interfaces[0].traffic.months')
echo "$trafficdata" | grep '"id":' | sed -e s/[^0-9]//g | while read line ; do
month=$(echo "$trafficdata" | jq ".[$line].date.month")
year=$(echo "$trafficdata" | jq ".[$line].date.year")
currentmonth=$(date +%m | bc -l)
currentyear=$(date +%Y)
localmonthdate=$(echo "$currentmonth" "$currentyear")
pfsensemonthdate=$(echo "$month" "$year")
if [ "$localmonthdate" == "$pfsensemonthdate" ]; then
rx=$(echo "$trafficdata" | jq ".[$line].rx")
rxgb=$(echo "scale=2; $rx / 1048576" | bc -l)
tx=$(echo "$trafficdata" | jq ".[$line].tx")
txgb=$(echo "scale=2; $tx / 1048576" | bc -l)
echo "Internet Usage -" "Download:" "$rxgb" "GB" "Upload:" "$txgb" "GB"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=377&svalue=${rxgb}"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=378&svalue=${txgb}"
fi
done

# Get bandwidth usage from darkstat and load it into Domoticz
bandwidthdata=$(xmllint --xpath //seconds "http://${pfsenseipaddress}:666/graphs.xml")
inbytes=$(echo "$bandwidthdata" | awk '{print $3}' | sed -e s/[^0-9]//g | awk '{s+=$1} END {printf "%.0f", s}')
inbytesavg=$(echo "$inbytes / 60" | bc -l)
inbits=$(echo "$inbytesavg * 8" | bc -l)
inmegabits=$(echo "scale=2; $inbits / 1048576" | bc -l)
outbytes=$(echo "$bandwidthdata" | awk '{print $4}' | sed -e s/[^0-9]//g | awk '{s+=$1} END {printf "%.0f", s}')
outbytesavg=$(echo "$outbytes / 60" | bc -l)
outbits=$(echo "$outbytesavg * 8" | bc -l)
outmegabits=$(echo "scale=2; $outbits / 1048576" | bc -l)
echo "Average Bandwidth -" "RX:" "$inmegabits""Mbps" "TX:" "$outmegabits""Mbps"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=503&svalue=${inmegabits}"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=502&svalue=${outmegabits}"

sleep 60
done


This is the startup.sh script I use for running it in the background:

Code: Select all

# Start pfsense monitor
/usr/bin/screen -S pfsense-monitor -d -m /root/pfsense/update.sh


Screen Shot 2017-02-01 at 6.43.33 am (1).png
Screen Shot 2017-02-01 at 6.43.33 am (1).png (173.91 KiB) Viewed 508 times
Last edited by ben53252642 on Wednesday 22 March 2017 18:42, edited 1 time in total.

User avatar
Varazir
Posts: 162
Joined: Friday 20 February 2015 23:23
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: pfSense Internet Usage and Bandwidth

Postby Varazir » Sunday 12 March 2017 11:33

Great script but wouldn't be good to set the IDX as variables too?

Abit howto setup the dummy devices too.

I added it to the crontab so I don't need to start the screen if I reboot my server.
Removed the while loop and sleep

Added custom icons
Attachments
domoticz_custom_icon_upload.zip
(4.36 KiB) Downloaded 39 times
domoticz_custom_icon_download.zip
(4.38 KiB) Downloaded 9 times
Last edited by Varazir on Sunday 12 March 2017 17:54, edited 1 time in total.
1x Raspberry PI A with a RaZberry board
1x GreenWave 6 port Power node
1x tellstick
1x Qubino ZMNHOD1 ( controlling a Ikea blinder )
1x ZW075 Smart Switch Gen5
Several nexa/proove devices

Live Long and Prosper \\//_

User avatar
Varazir
Posts: 162
Joined: Friday 20 February 2015 23:23
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: pfSense Internet Usage and Bandwidth

Postby Varazir » Sunday 12 March 2017 17:52

and this icons

They look like this

pfsense.JPG
pfsense.JPG (51.15 KiB) Viewed 380 times
Attachments
domoticz_custom_icon_upspeed.zip
(2.72 KiB) Downloaded 12 times
domoticz_custom_icon_downspeed.zip
(2.84 KiB) Downloaded 11 times
1x Raspberry PI A with a RaZberry board
1x GreenWave 6 port Power node
1x tellstick
1x Qubino ZMNHOD1 ( controlling a Ikea blinder )
1x ZW075 Smart Switch Gen5
Several nexa/proove devices

Live Long and Prosper \\//_

ben53252642
Posts: 166
Joined: Saturday 02 July 2016 5:17
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: pfSense Internet Usage and Bandwidth

Postby ben53252642 » Wednesday 29 March 2017 13:47

Awesome! Love the icons! :D

If anyone finds a simpler way to get the data out of pfsense please let me know.


Return to “bash / powershell / bat etc.”

Who is online

Users browsing this forum: No registered users and 1 guest