pfSense Internet Usage and Bandwidth

All kinds of 'OS' scripts

Moderator: leecollings

User avatar
supergroover
Posts: 23
Joined: Friday 12 July 2013 19:37
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by supergroover »

I get this error and I really don't know why.
Can someone point me in the right direction?

Code: Select all

+ echo Interface: vmx0
Interface: vmx0
+ echo Calculating bandwidth per 60 seconds in mbps... (wait 60 seconds)
Calculating bandwidth per 60 seconds in mbps... (wait 60 seconds)
+ + snmpwalk -c public -v 2c 192.168.1.1 .1.3.6.1.2.1.2.2.1.10.1
awk { print $4 }
+ waninkb=4269204041
+ + awksnmpwalk { print $4 } -c
 public -v 2c 192.168.1.1 .1.3.6.1.2.1.2.2.1.16.1
+ wanoutkb=1721606739
+ sleep 60
+ + snmpwalkawk -c { print $4 } public
 -v 2c 192.168.1.1 .1.3.6.1.2.1.2.2.1.10.1
+ waninkb2=4271093885
+ + awksnmpwalk -c { print $4 } public
 -v 2c 192.168.1.1 .1.3.6.1.2.1.2.2.1.16.1
+ wanoutkb2=1721765333
+ + bc -l
echo 251976
+ inbits=251976
+ + bcecho -l scale=2; 251976 / 1000000

+ inmegabits=.25
+ + bcecho -l 21144

+ outbits=21144
+ + bcecho scale=2; 21144 / 1000000 -l

+ outmegabits=.02
+ + echobc -l 251976 > 2

+ 1
./update.sh: 57: ./update.sh: 1: not found
+ echo There was a problem getting the data
There was a problem getting the data
+ true
+ echo

+ echo
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by ben53252642 »

What is with all the +'s? How did you get that text?
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
supergroover
Posts: 23
Joined: Friday 12 July 2013 19:37
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by supergroover »

ben53252642 wrote: Thursday 17 January 2019 21:41 What is with all the +'s? How did you get that text?
I really don't know , just copied de text output from my ssh app .

Code: Select all

sh -x update.sh
I see those +'s also by https://www.domoticz.com/forum/viewtopi ... 7#p197087
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by ben53252642 »

supergroover wrote: Friday 18 January 2019 9:15

Code: Select all

sh -x update.sh
That's the problem right there, at the top of the script it tells your system to execute the script using the BASH command line processor.

You are manually over-riding that and causing the incompatibility by trying to start the script with the SH command line processor.

Try: ./update.sh

Note if you need to make the script executable (which is only needed a single time) use: chmod 755 update.sh
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
supergroover
Posts: 23
Joined: Friday 12 July 2013 19:37
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by supergroover »

ben53252642 wrote: Friday 18 January 2019 11:01
supergroover wrote: Friday 18 January 2019 9:15

Code: Select all

sh -x update.sh
That's the problem right there, at the top of the script it tells your system to execute the script using the BASH command line processor.

You are manually over-riding that and causing the incompatibility by trying to start the script with the SH command line processor.

Try: ./update.sh

Note if you need to make the script executable (which is only needed a single time) use: chmod 755 update.sh
Thank you for your help.

I also had to change:

Code: Select all

# 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)
In:

Code: Select all

# 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)
Do you know how i got the total download and upload per day?
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by ben53252642 »

Total usage per day is currently not a feature.

Only the current bandwidth utilization in Mbps.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Cletus
Posts: 54
Joined: Wednesday 17 August 2016 9:32
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Purmerend, Netherlands
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by Cletus »

Hi, I dove into this thing again as i still had the old script of you ben53252642 running but it was not working anymore. I made an update only for the total datatraffic used. Please see the script below. Change accordingly and it will work (at least for me)

Code: Select all

