DD-WRT Internet Usage Script

All kinds of 'OS' scripts

Moderator: leecollings

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

DD-WRT Internet Usage Script

Post by ben53252642 »

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 10216 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 10216 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.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Post by remb0 »

nice script! works great!
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: DD-WRT Internet Usage Script

Post by LouiS22 »

Nice script! I suppose it's only for DD-WRT and not for OpenWRT, right?
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

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?
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
LouiS22
Posts: 433
Joined: Friday 27 February 2015 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: DD-WRT Internet Usage Script

Post by LouiS22 »

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: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: DD-WRT Internet Usage Script

Post by emme »

Uh... you make my day!!! :o
The most dangerous phrase in any language is:
"We always done this way"
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

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 10053 times
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Post by remb0 »

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: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

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
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Post by remb0 »

thanks ben! but I think the problem is not in the script. see my screens
januari.png
januari.png (22.36 KiB) Viewed 9883 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 9883 times
strange huh? Can't find the reason.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

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.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

Ahh yes, just saw your second screen... Looks to be a problem in the router.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Bigted
Posts: 68
Joined: Friday 12 December 2014 14:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: DD-WRT Internet Usage Script

Post by Bigted »

Thanks for this, the hardest part was to get the screen command working to keep it running in the background
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: DD-WRT Internet Usage Script

Post by emme »

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
The most dangerous phrase in any language is:
"We always done this way"
Failure404
Posts: 15
Joined: Sunday 24 July 2016 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DD-WRT Internet Usage Script

Post by Failure404 »

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: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

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.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
chesslin
Posts: 1
Joined: Wednesday 24 January 2018 14:49
Target OS: Linux
Domoticz version:
Contact:

Re: DD-WRT Internet Usage Script

Post by chesslin »

Hi Guys

I get this error.... :(


Fetching data from the router...
(standard_in) 1: syntax error
(standard_in) 1: syntax error
Average Bandwidth - RX: 4.43Mbps TX: .24Mbps
Internet Usage - Download: GB Upload: GB
{
"status" : "ERR"
}
{
"status" : "ERR"
}
{
"status" : "OK",
"title" : "Update Device"
}
{
"status" : "OK",
"title" : "Update Device"
}
Fetching data from the router...



Any idea what I have done wrong?
I have not really changed anything, just copy/paste, Im geting the average numbers so the communikation is working...

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

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

chesslin, I'm guessing that your running the script via something like: sh script.sh

Instead try running it via /path/to/script.sh or ./script.sh

If that doesn't work, please provide as much detail as possible in your reply (eg what OS you are using).
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Wienen
Posts: 5
Joined: Wednesday 17 October 2018 13:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DD-WRT Internet Usage Script

Post by Wienen »

I really like your scripts but unfortunately i'm fairly new to Domoticz/Linux and i'm having some trouble installing your script.

I'm on a RPi3b+ with Raspbian strech Lite (updated)
I'm starting the startup.sh script from a cronjob (@reboot)
I'm getting the following error "-bash: /usr/bin/screen: No such file or directory"
Update: In the meanwhile this is fixed. After installing Screen the error disappeared. When viewing the Cron log there are no errors so it seems (i guess) the script runs in loop now?

Unfortunatly the output isn't generated. I have no idea where to look for logs?

When i manually try to run the dd-wrt.sh (bash dd-wrt.sh) i'm getting the following error;
dd-wrt.sh: line 54: syntax error near unexpected token `done'
dd-wrt.sh: line 54: `done'
Update: After allot of Googling i found the problem occurred because my script was made with Notepad++ on Windows. The character set was set to CR FL (Windows) instead of FL (Unix). This can be changed by r.click bottom right in Notepad++ (mostly Windows) and then select Unix and save the file. After this all worked fine for me :D

I've created 1 dummy "hardware" and from that specific dummy i created 4 virtual sensors. Do i need to create a dummy "hardware" for each virtual sensor or is it ok the way i created it?
Update: It seems this doesn't matter. 1 dummy hardware was enough for me. Creating 4 virtual sensors with this same Dummy hardware

Hopefully someone wants to help me out.
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: DD-WRT Internet Usage Script

Post by ben53252642 »

Try running the script manually from terminal: ./script.sh

Obviously you need to name the script as script.sh but if you run it like that in terminal it should show you output to determine if it's working or not.

The design is such that it's not meant to be run from CRON but started once at startup and it keeps running in the background in the screen session.

You would view it by typing: screen -x and de-attach from it again by using ctrl + a + d

To make it run on startup, assuming you put the script in /scripts/script.sh for example

1) nano /etc/init.d/ddwrt
2) paste these contents:

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides: ddwrt
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description: ddwrt
### END INIT INFO

case "$1" in
'start')
        cd /scripts
        sudo -u root /usr/bin/screen -S ddwrt -d -m /scripts/script.sh
        ;;
'stop')
        sudo -u root pkill -f "ddwrt"
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac
exit 0
3) chmod 755 /etc/init.d/ddwrt
4) update-rc.d ddwrt defaults

Make sure you change "root" to whichever user account it should run under (eg pi), personally mine uses root.

Reboot and type screen -x at terminal, you should see it running in the background.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest