Bandwidth Reporting Script based on your true Billing Month

Post new topic   Reply to topic    DD-WRT Forum Forum Index -> Contributions Upload
Author Message
tangled111
DD-WRT Novice


Joined: 31 Jan 2011
Posts: 7

PostPosted: Sat Feb 05, 2011 6:26    Post subject: Bandwidth Reporting Script based on your true Billing Month Reply with quote
This is my first adventure in router scripting. I Only discovered and implemented DD-WRT on my new router a week ago.

I created the script for and tested with DD-WRT v24-sp2 (08/07/10) std (SVN revision 14896). The script does NOT work with the mini version (not sure about others). It is built on top of script "Email_Bandwidth_Usage_Daily" from http://www.dd-wrt.com/wiki/index.php/Useful_Scripts.

Program uses DD-WRT Traffic data to supply bandwidth usage for your billing month.

Usage: <program> <billing start day> <display choice>
billing start day must be a day from 1-28
display choice - optional parameter - "GB" to display monthly #s in GB, default is MB
Examples:
/bin/sh email_monthly_bandwidth.sh 28 GB
/bin/sh email_monthly_bandwidth.sh 1

after running this >/bin/sh email_monthly_bandwidth.sh 28

The program will send an email with the following report with Bandwidth remaining, usage for monthly billing period to date and yesterday's total:
-------------------
Bandwidth Remaining: 167661 MB
Days left in period: 22

Totals for Period Starting 1/28/2011
Incoming: 35861 MB
Outgoing: 1278 MB
Total : 37139 MB

Totals for 2/4/2011
Incoming: 2231 MB
Outgoing: 95 MB
Total : 2326 MB
-------------------

You will need to update 2 things.
The sendmail command in fcn_mail needs to reflect your ISP's SMTP server and sending address as well as the recipient email.
Secondly there is a variable MONTHLY_CAP that is currently set to 204800 (200GB cap). This is in MB.

I have accounted for all date logic including leap years. I have not tested all scenarios so there may still be bugs left.

Calculating in this OS is limited to integers so using GB for bandwidth takes away some accuracy. If there is a way around this let me know. Because of the integer limitation I used MB in yesterday's totals.

Let me know what you think!

Edit Feb 8th 2011
New release, ver 0.2
Had to change zero stripping logic when running
date commands. 08 and 09 do not work with "let"
commands. 07 and below are fine...Weird!


Last edited by tangled111 on Sun Mar 27, 2011 12:12; edited 1 time in total
Sponsor
tangled111
DD-WRT Novice


Joined: 31 Jan 2011
Posts: 7

PostPosted: Mon Feb 21, 2011 21:44    Post subject: Version 0.3 Reply with quote
Hi,

I changed the script again to my preferences and needs. <NOTE: You need to register to see attached script!>

I changed the report a bit. Took out yesterday's totals because I really don't care about that and I added %.
The report that is emailed now looks like this:
Quote:

Total Monthly Bandwidth: 204800 MB
Bandwidth Remaining: 73888 MB or 36%
Days left in period: 6

Totals for Period Starting 1/28/2011
Incoming: 125829 MB
Outgoing: 5083 MB
Total : 130912 MB


I have also changed the frequency of the email reports. I have the script scheduled to run once a day but I got tired of receiving daily emails.

Now the report only emails on specific trigger points.
It will email once when bandwidth remaining goes under 75%, then once under 50%, once under 25%, and then every time executed once below 10%.

I left version 0.2 in case you prefer the older version.
Enjoy!


Last edited by tangled111 on Sun Feb 27, 2011 1:24; edited 1 time in total
s_m_turner
DD-WRT Novice


Joined: 23 Feb 2011
Posts: 5

PostPosted: Wed Feb 23, 2011 10:51    Post subject: Email bandwidth report on a threshold/cap Reply with quote
Hi tangled111

I'm trying to create a script that will Email me on the 28th of each month, if the bandwidth has reached on gone over a set limit.

e.g if the total bandwidth = or > 10GB

Any help would be appreciated.

Cheers Smile
tangled111
DD-WRT Novice


Joined: 31 Jan 2011
Posts: 7

PostPosted: Wed Feb 23, 2011 15:37    Post subject: Re: Email bandwidth report on a threshold/cap Reply with quote
s_m_turner wrote:
Hi tangled111
I'm trying to create a script that will Email me on the 28th of each month, if the bandwidth has reached on gone over a set limit.
e.g if the total bandwidth = or > 10GB
Any help would be appreciated.
Cheers Smile

To have the script email you only on the 28th setup the cron to run the job on the 28th of every month.

To have the script email you on the condition that total bandwidth >= 10240 MB modify version 0.3 of my script by replacing lines 295-324 with the following:
Quote:
##Threshold value is in MB
threshold=10240
if [ $billMonthTotal -ge $threshold ]
then
fnc_mail "Bandwidth Report" "$msg" "/tmp/bwmail.log"
fi
s_m_turner
DD-WRT Novice


Joined: 23 Feb 2011
Posts: 5

PostPosted: Wed Feb 23, 2011 16:41    Post subject: Reply with quote
Thanks tangled111

I'll give this script a whirl, many thanks again.

Steve Laughing Very Happy
gdlgiii
DD-WRT Novice


Joined: 26 Mar 2011
Posts: 22

PostPosted: Sat Mar 26, 2011 18:12    Post subject: Re: Bandwidth Reporting Script based on your true Billing Mo Reply with quote
But how do you upload the script into the router? How is it called? Via CRON job? Is this automated??


tangled111 wrote:
This is my first adventure in router scripting. I Only discovered and implemented DD-WRT on my new router a week ago.

I created the script for and tested with DD-WRT v24 preSP2 [Beta] 14896. It is built on top of script "Email_Bandwidth_Usage_Daily" from http://www.dd-wrt.com/wiki/index.php/Useful_Scripts.

Program uses DD-WRT Traffic data to supply bandwidth usage for your billing month.

Usage: <program> <billing start day> <display choice>
billing start day must be a day from 1-28
display choice - optional parameter - "GB" to display monthly #s in GB, default is MB
Examples:
/bin/sh email_monthly_bandwidth.sh 28 GB
/bin/sh email_monthly_bandwidth.sh 1

after running this >/bin/sh email_monthly_bandwidth.sh 28

The program will send an email with the following report with Bandwidth remaining, usage for monthly billing period to date and yesterday's total:
-------------------
Bandwidth Remaining: 167661 MB
Days left in period: 22

Totals for Period Starting 1/28/2011
Incoming: 35861 MB
Outgoing: 1278 MB
Total : 37139 MB

Totals for 2/4/2011
Incoming: 2231 MB
Outgoing: 95 MB
Total : 2326 MB
-------------------

You will need to update 2 things.
The sendmail command in fcn_mail needs to reflect your ISP's SMTP server and sending address as well as the recipient email.
Secondly there is a variable MONTHLY_CAP that is currently set to 204800 (200GB cap). This is in MB.

I have accounted for all date logic including leap years. I have not tested all scenarios so there may still be bugs left.

Calculating in this OS is limited to integers so using GB for bandwidth takes away some accuracy. If there is a way around this let me know. Because of the integer limitation I used MB in yesterday's totals.

Let me know what you think!

Edit Feb 8th 2011
New release, ver 0.2
Had to change zero stripping logic when running
date commands. 08 and 09 do not work with "let"
commands. 07 and below are fine...Weird!
tangled111
DD-WRT Novice


Joined: 31 Jan 2011
Posts: 7

PostPosted: Sun Mar 27, 2011 2:58    Post subject: Re: Bandwidth Reporting Script based on your true Billing Mo Reply with quote
gdlgiii wrote:
But how do you upload the script into the router? How is it called? Via CRON job? Is this automated??

You definitely want to automate the script through cron. I have it running once a day.

You are going to have to do some reading to get this up and running.
Use the wiki to help you get through the steps below http://www.dd-wrt.com/wiki/index.php/Main_Page