#!/bin/bash
# Requirements (for Debian / Ubuntu and Raspbian based systems): apt-get install jq bc libxml2-utils
#
# Send PFSense Monthly Upload / Download data to Domoticz
#
# Original script made by: ben53252642 on the forum of Domoticz
# URL: https://www.domoticz.com/forum/viewtopic.php?t=16297
#
# Adjusted to accomodate new cookie logins, domoticz username and password, easy idx configuration
# https login for Domoticz and PFSense (PFSense default 443 only)
# by Paul Bonnemaijers (Cletus) 30 oktober 2019

#
# Configuration of script
#

# pfsense configuration
pfsenseipaddress="x.x.x.x"
pfusername="user"
pfpassword="pass"

# Domoticz Configuration
domoticzserver="x.x.x.x"
domoticzport="443"
domoticzuser="user"
domoticzpass="pass"
rxidx="1"
txidx="2"

#
# Done configuring, don't change anything below here unless you are sure :)
#

# Clear Cookies and CSRF to be sure
function clear_cookies {
if [ -f cookies.txt ] && [ -f csrf.txt ]; then
rm cookies.txt csrf.txt
printf "\nCookies Cleared!\n"
fi
}

# Set Workingdir to script executiondir
cd "${0%/*}"

# Clearing Cookies
clear_cookies

# Get Monthly Internet Usage Data
# Login to PFSense
login1=$(curl --silent --insecure -L -k --cookie-jar cookies.txt \
             https://$pfsenseipaddress/ \
             | grep "name='__csrf_magic'" \
             | sed 's/.*value="\(.*\)".*/\1/' > csrf.txt)

login2=$(curl --silent --insecure -L -k --cookie cookies.txt --cookie-jar cookies.txt \
             --data-urlencode "login=Login" \
             --data-urlencode "usernamefld=$pfusername" \
             --data-urlencode "passwordfld=$pfpassword" \
             --data-urlencode "__csrf_magic=$(cat csrf.txt)" \
             https://$pfsenseipaddress/ > /dev/null)

login3=$(curl --silent --insecure -L -k --cookie cookies.txt --cookie-jar cookies.txt \
             https://192.168.2.1/diag_backup.php  \
             | grep "name='__csrf_magic'"   \
             | sed 's/.*value="\(.*\)".*/\1/' > csrf.txt)

trafficdata=$(curl --silent --insecure -L -k --cookie cookies.txt --cookie-jar cookies.txt \
             --data-urlencode "__csrf_magic=$(head -n 1 csrf.txt)" \
             https://$pfsenseipaddress/vnstat_fetch_json.php | jq '.interfaces[0].traffic.months')

# Parse Traffic Data
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)

# Endresult
printf "Internet Usage - Download: $rxgb GB Upload: $txgb GB\n\n"

# Send to Domoticz
curl -s --insecure -u "$domoticzuser":"$domoticzpass" "https://{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=addlogmessage&message=Internet%20Usage%20-%20Download:%20$rxgb%20GB%20Upload:%20$txgb%20GB"
curl -s --insecure -u "$domoticzuser":"$domoticzpass" "https://{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=${rxidx}&svalue=${rxgb}"
curl -s --insecure -u "$domoticzuser":"$domoticzpass" "https://{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=${txidx}&svalue=${txgb}"
fi

# Clearing Cookies Again
clear_cookies

done
This is our world now... the world of the electron and the switch, the beauty of the baud.
tjabas
Posts: 564
Joined: Sunday 11 December 2016 13:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by tjabas »

Do i install this plugin in domoticz/plugins?
benedict
Posts: 16
Joined: Thursday 16 December 2021 16:05
Target OS: Linux
Domoticz version:
Contact:

Re: pfSense Internet Usage and Bandwidth

Post by benedict »

ben53252642 wrote: Friday 18 January 2019 13:26 Total usage per day is currently not a feature.
Is there anyone working on something for domoticz that does calculate totals per day, week, month out of an SNMP source?

I own a mikrotik router and it's incredible how hard it is to obtain this from anything out there. All people do is show current, max, average bandwidth per x time, but never total up and down data usage, while this is precisely what most people need to know, as ISPs all allow a max with fair-use policies thriving these days etc.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests