DD-WRT Internet Usage Script

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

DD-WRT Internet Usage Script

Postby ben53252642 » Saturday 21 January 2017 23:34

Linux Bash script for getting the monthly upload and download info from DD-WRT and loading it into custom sensor Domoticz devices:

Can set your own icons (I have nice up and down arrows). 8-)

Make sure you have bc installed, for Debian based systems the command to install it is usually: apt-get install bc

Screen Shot 2017-01-22 at 8.30.06 am.png
Screen Shot 2017-01-22 at 8.30.06 am.png (110.54 KiB) Viewed 580 times


Screen Shot 2017-01-22 at 8.30.21 am.png
Screen Shot 2017-01-22 at 8.30.21 am.png (289.79 KiB) Viewed 580 times


Here's the startup.sh I use to loop the script in screen:

Code: Select all

# Start dd-wrt monitor
/usr/bin/screen -S dd-wrtmonitor -d -m /root/dd-wrt/dd-wrt.sh


The script for getting data from DD-WRT and loading it into Domoticz devices via JSON every minute:

Code: Select all

#!/bin/bash
while true; do
# Get internet usage upload / download and load it into Domoticz
currentmonth=$(date +"%B %Y")
dayandyear=$(date +"%m-%Y")
incoming=$(curl -s "http://DDWRTUSERNAME:[email protected]/ttgraph.cgi?"$dayandyear"" | grep "$currentmonth" | tail -n 1 | awk '{print $4}')
incomingconvertedtoGB=$(echo "$incoming" / 1024 | bc -l | sed 's/\(\.[0-9][0-9]\)[0-9]*/\1/g')
outgoing=$(curl -s "http://DDWRTUSERNAME:[email protected]/ttgraph.cgi?"$dayandyear"" | grep "$currentmonth" | tail -n 1 | awk '{print $8}')
outgoingconvertedtoGB=$(echo "$outgoing" / 1024 | bc -l | sed 's/\(\.[0-9][0-9]\)[0-9]*/\1/g')
curl -s "http://DOMOTICZUSERNAME:[email protected]/json.htm?type=command&param=udevice&idx=377&svalue=${incomingconvertedtoGB}"
curl -s "http://DOMOTICZUSERNAME:[email protected]/json.htm?type=command&param=udevice&idx=378&svalue=${outgoingconvertedtoGB}"
echo "Download:" "$incomingconvertedtoGB" "GB" "Upload:" "$outgoingconvertedtoGB" "GB"
sleep 60
done


Remember to change the idx values to your Domoticz custom sensors that you want the data to go to.

User avatar
remb0
Posts: 752
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Postby remb0 » Sunday 22 January 2017 16:55

nice script! works great!

User avatar
LouiS22
Posts: 261
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: DD-WRT Internet Usage Script

Postby LouiS22 » Sunday 22 January 2017 17:57

Nice script! I suppose it's only for DD-WRT and not for OpenWRT, right?

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

Re: DD-WRT Internet Usage Script

Postby ben53252642 » Sunday 22 January 2017 18:00

LouiS22 wrote:Nice script! I suppose it's only for DD-WRT and not for OpenWRT, right?


Should be easily adaptable, can you send me a zipped copy of the html page in your router showing monthly downloads along with the exact URL in the router interface you got it from?

User avatar
LouiS22
Posts: 261
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: DD-WRT Internet Usage Script

Postby LouiS22 » Sunday 22 January 2017 18:09

ben53252642 wrote:
LouiS22 wrote:Nice script! I suppose it's only for DD-WRT and not for OpenWRT, right?


Should be easily adaptable, can you send me a zipped copy of the html page in your router showing monthly downloads along with the exact URL in the router interface you got it from?


Hey, thanks for the quick reply. Just found out that Openwrt has no such function by default, but I installed an app called vnstat. Will observe it and get back to you.

User avatar
emme
Posts: 305
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: DD-WRT Internet Usage Script