1) Enable and setup ssh for your router through dd-wrt web interface
2) Enable cron, also through the web interface
3) scp the script to the /tmp dir and test it out.
You can run from the /tmp dir, but if you restart your modem everything in /tmp is wiped out.
4) Enable jffs and "clear" jffs to create space. This will allow you to keep the script in the jffs space that won't get removed after a modem reboot.
5) Create cron job in the web tool, save, reboot
gdlgiii
DD-WRT Novice


Joined: 26 Mar 2011
Posts: 22

PostPosted: Sun Mar 27, 2011 4:39    Post subject: Re: Bandwidth Reporting Script based on your true Billing Mo Reply with quote
I do not have an option to enable JFFS.. I have the micro version running. I looked all over this thing and couldn't find it. But yes, those instructions look VERY easy.. just those options do not show up for me. Sad
Also do not have SSH but have cron.


tangled111 wrote:
gdlgiii wrote:
But how do you upload the script into the router? How is it called? Via CRON job? Is this automated??

You definitely want to automate the script through cron. I have it running once a day.

You are going to have to do some reading to get this up and running.
Use the wiki to help you get through the steps below http://www.dd-wrt.com/wiki/index.php/Main_Page

1) Enable and setup ssh for your router through dd-wrt web interface
2) Enable cron, also through the web interface
3) scp the script to the /tmp dir and test it out.
You can run from the /tmp dir, but if you restart your modem everything in /tmp is wiped out.
4) Enable jffs and "clear" jffs to create space. This will allow you to keep the script in the jffs space that won't get removed after a modem reboot.
5) Create cron job in the web tool, save, reboot
tangled111
DD-WRT Novice


Joined: 31 Jan 2011
Posts: 7

PostPosted: Sun Mar 27, 2011 12:11    Post subject: Re: Bandwidth Reporting Script based on your true Billing Mo Reply with quote
gdlgiii wrote:
I do not have an option to enable JFFS.. I have the micro version running. I looked all over this thing and couldn't find it. But yes, those instructions look VERY easy.. just those options do not show up for me. Sad
Also do not have SSH but have cron.


Yup, this will not work with micro version. I found that micro version doesn't even have all the commands needed to run the script. You can't even do an "ls" command on mini. I should have put this step in first:

0) Make sure your dd-wrt version is > mini
I actually don't know what the minimum required version is. I developed and tested this script on DD-WRT v24-sp2 (08/07/10) std (SVN revision 14896)
deadman36g
DD-WRT Novice


Joined: 09 Dec 2009
Posts: 13

PostPosted: Thu May 28, 2015 17:54    Post subject: Help Reply with quote
Hello, sorry for digging up an old thread but this scripts looks very useful to me.

I am sorta a ddwrt n00b though (but I did get ddwrt installed so maybe I got some skillz?) and need some help firstly,

'Modifications needed for your use update the sendmail command in fcn_mail to reflect your ISP's SMTP server and sending address as well as the recipient email'

I am not sure what to do here, I would like to use my gmail account if possible. I don’t ever use my ISP email address, but is it needed for the sender and I can use gmail as the reciever?

Also, is there a way I can test email to make sure it is sending instead of waiting for a bandwith limit?
deadman36g
DD-WRT Novice


Joined: 09 Dec 2009
Posts: 13

PostPosted: Tue Jun 23, 2015 23:11    Post subject: Reply with quote
Can anyone help me?
bfason
DD-WRT Novice


Joined: 31 Dec 2015
Posts: 1

PostPosted: Tue Jan 05, 2016 20:48    Post subject: Sendmail flags Reply with quote
Hey all, sorry to post on an old thread but I'm attempting to make this script work.

It displays correct information on screen but the SendMail flags appear to be incorrect and I'm coming up short trying to find a way to correct.

both
Code:
sendmail -s -F
show in the log as invalid flags.

The snippet this refers to is
Code:
fnc_mail() {
   subj="$1"
   msg="$2"
   if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastmail.log"; else logfile="$3"; fi
   sendmail -S"smtp.yourisp.com" -f"sender@yourisp.com" -F"DD-WRT" -d"yourisp.com" -s"Bandwidth Report" -m"$msg" recipient@domain.com >$logfile 2>&1
}


Any help would be greatly appreciated.
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Forum Index -> Contributions Upload All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum