TechInfoDepot:DD-WRT/Tc23emp

From TechInfoDepot
Jump to navigationJump to search

Email Bandwidth Usage Daily

This script by tc23emp will email you with the previous day's and total month's bandwidth usage. Edit it with your SMTP info. http://www.dd-wrt.com/phpBB2/viewtopic.php?p=476631#476631

Runs it 12:05AM at the start of a new day

Explanation of flags

-S = smtp server address
-f = from email address
-u = username for the smtp account
-p = password for smtp account
-s = email subject
-d = senders domain
-F = "From" name (as opposed to address)

This script will email you with the previous day's and total month's bandwidth usage. Edit it with your sendmail SMTP info.

Original Forum Topic

#!/bin/sh

fnc_mail() {
   subj="$1"
   msg="$2"
   if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastmail.log"; else logfile="$3"; fi
   
   sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"$subj" -m"$msg" me@gmail.com > $logfile 2>&1
}

#Parse the previous day and monthly totals and send an email
aff="aff" #keyword workaround
yday=$(date -D %s -d $(( $(date +%s) - 86400)) +%d)
ymon=$(date -D %s -d $(( $(date +%s) - 86400)) +tr$aff-%m-%Y)
ydat=$(date -D %s -d $(( $(date +%s) - 86400)) +"%h %d") 
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/\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)]/Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for '"${monmsg}"'\nIncoming: \3 MB\nOutgoing: \4 MB\n/')
fnc_mail "Bandwidth Report for $ydat" "$msg" "/tmp/bwmail.log" 


More in-depth examples

These scripts can be added via the web interface.
Tested on DD-WRT v24-sp2 (08/12/10) std-nokaid-small (SVN revision 14929)

Administration -> Management ->
Enable Cron = true
Additional Cron Jobs:

5 0 * * * root /tmp/custom.sh bwmail
*/5 * * * * root /tmp/custom.sh wlclient

Administration -> Commands -> Custom Script:

#!/bin/sh

# Bandwidth Usage email

fnc_mail() {
   subj="$1"
   msg="$2"
   if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/tmp/lastmail.log"; else logfile="$3"; fi
   
   sendmail -S"smtp.comcast.net" -f"sender@comcast.net" -F"DD-WRT" -d"comcast.net" -s"$subj" -m"$msg" me@gmail.com > $logfile 2>&1
}

if [ "$1" = "bwmail" ]; then
   #Parse the previous day and monthly totals and send an email
   aff="aff" #keyword workaround
   yday=$(date -D %s -d $(( $(date +%s) - 86400)) +%d)
   ymon=$(date -D %s -d $(( $(date +%s) - 86400)) +tr$aff-%m-%Y)
   ydat=$(date -D %s -d $(( $(date +%s) - 86400)) +"%h %d")
   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/\([^:]*\):\([^ ]*\) \[\([^:]*\):\([^]]*\)]/Totals for Yesterday\nIncoming: \1 MB\nOutgoing: \2 MB\n\nTotals for '"${monmsg}"'\nIncoming: \3 MB\nOutgoing: \4 MB\n/')
   fnc_mail "Bandwidth Report for $ydat" "$msg" "/tmp/bwmail.log" 
fi

# Client notification email

if [ "$1" = "wlclient" ]; then
   #Send a e-mail whenever a new wireless client connects, remember them until the next router reboot
   #Uncomment the following line and edit the list MAC addresses you don't want to be notified about (seperated by spaces)
   #macignore="0A:1B:2C:3D:4E:5F"
   for mac in $macignore; do
      grep -q $mac /tmp/wlclient.lst
      if [ $? -gt 0 ]; then
         echo $mac >> /tmp/wlclient.lst
      fi
   done
   maclist=$(wl -i $(nvram get wl0_ifname) assoclist | cut -d" " -f2)
   for mac in $maclist; do
      grep -q $mac /tmp/wlclient.lst
      if [ $? -gt 0 ]; then
         echo $mac >> /tmp/wlclient.lst
         inf=$(arp | grep $mac)
         if [ $? -gt 0 ]; then
            msg="$msg""Unknown at ${mac}\n"
         else
            msg="$msg""${inf}\n"
         fi
      fi
   done
   #Uncomment the following line if you want to remove disconnected clients from the previously connected list
   #echo "$maclist" | tr ' ' '\n' > /tmp/wlclient.lst
   if [ -n "$msg" ]; then
      msg=$(echo -e "$msg")
      fnc_mail "New Wireless Client(s)" "$msg"
   fi
fi