Postby emme » Monday 23 January 2017 11:07

Uh... you make my day!!! :o
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

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

Re: DD-WRT Internet Usage Script

Postby ben53252642 » Tuesday 31 January 2017 21:50

New alternate version which also calculates average bandwidth per minute for the WAN interface.

You need to set the IDX values for each device you create, for instance I have a custom virtual sensor in Domoticz for:

Internet Download
Internet Upload
WAN Down Mbps Avg Per Min
WAN Up Mbps Avg Per Min

You may also need to change the "routerinterface", mine is ppp0 for the WAN in DD-WRT.

Code: Select all

#!/bin/bash
while true; do

# Router  Configuration
routerip="192.168.0.1"
routerinterface="ppp0"
username="user"
password="pass"
timeperiod="60"

# Domoticz Configuration
domoticzserver="192.168.0.5"
domoticzport="80"
domoticzuser="user"
domoticzpass="pass"

echo "Fetching data from the router..."
# Average Bandwidth Mbps
data1=$(curl -s -u "$username":"$password" "http://{$routerip}/fetchif.cgi?{$routerinterface}" | grep "$routerinterface")
sleep "$timeperiod"
data2=$(curl -s -u "$username":"$password" "http://{$routerip}/fetchif.cgi?{$routerinterface}" | grep "$routerinterface")
rxbytes1=$(echo "$data1" | awk '{print $2}')
rxbytes2=$(echo "$data2" | awk '{print $2}')
rxbytes=$(echo "$rxbytes2 - $rxbytes1" | bc -l)
rxbits=$(echo "$rxbytes * 8" | bc -l)
rxbitspersecond=$(echo "$rxbits / $timeperiod" | bc -l)
rxmegabits=$(echo "scale=2; $rxbitspersecond / 1048576" | bc -l)
txbytes1=$(echo "$data1" | awk '{print $10}')
txbytes2=$(echo "$data2" | awk '{print $10}')
txbytes=$(echo "$txbytes2 - $txbytes1" | bc -l)
txbits=$(echo "$txbytes * 8" | bc -l)
txbitspersecond=$(echo "$txbits / $timeperiod" | bc -l)
txmegabits=$(echo "scale=2; $txbitspersecond / 1048576" | bc -l)

# Internet Usage
currentmonth=$(date +"%B %Y")
dayandyear=$(date +"%m-%Y")
data3=$(curl -s -u "$username":"$password" "http://{$routerip}/ttgraph.cgi?{$dayandyear}" | grep "$currentmonth" | tail -n 1)
incoming=$(echo "$data3" | awk '{print $4}')
incomingconvertedtoGB=$(echo "$incoming" / 1024 | bc -l | sed 's/\(\.[0-9][0-9]\)[0-9]*/\1/g')
outgoing=$(echo "$data3" | awk '{print $8}')
outgoingconvertedtoGB=$(echo "$outgoing" / 1024 | bc -l | sed 's/\(\.[0-9][0-9]\)[0-9]*/\1/g')

# Display Results
echo "Average Bandwidth -" "RX:" "$rxmegabits""Mbps" "TX:" "$txmegabits""Mbps"
echo "Internet Usage -" "Download:" "$incomingconvertedtoGB" "GB" "Upload:" "$outgoingconvertedtoGB" "GB"

# Load results into Domoticz
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=377&svalue=${incomingconvertedtoGB}"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=378&svalue=${outgoingconvertedtoGB}"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=503&svalue=${rxmegabits}"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=502&svalue=${txmegabits}"

done


Screen Shot 2017-02-01 at 6.43.33 am.png
Screen Shot 2017-02-01 at 6.43.33 am.png (173.91 KiB) Viewed 416 times

User avatar
remb0
Posts: 752
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Postby remb0 » Thursday 09 February 2017 20:49

Hi ben how can I see what's my wan interface?
eth0 is my wan port assignment is that right?


it worked a month but my router hasn't data in februari.. but before everything is fine. :(

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

Re: DD-WRT Internet Usage Script

Postby ben53252642 » Friday 10 February 2017 5:48

remb0 wrote:Hi ben how can I see what's my wan interface?
eth0 is my wan port assignment is that right?


it worked a month but my router hasn't data in februari.. but before everything is fine. :(


Hi remb0, you can identify the name of the wan interface by looking at this page on your DD-WRT router, change the IP to whichever your router is at:

Code: Select all

http://192.168.0.1/Status_Bandwidth.asp


In regards to no data in February, it's possible you are using a different date format to what we use in Australia. You will want to check the output of these commands in the linux terminal and see if they match your date format (and alter if necessary):

date +"%B %Y"
date +"%m-%Y"

See here in getting the data from the router, it uses the current date as a variable

http://{$routerip}/ttgraph.cgi?{$dayandyear}

You can see how they are used in the script.

It's also possible your script isn't running automatically on system boot or isn't started?

Cheers

User avatar
remb0
Posts: 752
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Postby remb0 » Saturday 11 February 2017 14:37

thanks ben! but I think the problem is not in the script. see my screens
januari.png
januari.png (22.36 KiB) Viewed 246 times
2017-02-11 13_34_48-Router (build 31277) - WAN Status.png
2017-02-11 13_34_48-Router (build 31277) - WAN Status.png (17.92 KiB) Viewed 246 times


strange huh? Can't find the reason.

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

Re: DD-WRT Internet Usage Script

Postby ben53252642 » Saturday 11 February 2017 14:50

First thing that comes to mind is check the output of the commands:

date +"%B %Y"
date +"%m-%Y"

I'm guessing language difference? The script does a string match for the date on the DD-WRT page, if there is any variance in spelling or format it will not be able to fetch the data. Script is written assuming english language DD-WRT and Domoticz setup.

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

Re: DD-WRT Internet Usage Script

Postby ben53252642 » Saturday 11 February 2017 14:54

Ahh yes, just saw your second screen... Looks to be a problem in the router.

Bigted
Posts: 29
Joined: Friday 12 December 2014 15:42
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: DD-WRT Internet Usage Script

Postby Bigted » Saturday 11 February 2017 17:21

Thanks for this, the hardest part was to get the screen command working to keep it running in the background

User avatar
emme
Posts: 305
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: DD-WRT Internet Usage Script

Postby emme » Wednesday 15 February 2017 15:12

Got the script and works as it is suppesed... except for the 'grep' part that looks exactely for the string pattern...
the issue is that in Italian, on the pi3, the month name is all lowercase, while it's Capital Letters on the ddrt...

so I had to add a line to convert into capitals the month string:
after line:

Code: Select all

   currentmonth=$(date +"%B %Y")

add

Code: Select all

   currentmonth="${currentmonth[@]^}"

now it works fine!

ciao
M
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

Failure404
Posts: 5
Joined: Sunday 24 July 2016 15:28
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: DD-WRT Internet Usage Script

Postby Failure404 » Thursday 23 February 2017 23:20

Hi there,
thanks alot for the script.
I am pretty much a noob on Linux.
I removed the looping part in the script and have it run as a cronjob every N minute. Seems to be working ok.
Is there any disadvantage to it?

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

Re: DD-WRT Internet Usage Script

Postby ben53252642 » Friday 24 February 2017 0:10

Failure404 wrote:Hi there,
thanks alot for the script.
I am pretty much a noob on Linux.
I removed the looping part in the script and have it run as a cronjob every N minute. Seems to be working ok.
Is there any disadvantage to it?


Not significantly, but it will re-read the file each time the cron job runs which will be a very small extra load on your hard disk / ssd (whatever storage device you are using).

Looping the script avoids reading it from disk every x minutes.

To answer your question though, I don't think its worth changing it if you have it working, the difference is so small.


Return to “bash / powershell / bat etc.”

Who is online

Users browsing this forum: No registered users and 1 guest