DD-WRT Internet Usage Script

All kinds of 'OS' scripts
Post Reply
ben53252642
Posts: 527
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

DD-WRT Internet Usage Script

Post by 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 5068 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 5068 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:DDWRTPASSWORD@192.168.0.1/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:DDWRTPASSWORD@192.168.0.1/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:DOMOTICZPASSWORD@192.168.0.5/json.htm?type=command&param=udevice&idx=377&svalue=${incomingconvertedtoGB}"
curl -s "http://DOMOTICZUSERNAME:DOMOTICZPASSWORD@192.168.0.5/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: 1002
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Post by remb0 » Sunday 22 January 2017 16:55

nice script! works great!

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

Re: DD-WRT Internet Usage Script

Post by LouiS22 » Sunday 22 January 2017 17:57

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

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

Re: DD-WRT Internet Usage Script

Post by 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: 497
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: DD-WRT Internet Usage Script

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

Re: DD-WRT Internet Usage Script

Post by emme » Monday 23 January 2017 11:07

Uh... you make my day!!! :o
The most dangerous phrase in any language is:
"We always done this way"

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

Re: DD-WRT Internet Usage Script

Post by 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 4904 times

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

Re: DD-WRT Internet Usage Script

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

Re: DD-WRT Internet Usage Script

Post by 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: 1002
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: DD-WRT Internet Usage Script

Post by 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 4734 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 4734 times
strange huh? Can't find the reason.

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

Re: DD-WRT Internet Usage Script

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

Re: DD-WRT Internet Usage Script

Post by ben53252642 » Saturday 11 February 2017 14:54

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

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

Re: DD-WRT Internet Usage Script

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

Re: DD-WRT Internet Usage Script

Post by 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
The most dangerous phrase in any language is:
"We always done this way"

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

Re: DD-WRT Internet Usage Script

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

Re: DD-WRT Internet Usage Script

Post by 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.

chesslin
Posts: 1
Joined: Wednesday 24 January 2018 15:49
Target OS: Linux
Domoticz version:
Contact:

Re: DD-WRT Internet Usage Script

Post by chesslin » Wednesday 24 January 2018 15:57

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

Re: DD-WRT Internet Usage Script

Post by ben53252642 » Monday 29 January 2018 23:28

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).

Wienen
Posts: 4
Joined: Wednesday 17 October 2018 13:32
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: DD-WRT Internet Usage Script

Post by Wienen » Saturday 20 October 2018 0:31

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

Re: DD-WRT Internet Usage Script

Post by ben53252642 » Sunday 21 October 2018 15:04

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest