Daily email with the previous day's bandwidth usage?

Post new topic   This topic is locked: you cannot edit posts or make replies.    DD-WRT Forum Index -> General Questions
Goto page 1, 2, 3 ... 10, 11, 12  Next
Author Message
rseiler
DD-WRT Guru


Joined: 01 Oct 2007
Posts: 622

PostPosted: Sat Jun 26, 2010 6:17    Post subject: Daily email with the previous day's bandwidth usage? Reply with quote
Does anyone already happen to have a script for that? I see how in this thread that sendmail can be used, and I know that the traff data is available, but that's about as far as I've gotten.

http://www.dd-wrt.com/phpBB2/viewtopic.php?t=68071
Sponsor
phuzi0n
DD-WRT Guru


Joined: 10 Oct 2006
Posts: 10141

PostPosted: Sat Jun 26, 2010 9:00    Post subject: Reply with quote
For the ttraff nvram variables you can view them with this.

nvram show | grep traff-

The variables are named traff-MM-YYYY. The data is a space separated list of values for each day's download:upload. At the end is a bracketed set of totals for the month.

Have fun reading the man pages for date, cut, etc. Wink

_________________
Read the forum announcements thoroughly! Be cautious if you're inexperienced.
Available for paid consulting. (Don't PM about complicated setups otherwise)
Looking for bricks and spare routers to expand my collection. (not interested in G spec models)
rseiler
DD-WRT Guru


Joined: 01 Oct 2007
Posts: 622

PostPosted: Sat Jun 26, 2010 22:38    Post subject: Reply with quote
Right, I know where the data is, but it's the entire rest that could prove interesting. At least I have some clues with date/cut. It's a little surprising that this hasn't been done before. A daily report, weekly, whatever.
rseiler
DD-WRT Guru


Joined: 01 Oct 2007
Posts: 622

PostPosted: Sat Aug 14, 2010 15:58    Post subject: Reply with quote
If anyone has an interest in doing this, or knows where it's been done, I'm still interested. There's no way I'm going to be able to do it with the information I have so far.
tc23emp
DD-WRT Novice


Joined: 23 Aug 2010
Posts: 33

PostPosted: Tue Aug 24, 2010 3:00    Post subject: Reply with quote
EDIT: Simplified process here.

Output (email):
Code:

Totals for Yesterday
Incoming: 1000 MB
Outgoing: 300 MB

Totals for Month to Date
Incoming: 80000 MB
Outgoing: 30000 MB


Works on DD-WRT v24-sp2 (14896).

The user and pass may be required for sendmail via SMTP.

Note: Do not use the script as quoted below, use the latest version on the above linked wiki page.


Last edited by tc23emp on Wed Sep 01, 2010 15:01; edited 5 times in total
stephensuley
DD-WRT Guru


Joined: 09 Feb 2008
Posts: 641
Location: Canada

PostPosted: Tue Aug 24, 2010 10:27    Post subject: Reply with quote
tc23emp wrote:
Output (email):
Code:

Totals for Yesterday
Incoming: 1000 MB
Outgoing: 300 MB

Totals for Month to Date
Incoming: 80000 MB
Outgoing: 30000 MB


Startup Script:
Code:

/bin/sh -c 'echo '\''#!/bin/sh'\'' > /tmp/bwmail; echo '\''sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"Bandwidth Report" -m"$(nvram get $(date +traff-%m-%Y) | awk '\'\\\'\''{print $'\'\\\'\''$(expr $(date +%d) - 1)'\'\\\'\'', $NF}'\'\\\'\'' | sed -e '\'\\\'\''s;\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)];Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for Month to Date\nIncoming: \3 MB\nOutgoing: \4 MB\n;'\'\\\'\'')" me@gmail.com > /tmp/bwmail.log 2>&1'\'' >> /tmp/bwmail; chmod u+x /tmp/bwmail; sleep 20; echo "5 0 * * * root /tmp/bwmail" >> /tmp/crontab'



Works on DD-WRT v24-sp2 (14896).

The user and pass may be required for sendmail via SMTP.

The mess of nested apostrophes can be avoided if you can create script files on a permanent filesytem.

The code can be pasted via the web GUI into the startup script or, if that is bugged, into the firewall script. I couldn't get this to work via cron_jobs even while bypassing the web GUI and modifying nvram directly. I think you are better off sticking with simple calls to scripts in any crontab files.

Also, the sleep is there because the crontab files can be flushed if you are adding to them before cron has started.



Can you show how the script is modified to use SMTP Auth? Also can you tell me if the email is sent when the script is done processing?

Great work thanks

_________________
Location (urban) - 1x Linksys EA8500 (AP wlan0 & wlan1 enabled)
1x Asus 68u (Repeater Bridge w/VAP) - wl0 disabled
1x Asus 87u (Client Bridge) - wl1 disabled


Latest and greatest builds Wink
tc23emp
DD-WRT Novice


Joined: 23 Aug 2010
Posts: 33

PostPosted: Tue Aug 24, 2010 13:12    Post subject: Reply with quote
Add -u"user" -p"password" to the sendmail arguments.

After you add the line to your startup script, you will need to reboot the router (or run the command manually as well). Every boot, a script will be stashed in memory (/tmp/bwmail). You can edit that file directly and run it at any time to test if the mail is being sent. Look in /tmp/bwmail.log for output or errors. If you make changes to bwmail directly, make sure they are reflected in the startup script or they will revert at next reboot. Also, note the startup script is more complicated since it has multiple nested apostrophes.

A cron job is scheduled for 12:05AM at the start of a new day (5 0 * * *) and the script will not be executed until that time. Do not schedule it to run before midnight since it simply looks for the previous day's totals. Also, if you run it later in the day, the previous day's total will stay static, but you will have to add in some math if you want the monthly total for a previous time.
rseiler
DD-WRT Guru


Joined: 01 Oct 2007
Posts: 622

PostPosted: Tue Aug 24, 2010 14:03    Post subject: Reply with quote
I look forward to trying this today. Thanks SO much for this!
phuzi0n
DD-WRT Guru


Joined: 10 Oct 2006
Posts: 10141

PostPosted: Tue Aug 24, 2010 17:56    Post subject: Reply with quote
Nice script, I added it to the useful scripts page.

http://www.dd-wrt.com/wiki/index.php/Useful_Scripts#Email_Bandwidth_Usage_Daily

_________________
Read the forum announcements thoroughly! Be cautious if you're inexperienced.
Available for paid consulting. (Don't PM about complicated setups otherwise)
Looking for bricks and spare routers to expand my collection. (not interested in G spec models)
rseiler
DD-WRT Guru


Joined: 01 Oct 2007
Posts: 622

PostPosted: Tue Aug 24, 2010 20:02    Post subject: Reply with quote
tc23emp, I tried this on Comcast as well, so only had to make the following changes:

sender@comcast.net
me@gmail.com

To test it, I've tried running /tmp/bwmail manually, but mail is not sending or bouncing. bwmail.log was written once, but it's zero bytes. Can you take a look at bwmail to see if anything jumps out at you? I changed the above two items back for the purposes of posting here. I have a fairly recent build (14583) but can update it if it turns out that's the problem. Thanks.

Code:
sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"Bandwidth Report" -m"$(nvram get $(date +traff-%m-%Y) | awk '{print $'$(expr $(date +%d) - 1)', $NF}' | sed -e 's;\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)];Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for Month to Date\nIncoming: \3 MB\nOutgoing: \4 MB\n;')" me@gmail.com > /tmp/bwmail.log 2>&1
tc23emp
DD-WRT Novice


Joined: 23 Aug 2010
Posts: 33

PostPosted: Tue Aug 24, 2010 21:05    Post subject: Reply with quote
The script as you posted, works for me.
Try the following two commands seperately from the shell to see if you can isolate the problem.

Code:

sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"Bandwidth Report" -m"test" me@gmail.com


Code:

nvram get $(date +traff-%m-%Y) | awk '{print $'$(expr $(date +%d) - 1)', $NF}' | sed -e 's;\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)];Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for Month to Date\nIncoming: \3 MB\nOutgoing: \4 MB\n;'


The script should look exactly like this:
Code:

#!/bin/sh
sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"Bandwidth Report" -m"$(nvram get $(date +traff-%m-%Y) | awk '{print $'$(expr $(date +%d) - 1)', $NF}' | sed -e 's;\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)];Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for Month to Date\nIncoming: \3 MB\nOutgoing: \4 MB\n;')" me@gmail.com > /tmp/bwmail.log 2>&1
Dark_Shadow
DD-WRT Guru


Joined: 31 Aug 2009
Posts: 2448
Location: Third Rock from the Sun

PostPosted: Tue Aug 24, 2010 21:57    Post subject: Reply with quote
tc23emp wrote:
Add -u"user" -p"password" to the sendmail arguments.

After you add the line to your startup script, you will need to reboot the router (or run the command manually as well). Every boot, a script will be stashed in memory (/tmp/bwmail). You can edit that file directly and run it at any time to test if the mail is being sent. Look in /tmp/bwmail.log for output or errors. If you make changes to bwmail directly, make sure they are reflected in the startup script or they will revert at next reboot. Also, note the startup script is more complicated since it has multiple nested apostrophes.

A cron job is scheduled for 12:05AM at the start of a new day (5 0 * * *) and the script will not be executed until that time. Do not schedule it to run before midnight since it simply looks for the previous day's totals. Also, if you run it later in the day, the previous day's total will stay static, but you will have to add in some math if you want the monthly total for a previous time.


What about using port 465 for smtp.gmail? Is it possible to add this argument?

tc23emp wrote:

The script should look exactly like this:
Code:

#!/bin/sh
sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"Bandwidth Report" -m"$(nvram get $(date +traff-%m-%Y) | awk '{print $'$(expr $(date +%d) - 1)', $NF}' | sed -e 's;\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)];Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for Month to Date\nIncoming: \3 MB\nOutgoing: \4 MB\n;')" me@gmail.com > /tmp/bwmail.log 2>&1

At this point a cron job should be added to run the script file?

_________________
Peacock Thread-FAQ -- dd-wrt Wiki

Testing Multiple Routers -- Bootloader Collection Project -- My Wiki
tc23emp
DD-WRT Novice


Joined: 23 Aug 2010
Posts: 33

PostPosted: Tue Aug 24, 2010 22:04    Post subject: Reply with quote
EDIT: I was looking at a completely different shell, just check the busybox manual.

I'm not really sure. On my firmware, sendmail points to exim. You can actually use: man exim to get more information or google which arguments you need to use with exim.


Last edited by tc23emp on Wed Aug 25, 2010 1:14; edited 1 time in total
phuzi0n
DD-WRT Guru


Joined: 10 Oct 2006
Posts: 10141

PostPosted: Tue Aug 24, 2010 22:28    Post subject: Reply with quote
tc23emp wrote:
I'm not really sure. On my firmware, sendmail points to exim. You can actually use: man exim to get more information or google which arguments you need to use with exim.

Does that mean you're using optware? Sendmail is handled by busybox by default.

root@DD-WRT:~# ls -l /usr/sbin/sendmail
lrwxrwxrwx 1 root root 17 May 21 14:24 /usr/sbin/sendmail -> ../../bin/busybox

ps. sorry about the crediting the wrong person. I just went to correct it and you already had.

_________________
Read the forum announcements thoroughly! Be cautious if you're inexperienced.
Available for paid consulting. (Don't PM about complicated setups otherwise)
Looking for bricks and spare routers to expand my collection. (not interested in G spec models)
tc23emp
DD-WRT Novice


Joined: 23 Aug 2010
Posts: 33

PostPosted: Tue Aug 24, 2010 22:37    Post subject: Reply with quote
EDIT: I was testing something in a different shell and got completely mixed up. Forget exim, there's just busybox to worry about.

-----
No big deal, it was an easy mistake to make with all the quoting.

I'm not using optware. I was on DD-WRT v24-sp2 (14896), but I just upgraded to DD-WRT v24-sp2 (14929) to try out K2.6.

The same syntax worked on both, but thanks for the info. Checking the busybox manual is probably the way to go.


Last edited by tc23emp on Wed Aug 25, 2010 1:18; edited 1 time in total
Goto page 1, 2, 3 ... 10, 11, 12  Next Display posts from previous:    Page 1 of 12
Post new topic   This topic is locked: you cannot edit posts or make replies.    DD-WRT Forum Index -> General Questions 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 can attach files in this forum
You can download files in this forum