The script is working great on my router. Thank you to all who has contributed to it.
I have been thinking that it would be nice to have a daily average statistic to the bandwidth report.
For example, I would like to have the report show:
Totals for Yesterday
Incoming: 29 MB
Outgoing: 9 MB
Totals for Month to Date
Incoming: 11886 MB
Outgoing: 2569 MB
Daily average Month to Date:
Incoming: 476 MB
Outgoing: 67 MB
Therefore if the report was sent on day 23 of a particular month, the script would calculate the daily average of the previous 22 days. Thanks in advance for any help on this.
Thanks to all who have put an effort into this. Here's my contribution...
Conditional email message based on percent of bandwidth used and percent penetration into your billing period. In other words, if you are using more than you should, billing period to date, you'll get an alert.
Set "bd" to the last day of your billing period.
Set "monthcap" to your bandwidth cap, in MB's.
Code:
#!/bin/sh
fnc_mail() {
subj="$1"
msg="$2"
if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastmail.log"; else logfile="$3"; fi
if [ $percentu -gt $percentp ]
then
remainbw=$((monthcap-pdt))
fnc_mail "Problem!! You will bust if you keep this up. You have "$remainbw "MB left..."
fi
fi
It's still a little rough, but I figured I'd post it in its current state so others may play around with it. Shell doesn't do integers, so the percentages are approximate.
NB: it uses jffs to store the tally. I'm not sure if rewriting once a day is something that could harm router.
Comcast has done away with port 25 and unauthenticated sending. Those with older DD-WRT will know how to specify user/pass, but with the newer sendmail found in the changed BusyBox (also see up the thread), it's different. This is how the new line should look for Comcast:
Tried this and now i'm getting the report email every 5 minutes instead of once a day.
It's unclear what you changed, but it had to be your cron line in the Administration tab, since changing anything about the script itself is unrelated. You want the first part of the line to look something like this:
5 0 * * *
Tried this and now i'm getting the report email every 5 minutes instead of once a day.
It's unclear what you changed, but it had to be your cron line in the Administration tab, since changing anything about the script itself is unrelated. You want the first part of the line to look something like this:
5 0 * * *
That's 5 past midnight once a day.
I used Drats version from page 8 BWSendc.txt and put
Yes, that second line would, but if you're just interested in the daily bandwidth report, then you don't need the second line at all, which was in the original version of the script and had something to do with monitoring wireless clients attaching to the network (I think).
Thanks to everyone that worked on this script, I'm still using it. I Upgraded from BS 15962 to BS 27506 which came with BusyBox 1.23.2. In addition to changes already discussed in this thread (sendmail and line termination) there were changes to the date command. This is the current version of my Bandwidth Email Script. Note the changes to the format of the date command in the 3 lines ydat=, yday=, and ymon=.
Code:
#!/bin/sh
fnc_mail() {
subj="$1"
msg="$2"
if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/jffs/log/lastmail.log"; else logfile="$3"; fi
echo "From: Tech Services Router <me@nowhere.com>" > /jffs/bwmsg.txt
echo "Subject: $subj" >> /jffs/bwmsg.txt
echo "To: me@nowhere.com" >> /jffs/bwmsg.txt
echo "To: mybuddy@lost.com" >> /jffs/bwmsg.txt
echo "To: myotherbuddy@overthere.com" >> /jffs/bwmsg.txt
echo -e $msg >> /jffs/bwmsg.txt
cat /jffs/bwmsg.txt | sendmail -S"smtp.nowhere.com:587" -au"me@nowhere.com" -ap"mypassword" -f"me@nowhere.com" > $logfile 2>&1
}
#Parse the previous day and monthly totals and send an email
aff="aff" #keyword workaround
fnvr=`nvram show 2>&1 1>/dev/null | grep -o ' ([0-9]*' | tr -cd '0-9'`
ctim=$(date +%T)
upt=$(uptime | awk -F'l' '{print $1}' |cut -d' ' -f4-)
pubip=$(nvram get wan_ipaddr)
ydat=$(date -d @$(( $(date +%s) - 86400)) +"%d %h %Y")
yday=$(date -d @$(( $(date +%s) - 86400)) +%d)
ymon=$(date -d @$(( $(date +%s) - 86400)) +tr$aff-%m-%Y)
if [ $(date +%d) -eq 1 ]; then monmsg="Last Month"; else monmsg="Month to Date"; fi
msg=$(nvram get $ymon | awk '{print $'$yday', $NF}' | sed 's/\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)]/Tech Services\rSystem Router\r\rTotals for '"${ydat}"'\rIncoming: \1 MB\rOutgoing: \2 MB\r\rTotals for '"${monmsg}"'\rIncoming: \3 MB\rOutgoing: \4 MB\r\rRouter Stats\rNVRAM Free: '"${fnvr}"' Bytes\rUptime: '"${upt}"' at '"${ctim}"'\rPublic IP: '"${pubip}"'\r/')
fnc_mail "Bandwidth Report for $ydat" "$msg" "/jffs/log/bwmail.log"
Thanks. I see the changes in the date command, but what problem prompted you to make them?
I just upgraded to this build too, and so far nothing seems to have broken with the script relative to dates. Is it something subtle that I'll only see later?
Posted: Wed Jun 21, 2017 19:32 Post subject: Script help needed...
I recently upgraded my router. My old router was running build v24sp2-20025b while the new one I flashed with v3.0-r32170 std. On my old router I had a script that emailed me stats every day. The script will not work on the new unit. I was hoping someone could take a look at it below and maybe point out what needs to be changed for it to work. At first the script was giving me a date error. I looked through the forums and found out I have to change the date section from:
The script will still not send the email. My ISP does not require authentication. Here is the script I've been using:
#!/bin/sh
#
# 03-20-2012 --- RNR
# Brokeout all the personal variables to make it easier to modify.
# Added $comcast variable to make it work with . . .
#
# 03-07-2012 --- RNR
# Added DNS lease information to the end of the report.
#
# 01-24-2012 --- RNR
# Modified for DD-WRT Rev > 17628.
#
# 08-18-2011 --- RNR
# Bandwith report by email, custom script for DD-WRT .
# From: Useful Scripts Wiki --- Email Bandwidth Usage Daily
#
# Functions/Subs
#
fnc_mail() {
subj="$1"
msg="$2"
my_mail_addr="the from address"
my_mail_to="the to address"
my_smtp="the smtp server"
comcast="-d the domain"
if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastsentmail.log"; else logfile="$3"; fi
Posted: Wed Jun 21, 2017 21:13 Post subject: Re: Script help needed...
Johnb175 wrote:
I recently upgraded my router. My old router was running build v24sp2-20025b while the new one I flashed with v3.0-r32170 std. On my old router I had a script that emailed me stats every day. The script will not work on the new unit. I was hoping someone could take a look at it below and maybe point out what needs to be changed for it to work. At first the script was giving me a date error. I looked through the forums and found out I have to change the date section from:
The script will still not send the email. My ISP does not require authentication. Here is the script I've been using:
#!/bin/sh
#
# 03-20-2012 --- RNR
# Brokeout all the personal variables to make it easier to modify.
# Added $comcast variable to make it work with . . .
#
# 03-07-2012 --- RNR
# Added DNS lease information to the end of the report.
#
# 01-24-2012 --- RNR
# Modified for DD-WRT Rev > 17628.
#
# 08-18-2011 --- RNR
# Bandwith report by email, custom script for DD-WRT .
# From: Useful Scripts Wiki --- Email Bandwidth Usage Daily
#
# Functions/Subs
#
fnc_mail() {
subj="$1"
msg="$2"
my_mail_addr="the from address"
my_mail_to="the to address"
my_smtp="the smtp server"
comcast="-d the domain"
if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastsentmail.log"; else logfile="$3"; fi
fnc_mail "Router Bandwidth Report for $ydat" "$msg" "/tmp/bwmail.log"
#
# End BWSend.sh
#
In CLI, try running the sendmail command listed in your script as if you were going to send a test email and see if it throws any errors. I have noticed this aswell and found out that my syntax line has changed and the "-d" option is no longer needed.
and I get -sh: can't create : nonexistent directory
now if I run
cat /tmp/bwmsg.txt | sendmail <my to address> -S <my smtp> -f <my from address>
it works fine.
So I had to add <my to address> and remove the > $logfile 2>&1
I'm not really sure why though. If I edit custom.sh with those changes and try and run the whole thing from the CLI via ./custom.sh it never exits and keeps emailing me every two minutes. Any ideas on what's up with this script?
Joined: 01 Feb 2007 Posts: 138 Location: Wherever the boat takes me.
Posted: Thu Jun 22, 2017 14:06 Post subject:
The script fails because DD-WRT has upgraded BusyBox to the latest version. The -d option is no longer available as pointed out in the previous message.
There were also other changes made so that the script portion that resends the message, if it fails, is not getting the correct return error code. If you remove the resend portion near where there is a "sleep 120s" that should stop the resend. Removing the logfile means it never sees a valid return.
This change occurred in early February.
It seems like everytime BusyBox changes, it messes up something with Sendmail.
Is there an updated script somewhere that I missed? I did try jft's above but it would just give me permission denied. I did like you suggested. I "think" I removed the correct parts of the script. I will post it below. This one does send the email. Does it look correct?
#!/bin/sh
#
# 03-20-2012 --- RNR
# Brokeout all the personal variables to make it easier to modify.
# Added $comcast variable to make it work with . . .
#
# 03-07-2012 --- RNR
# Added DNS lease information to the end of the report.
#
# 01-24-2012 --- RNR
# Modified for DD-WRT Rev > 17628.
#
# 08-18-2011 --- RNR
# Bandwith report by email, custom script for DD-WRT .
# From: Useful Scripts Wiki --- Email Bandwidth Usage Daily
#
# Functions/Subs
#
fnc_mail() {
subj="$1"
msg="$2"
my_mail_addr="the from address"
my_mail_to="the to address"
my_smtp="the smtp server"
comcast="-d the domain"
if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastsentmail.log"; else logfile="$3"; fi