Get Usage Stats from Netgear DG834G Router

Last Updated on Wednesday, 2 December 2009 12:00 Written by admin Thursday, 3 September 2009 01:43

After being on the receiving end of an FUP (Fair Usage Policy) violation – essentially my ISP felt I had been downloading greedily, I decided that I’d write a utility to monitor my own Broadband usage, seeing as Demon (my ISP) does not provide this facility.

Initially I tried off-the-shelf applications like PRTG and Router Stats but they did not fit the bill for various reasons, not least the poor SNMP support on the DG843G router.

Then I stumbled upon a telnet reference that extracted the upload and download stats from my router.

After hacking some vbscript about a bit I produced the code below. Now, before you flame me for my poor code let me just say that I am not a professional programmer and only attempt this sort of thing once in a blue moon.

The purpose of the code is to log the router’s stats to a text file on a regular basis. This file can then be imported into Excel (or another application of your choice) for graphing etc.

The code produces a three column text file that looks like this:-

06/09/2009 08:00:00    2566906384    420938933
06/09/2009 08:15:00    2567674326    421835475
06/09/2009 08:30:00    2571321533    425060305
06/09/2009 08:45:00    2571606416    426546073
06/09/2009 09:00:00    2571722487    430474825
06/09/2009 09:15:00    2571827612    430826412
06/09/2009 09:30:00    2571883066    431213154
06/09/2009 09:45:00    2571932634    431365976
06/09/2009 10:00:00    2572103375    432212404
06/09/2009 10:15:00    2572240782    432670760

The Date/timestamp is a single column, the uploaded, and the downloaded byte counts are tab separated.

Setting the Task Scheduler to run this script every 15 minutes does the job for me.

A little manipulation in Excel enables me to produce usefull charts, like this one:-


Step-by-step guide to set this up:
1. Create a folder called C:\Utils
2. Go to http://tech.dimac.net/ and download the free “w3sockets” DLL.
3. Unpack the above into C:\Utils and run “SocketReg.exe
4. Create a text file called C:\Utils\RouterUsageStats.vbs
5. Copy the code , below, into this file
6. Edit the file and change the router address to your own.
7. Activate the Netgear’s Telnet feature by going to http://192.168.2.1/setup.cgi?todo=debug
(substitute the address with your own). This will produce a page saying “Debug Enable!”
8. Double click C:\Utils\RouterUsageStats.vbs to run the script
9. Check the C:\Utils folder for a new file called “C:\Utils\Usage.log”
10. If the above works OK then setup a task schedule to run the script as frequently as you want.
11. Your data will accumulate in the above log file
12. I use Excel to model this data


RouterUsageStats.vbs
‘ ”””””””””””””””””””””””””””””””””’
‘ The following code extracts both upload and download byte counts
‘ from a Netgear DG834G router and writes them into a log file.
‘ Schedule this utility to run from Task manager every 15mins
‘ (for example).
‘ NOTE: this utility relies on a DLL file, “socket.dll” – which can
‘ be downloaded free from http://tech.dimac.net/ – It is called
‘ “w3sockets”.
‘ Output is appended to the bottom of C:\utils\usage.log
‘ Each line looks like: “02/09/2009 22:15:00 455400415 501201425″
‘ The two numbers being cumulative totals for uploads and downloads
‘ respectively.
‘ 2/9/09
‘ ”””””””””””””””””””””””””””””””””’

Dim w3sock, contents, TxTotalBytesStart, TxTotalBytesEnd, RxTotalBytesStart, RxTotalBytesEnd
Dim TxTotalBytes, RxTotalBytes, DataLine
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set w3sock = CreateObject(“Socket.TCP”)
Set fso = CreateObject(“Scripting.FileSystemObject”)
w3sock.DoTelnetEmulation = True
w3sock.TelnetEmulation = “TTY”
w3sock.Host = “192.168.2.1:23″
w3sock.Open
w3sock.SendLine “cat /proc/avalanche/avsar_modem_stats | grep ‘ Total Bytes:’”
‘ the above line extracts the following from the router
‘ ”””””””””””””””””””””””””””””””””’
‘ cat /proc/avalanche/avsar_modem_stats | grep ‘ Total ‘



‘ BusyBox v0.61.pre (2008.06.11-10:37+0000) Built-in shell (ash)
‘ Enter ‘help’ for a list of built-in commands.

‘ # cat /proc/avalanche/avsar_modem_stats | grep ‘ Total ‘
‘ Tx Total Bytes: 396768076
‘ Rx Total Bytes: 332285881
‘ #
‘ #
‘ ”””””””””””””””””””””””””””””””””’
w3sock.WaitFor “Rx Total Bytes:”
‘Wscript.echo w3Sock.Buffer
w3sock.Close
Set outfile = fso.OpenTextFile(“c:\Utils\usage.log”,ForAppending,true)
contents = w3Sock.Buffer
‘ ”””””””””””””””””””””””””””””””””’
‘ Parse ‘contents’ for TX and RX numbers
‘ ”””””””””””””””””””””””””””””””””’
TxTotalBytesStart = 16 +(InStr(contents, “Tx Total Bytes: “))
TxTotalBytesEnd = InStr(TxTotalBytesStart, contents, Chr(13))
TxTotalBytes = Mid(contents, TxTotalBytesStart, (TxTotalBytesEnd – TxTotalBytesStart))
RxTotalBytesStart = 16 +(InStr(contents, “Rx Total Bytes: “))
RxTotalBytesEnd = InStr(RxTotalBytesStart, contents, Chr(13))
RxTotalBytes = Mid(contents, RxTotalBytesStart, (RxTotalBytesEnd – RxTotalBytesStart))
‘ ”””””””””””””””””””””””””””””””””’
‘ Assemble & output data to log file
‘ ”””””””””””””””””””””””””””””””””’
DataLine = Date &” “& Time &chr(9)& TxTotalBytes &chr(9)& RxTotalBytes
outfile.WriteLine DataLine
outfile.Close

Leave a Reply