<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WEB-NES-BAY &#187; Snmp Tutorial</title>
	<atom:link href="http://webnesbay.com/tag/snmp-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://webnesbay.com</link>
	<description>Learn Tips and tricks on Linux, Hacking, linux, PHP, Perl, Web, Hardware</description>
	<lastBuildDate>Sun, 11 Apr 2010 05:12:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Traffic accounting with SNMP and Python</title>
		<link>http://webnesbay.com/traffic-accounting-with-snmp-and-python/</link>
		<comments>http://webnesbay.com/traffic-accounting-with-snmp-and-python/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 12:03:29 +0000</pubDate>
		<dc:creator>WEBNESBAY</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Adsl Line]]></category>
		<category><![CDATA[Adsl Modem]]></category>
		<category><![CDATA[Adsl Modems]]></category>
		<category><![CDATA[Batch Files]]></category>
		<category><![CDATA[Eol]]></category>
		<category><![CDATA[Glob]]></category>
		<category><![CDATA[Interface Names]]></category>
		<category><![CDATA[Network Interface]]></category>
		<category><![CDATA[Network Topology]]></category>
		<category><![CDATA[Octets]]></category>
		<category><![CDATA[Old Folks]]></category>
		<category><![CDATA[Python Script]]></category>
		<category><![CDATA[Snmp Oid]]></category>
		<category><![CDATA[Snmp Tools]]></category>
		<category><![CDATA[Snmp Tutorial]]></category>
		<category><![CDATA[Snmp Windows]]></category>
		<category><![CDATA[Traffic Data]]></category>
		<category><![CDATA[Windows Batch File]]></category>
		<category><![CDATA[Windows Operating System]]></category>
		<category><![CDATA[Windows Scheduled Task]]></category>

		<guid isPermaLink="false">http://webnesbay.com/?p=243</guid>
		<description><![CDATA[This tutorial is based on windows operating system
If you&#8217;re not on a flat-rate ISP data-plan, and your ISP made you pay extra last month such as myself   You might be interested in monitoring your home bandwidth. A nifty little features of most routers or ADSL modems, is support for SNMP. Since both me and my old folks use the same ADSL line, any host based monitoring solution wouldn&#8217;t really work. I had to monitor the usage using SNMP. [...]


Related posts:<ol><li><a href='http://webnesbay.com/browse-remote-directories-in-python/' rel='bookmark' title='Permanent Link: Browse remote directories in python'>Browse remote directories in python</a></li>
<li><a href='http://webnesbay.com/user-defined-exceptions-in-python/' rel='bookmark' title='Permanent Link: User defined exceptions in python'>User defined exceptions in python</a></li>
<li><a href='http://webnesbay.com/writing-daemons-in-python/' rel='bookmark' title='Permanent Link: writing daemons in python'>writing daemons in python</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This tutorial is based on windows operating system</p>
<p>If you&#8217;re not on a flat-rate ISP data-plan, and your ISP made you pay extra last month such as myself <img src='http://webnesbay.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  You might be interested in monitoring your home bandwidth. A nifty little features of most routers or ADSL modems, is support for <a href="http://en.wikipedia.org/wiki/Simple_Network_Management_Protocol">SNMP</a>. Since both me and my old folks use the same ADSL line, any host based monitoring solution wouldn&#8217;t really work. I had to monitor the usage using SNMP. Since my folks are using Windows, the solution had to work on that. The solution overview is:</p>
<p>1- Enable SNMP on ADSL modem<br />
2- Use a windows scheduled task to run a windows Batch file, that uses <a href="http://www.elifulkerson.com/articles/net-snmp-windows-binary-unofficial.php">SNMP-tools</a> for windows to pull data from the router<br />
3- Use a windows scheduled task to run a python script that processes the batch file output and computes the total traffic</p>
<p>Data from the batch files are written in a file signifying the current month (example 05.txt for May). Here is the code for the batch file</p>
<pre><span style="color: #0000ff;">cd e:\traffic

e:\

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B

FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B

echo %mm%

snmpget.exe -c public -O v -v 1 192.168.11.1 .1.3.6.1.2.1.2.2.1.10.12 &gt;&gt; %mm%.txt
</span></pre>
<p>Note that the IP 192.168.11.1 should be replaced with the internal IP of your ADSL router. Also, the SNMP OID .1.3.6.1.2.1.2.2.1.10.12 (symbolically: IF-MIB::ifInOctets.12) is the incoming octets on network interface &#8220;12&#8243;. You might need to change that last &#8220;12&#8243; to represent your network topology. You can use the &#8220;snmpwalk&#8221; command to list your interface names like:</p>
<p>snmpwalk -c public -v1 192.168.11.1</p>
<p>this yields</p>
<p>&#8230;</p>
<p>IF-MIB::ifDescr.12 = STRING: ppp0</p>
<p>&#8230;</p>
<p>I know that my ppp0 is the link to my ISP, so that&#8217;s the interface I used</p>
<p>And here is the code for the python script</p>
<pre>
</pre>
<pre><span><span style="font-family: monospace;"><span style="color: #0000ff;"><strong># -*- coding: utf-8 -*-</strong>

</span><span style="color: #0000ff;"><strong>import</strong></span><span style="color: #0000ff;"> glob
</span><span style="color: #0000ff;"><strong>import</strong></span><span style="color: #0000ff;"> locale
</span><span style="color: #0000ff;"><strong>import</strong></span><span style="color: #0000ff;"> os
locale.setlocale(locale.LC_ALL, '')
reports = sorted(glob.glob('</span><span style="color: #0000ff;"><strong>??.txt</strong></span><span style="color: #0000ff;">'))
totalsReport = open('</span><span style="color: #0000ff;"><strong>totals.txt</strong></span><span style="color: #0000ff;">','</span><span style="color: #0000ff;"><strong>w</strong></span><span style="color: #0000ff;">')
totalsReport.write('</span><span style="color: #0000ff;"><strong> Month      Traffic </strong></span><span style="color: #0000ff;"><strong>\n</strong></span><span style="color: #0000ff;">')
totalsReport.write('</span><span style="color: #0000ff;"><strong>====================</strong></span><span style="color: #0000ff;"><strong>\n</strong></span><span style="color: #0000ff;">')
</span><span style="color: #0000ff;"><strong>for</strong></span><span style="color: #0000ff;"> report </span><span style="color: #0000ff;"><strong>in</strong></span><span style="color: #0000ff;"> reports:
        datastring = open(report,'</span><span style="color: #0000ff;"><strong>r</strong></span><span style="color: #0000ff;">').readlines()
        data = [ int(datastring[i].strip().split()[1]) </span><span style="color: #0000ff;"><strong>for</strong></span><span style="color: #0000ff;"> i </span><span style="color: #0000ff;"><strong>in</strong></span><span style="color: #0000ff;"> range(len(datastring)) ]
        diff = [ data[i+1] - data[i] </span><span style="color: #0000ff;"><strong>for</strong></span><span style="color: #0000ff;"> i </span><span style="color: #0000ff;"><strong>in</strong></span><span style="color: #0000ff;"> range(len(data) - 1) ]
        trafficDeltas = [ (data[i+1],diff[i])[diff[i] &gt; 0] </span><span style="color: #0000ff;"><strong>for</strong></span><span style="color: #0000ff;"> i </span><span style="color: #0000ff;"><strong>in</strong></span><span style="color: #0000ff;"> range(len(diff)) ]
        totalTraffic = sum(trafficDeltas)
        totalsReport.write('</span><span style="color: #0000ff;"><strong> %s =&gt; %s </strong></span><span style="color: #0000ff;"><strong>\n</strong></span><span style="color: #0000ff;">'% (report.replace('</span><span style="color: #0000ff;"><strong>.txt</strong></span><span style="color: #0000ff;">','') , locale.format('</span><span style="color: #0000ff;"><strong>%d</strong></span><span style="color: #0000ff;">', totalTraffic, True)) )

totalsReport.close()</span>

</span></span></pre>
<p>Glancing over the Python code, you can already see I&#8217;m a list comprehension addict <img src='http://webnesbay.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  The code reads the snmp bytes values from ??.txt (example: 05.txt), computes bandwidth deltas, handles counter resets (32bit overflow, or router loosing power) and computes totals. Then writes the total to a file named &#8220;totals.txt&#8221;. This solution is resistant to the router loosing power and resetting its internal traffic counter back to zero. This is because the data is continuously being saved to non-volatile medium (PC&#8217;s hard-disk). Also, the Windows PC running those two scripts, does NOT need to be running 24&#215;7 .. Just most of the time. If however the PC is powered off, AND the modem power cycles or hangs, you might loose some accuracy, the results will still be fairly valid though. Hope that&#8217;s helpful to anyone out there</p>
<pre></pre>


<p>Related posts:<ol><li><a href='http://webnesbay.com/browse-remote-directories-in-python/' rel='bookmark' title='Permanent Link: Browse remote directories in python'>Browse remote directories in python</a></li>
<li><a href='http://webnesbay.com/user-defined-exceptions-in-python/' rel='bookmark' title='Permanent Link: User defined exceptions in python'>User defined exceptions in python</a></li>
<li><a href='http://webnesbay.com/writing-daemons-in-python/' rel='bookmark' title='Permanent Link: writing daemons in python'>writing daemons in python</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://webnesbay.com/traffic-accounting-with-snmp-and-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
