IP Accounting script for RouterTech firmware

I modded my D-LINK DSL G604T last year with the RouterTech custom firmware (2.3 version). I did this primarily to mess around with IP Accounting, as our ISP doesn’t provide any realtime accounting information and we’re on a 30GB monthly cap.

Unfortunately I found that on enabling IP Accounting, the logs stored in /proc/net/ipt_account/mynetwork get wiped on router reboot.

I created the following script to save out the populated IP Accounting rows to a minix file system that is mounted using the persistent RT_cmd approach  (e.g. setenv RT_cmd_3 mount -t minix -o sync /dev/mtdblock/5 /nvram) available with the RouterTech firmware.  Note that the script assumes that IP Accounting is enabled.

# Remember to chmod a+x this file
# Might need to run “gzip < /proc/net/ipt_account/mynetwork >
#/nvram/ipta.gz” manually to seed
gunzip < /nvram/ipta.gz | while read iptaline; \
do echo $iptaline > /proc/net/ipt_account/mynetwork; done
echo “*/5 * * * * gzip < /proc/net/ipt_account/mynetwork > /nvram/ipta.gz” \
>> /var/spool/cron/crontabs/root
echo “1,6,11,16,21,26,31,36,41,46,51,56 * * * * umount /nvram” \
>> /var/spool/cron/crontabs/root
echo “2,7,12,17,22,27,32,37,42,47,52,57 * * * * mount -t minix -o \
sync /dev/mtdblock/5 /nvram” >> /var/spool/cron/crontabs/root
kill `ps afx | grep ‘[/ ]crond’ | sed -e ‘s/^ *//’ -e ‘s/ .*//’`
crond -b&

The script is named startup.sh in the /nvram mount point so it gets executed on boot and reloads the IP Accounting state. The script saves and compresses only the relevant IP Account rows to the minix file system every 5 minutes and umounts and mounts the minixfilesystem, using cron, during the following 2 minutes after the save to ensure that the information is saved if a pull-the-plug reboot results in the minix file system not being shut down correctly (sync didn’t work in my usage) and losing the saved state. If you’re working on the file system with this executing, make sure to disable the script so it doesn’t pull the rug from under you. At the end of the script I kill crond and start it again - I found that crond could take quite a while to see the crontabs changes otherwise.

Finally, as I have no error checking in the state reload,  I recommend running ”gzip < /proc/net/ipt_account/mynetwork > /nvram/ipta.gz” to seed the process.


Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.