Blog is moving

My blog is moving to http://victormendonca.com/blog/. If you are looking for a specific or older post you are in the right place Otherwise check out my new page for more up to date content.

Monday, October 21, 2013

Email DD-WRT Usage Based on MAC Address

If you have limited bandwidth with your ISP and need to monitor data usage of a device at home, here's a neat script that will do just that for you.

The script can check the last day or hour usage and compare with a limit that you set. If it's over it will send you an email with a warning and the amount of data used.

You will need to have the following installed on you DD-WRT router:
. bc (calculation)
. msmtp (email)

The following needs to be changed on the script:

# Change this as needed
SUBJECT=""
MAIL_FROM="[mail address used to send the email]"
MAIL_TO="[the email address we are sending the alert]"
MAIL_FROM_NAME="[mail address used to send the email]"
DAILY_LIMIT="[daily limit for the device]"
HOUR_LIMIT="[hour limit for the device]"
export LD_LIBRARY_PATH="[login to the router and run 'echo $LD_LIBRARY_PATH']"

Copy the script over to your USB on your router, then set two cron jobs in "Administration => Management" using the router's web page:

0 0 * * * root [path]/bandwUsageByMac.sh [YAMon data path] [mac] day >> /jffs/var/log/cron 2>&1
0 * * * * root [path]/bandwUsageByMac.sh [YAMon data path] [mac] hour >>/jffs/var/log/cron 2>&1

And here's the script:

Download

#!/bin/sh
################################################################################
################################################################################
# Name:   bandwUsageByMac.sh
# Usage:  bandwUsageByMac.sh [yamon data folder] [mac] [hour|day]
# Created:  2013-10-17
# Last Modified:
# Copyright 2009, Victor Mendonca - http://wazem.org
# License: Released under the terms of the GNU GPL license
################################################################################
################################################################################

#-------------------------------------------------------------------------------
# Sets variables
#-------------------------------------------------------------------------------

RUN_FOLDER="$1" # Usually /opt/YAMON/data
MAC="$2"
MODE="$3"
USAGE="Usage: bandwUsageByMac.sh [yamon data folder] [mac] [hour|day]
"
HOUR=$(date +%H)
DATE=$(date +%d-%m-%Y)
YESTERDAY=$(awk -v days="-1" 'BEGIN{print \
strftime("%d",systime()+(days*86400))}')
LAST_HOUR=$(awk -v hour="-1" 'BEGIN{print \
strftime("%H",systime()+(hour*3600))}')

# Change this as needed
SUBJECT="DD-WRT Usage"
MAIL_FROM=""
MAIL_TO=""
MAIL_FROM_NAME=""
DAILY_LIMIT="1024"
HOUR_LIMIT="250"
export LD_LIBRARY_PATH=""

#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------

SendMail () {
## Example command
# echo "$VAR1" | msmtp -C msmtprc user@domain.com

## Example Body
#To: user@domain.com
#FROM: router@home.com
#Subject: DD-WRT - Usage
#This is a test.
#Ciao

MAIL="To: $MAIL_TO
FROM: $MAIL_FROM_NAME
Subject: $SUBJECT
$BODY"

echo "$MAIL" | msmtp -C /jffs/etc/msmtprc $MAIL_TO
}


CalcMB () {
echo "scale=2;(${1}*9.5367)/10000000" | bc
}


## => Daily Usage <= ##
getDayUsage () {
LOG_TOTL=$(ls -tr *mac_data.html | tail -1)
if [ "x$LOG_TOTL" = "x" ] ; then
  echo "Could not find daily usage file"
  exit 1
fi

#-- Daily download
DOWNLOAD_TOTAL=$(grep -i "${MAC}.*${YESTERDAY}$" ${LOG_TOTL} | awk -F, '{print $3}')
if [ "x$DOWNLOAD_TOTAL" = "x" ] ; then
  exit 0
fi
DOWNLOAD_TOTAL=$(CalcMB ${DOWNLOAD_TOTAL})

#-- Daily upload
UPLOAD_TOTAL=0
UPLOAD_TOTAL=$(grep -i "${MAC}.*${YESTERDAY}$" ${LOG_TOTL} | awk -F, '{print $4}')
UPLOAD_TOTAL=$(echo "scale=2;(${UPLOAD_TOTAL}*9.5367)/10000000" | bc)
DAILY_TOTAL=$(echo ${DOWNLOAD_TOTAL} + ${UPLOAD_TOTAL} | bc)
if [[ $(echo "$DAILY_TOTAL $DAILY_LIMIT" | awk '{if ($1 > $2) print "1"; else print "0";}') -eq 1 ]] ; then
  BODY="
Total day usage has gone over limit of ${DAILY_LIMIT}MB

DOWN: ${DOWNLOAD_TOTAL}MB UP: ${UPLOAD_TOTAL}MB
TOTAL: ${DAILY_TOTAL}MB"
  SendMail
else
  echo "Usage ok"
fi

echo "TOTAL - DOWN: $DOWNLOAD_TOTAL UP: $UPLOAD_TOTAL"
}


## => Hour Usage <= ##
getHourUsage () {
LOG_HOUR="${DATE}-hourly_data.html"
if [ "x$LOG_HOUR" = "x" ] ; then
  echo "Could not find hour usage file"
  exit 1
fi

#-- Hourly download
DOWNLOAD_HOUR=$(grep -i "${MAC}.*${LAST_HOUR}$" ${LOG_HOUR} | awk -F, '{print $3}')
if [ "x$DOWNLOAD_HOUR" = "x" ] ; then
  echo "No hourly file"
  exit 0
fi
DOWNLOAD_HOUR=$(echo "scale=2;(${DOWNLOAD_HOUR}*9.5367)/10000000" | bc)

#-- Hourly upload
UPLOAD_HOUR=$(grep -i "${MAC}.*${LAST_HOUR}$" ${LOG_HOUR} | awk -F, '{print $4}')
UPLOAD_HOUR=$(echo "scale=2;(${UPLOAD_HOUR}*9.5367)/10000000" | bc)

HOUR_TOTAL=$(echo ${DOWNLOAD_HOUR} + ${UPLOAD_HOUR} | bc)
if [[ $(echo "$HOUR_TOTAL $HOUR_LIMIT" | awk '{if ($1 > $2) print "1"; else print "0";}') -eq 1 ]] ; then
  BODY="
Total hour usage has gone over limit of ${HOUR_LIMIT}MB

DOWN: ${DOWNLOAD_HOUR}MB UP: ${UPLOAD_HOUR}MB
TOTAL: ${HOUR_TOTAL}MB"
  SendMail
else
  echo "Usage ok"
fi

echo "Hour - DOWN: $DOWNLOAD_HOUR UP: $UPLOAD_HOUR"
}

#-------------------------------------------------------------------------------
# Starts script
#-------------------------------------------------------------------------------

if [[ $# -ne 3 ]] ; then
  echo $USAGE
  exit 1
fi

if [[ $(cd "$RUN_FOLDER" 2> /dev/null ; echo $?) -eq 0 ]] ; then
  cd "$RUN_FOLDER"
else
  echo "yamon data folder could not be found"
  exit 1
fi

case $MODE in
 day) getDayUsage ;;
 hour) getHourUsage ;;
 *) echo "$USAGE" 
    exit 1 ;;
esac

Friday, October 18, 2013

How to install packages on Asus RT-AC66U with DD-WRT (opkg)

If you are having issues installing packages with the built-in ipkg, this post might help you. Please note that the links that I provide are hardware specific, so you need to find what your hardware is and then find the proper download path here - http://downloads.openwrt.org/attitude_adjustment/12.09/.

Before we start, let's make sure your USB is working. Make sure it's formatted with ext3.

=> 1- USB Setup

1a. Login to your router and go to Services => USB:


1b. Make sure that these options are enabled:
- Core USB Support
- USB Storage Support
- Automatic Drive Mount

1c. Connect to your router via ssh and plug in your USB; then run the command "df -h"

# df -h
 Filesystem                Size      Used Available Use% Mounted on
 rootfs                   16.0M     16.0M         0 100% /
 /dev/root                16.0M     16.0M         0 100% /
 none                    512.0K         0    512.0K   0% /dev
 /dev/sda1               464.9M     0    464.9M   0% /tmp/mnt/sda1

My USB drive shows as '/dev/sda1' mounted in '/tmp/mnt/sda1'

1d. Create a /opt and /jffs on your USB drive

# mkdir /tmp/mnt/sda1/opt /tmp/mnt/sda1/jffs

1e. Back to your router admin page, go to Administration => Commands. Add a startup command with the following code:

 ## Sets up USB mount
 CURRENT_MOUNT=$(df -h | grep sda | awk '{print $6}')
 umount $CURRENT_MOUNT
 mount /dev/sda1 /mnt
 mount -o bind /mnt/opt /opt
 mount -o bind /mnt/jffs /jffs

Note: the command above assumes you have only one USB device and it shows as /dev/sda*

1f. Reboot the router

reboot

1g. Logon via ssh again and check the mount points

 # df -h
 Filesystem                Size      Used Available Use% Mounted on
 rootfs                   16.0M     16.0M         0 100% /
 /dev/root                16.0M     16.0M         0 100% /
 none                    512.0K         0    512.0K   0% /dev
 /dev/sda1               464.9M     0    464.9M   0% /tmp/mnt
 /dev/sda1               464.9M     0    464.9M   0% /opt
 /dev/sda1               464.9M     0    464.9M   0% /jffs

=> 2- Install opkg

2a. First let's update the ipkg package list

ipkg update

2b. Now let's install opkg to '/jffs'

ipkg -d /jffs install http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages/opkg_618-3_brcm47xx.ipk 

2c. Set 'dest root' in '/jffs/etc/opkg.conf' to use '/jffs'

# cat /jffs/etc/opkg.conf
src/gz attitude_adjustment http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages
dest root /jffs
dest ram /tmp
lists_dir ext /var/opkg-lists
option overlay_root /overlay 

2d. Update opkg

opkg -f /jffs/etc/opkg.conf update

2e. Install libc (dependency)

opkg -f /jffs/etc/opkg.conf install http://downloads.openwrt.org/attitude_adjustment/12.09/brcm47xx/generic/packages/libc_0.9.33.2-1_brcm47xx.ipk

2f. Now you can install packages with

opkg -f /jffs/etc/opkg.conf install [package]

Monday, October 14, 2013

How to install DD-WRT on Asus RT-AC66U (AC1750)

Update (Oct 23 2014)

It looks like the link for Kongs build is down (.bin file), and his images are now being posted under the "betas" ftp site for DD-WRT:


My instructions show how to install the stable (at that time) DD-WRT version from BrainSlayer, and then flash Kong's firmware, which usually has additional options.

If you just to want to install DD-WRT, I think the best option would be to follow the instructions on the official wiki - http://www.dd-wrt.com/wiki/index.php/Asus_RT-AC66U.

If you want to install Kong's firmware, go to the link he mentioned (screenshot above) and look for the folder "asus-rt-ac66u". These are all beta and might have images from other contributors.

Please note that I followed the procedure I outlined below, but I believe that flashing from the official wiki should be pretty safe.

-----------------------------------------------------------------------------------------------------------------

Here are quick instructions on how to install DD-WRT in the new Asus RT-AC66U. You can find additional info and get support here.

You are going to need two files; the first is a .trx, and the second is a .bin.

Download trx
Download bin

1- Login via UI and enable telnet (or ssh)

2- Login via telnet (or ssh)

$ telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
RT-AC66U login: admin
Password: 


ASUSWRT RT-AC66U_3.0.0.4 Fri Jul  5 03:20:20 UTC 2013
admin@RT-AC66U:/tmp/home/root#

3- Clear the nvram with 'mtd-erase -d nvram':


# mtd-erase -d nvram
Erasing 0x0 - 0xffff
"nvram" successfully erased.

4- Reboot the router


# reboot
Rebooting...
admin@RT-AC66U:/tmp/home/root# Connection closed by foreign host.

5- Login via UI (password is reset to admin:admin)

6- Install the .trx file

7- Login via telnet again


$ telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.

DD-WRT v24-sp2 giga (c) 2013 NewMedia-NET GmbH
Release: 07/24/13 (SVN revision: 22118)

DD-WRT login: root
Password: 
==========================================================

 ____  ___    __        ______ _____         ____  _  _ 
 | _ \| _ \   \ \      / /  _ \_   _| __   _|___ \| || | 
 || | || ||____\ \ /\ / /| |_) || |   \ \ / / __) | || |_ 
 ||_| ||_||_____\ V  V / |  _ < | |    \ V / / __/|__   _| 
 |___/|___/      \_/\_/  |_| \_\|_|     \_/ |_____|  |_| 

                       DD-WRT v24-sp2
                   http://www.dd-wrt.com

==========================================================


BusyBox v1.21.0 (2013-07-24 10:53:52 CEST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

root@DD-WRT:~#

8- Erase nvram again, this time include 'clear nvram'


# mtd-erase -d nvram
Erasing 0x0 - 0xffff
"nvram" successfully erased.

# clear nvram

9- Reboot the router


# reboot
root@DD-WRT:~# Connection closed by foreign host.

10- Login via UI and install the *.bin file