<?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>Ashish's Tech Blog &#187; SNMP</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/category/snmp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Multi Threaded Trap Receiver using SNMP4J</title>
		<link>http://www.ashishpaliwal.com/blog/2008/12/multi-threaded-trap-receiver-using-snmp4j/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/12/multi-threaded-trap-receiver-using-snmp4j/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 08:12:45 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[SMPP]]></category>
		<category><![CDATA[SNMP]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=231</guid>
		<description><![CDATA[The entry briefly touched upon creating a multithreaded Trap receiver using SNMP4J. This is based on example shipped with SNMP4J Pre-requisite  SNMP4J  Log4j Structure For complete source have a look at the class org.snmp4j.test. MultiThreadedTrapReceiver.java shipped with SNMP4J distribution Logical structure of the program Create Socket for accepting Traps Create a Thread pool to process [...]]]></description>
			<content:encoded><![CDATA[<p>The entry briefly touched upon creating a multithreaded Trap receiver using SNMP4J. This is based on example shipped with SNMP4J</p>
<p><strong>Pre-requisite</strong></p>
<ul>
<li><span> </span>SNMP4J</li>
<li><span> </span>Log4j</li>
</ul>
<p><strong>Structure</strong></p>
<p>For complete source have a look at the class org.snmp4j.test. MultiThreadedTrapReceiver.java shipped with SNMP4J distribution</p>
<p><em>Logical structure of the program</em></p>
<ul>
<li>Create Socket for accepting Traps</li>
<li>Create a Thread pool to process PDU’s</li>
<li>Process the Traps</li>
</ul>
<p>Let’s get into the code</p>
<p>Lets address the Listener creation steps</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+UnknownHostException"><span style="color: #aaaadd; font-weight: bold;">UnknownHostException</span></a>, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/IOException.html"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    threadPool = ThreadPool.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Trap&quot;</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    dispatcher = MultiThreadedMessageDispatcher<span style="color: #66cc66;">&#40;</span>threadPool,
                                           <span style="color: #000000; font-weight: bold;">new</span> MessageDispatcherImpl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    listenAddress = GenericAddress.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">getProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;snmp4j.listenAddress&quot;</span>,
                                                <span style="color: #ff0000;">&quot;udp:0.0.0.0/162&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    TransportMapping transport;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>listenAddress <span style="color: #000000; font-weight: bold;">instanceof</span> UdpAddress<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      transport = <span style="color: #000000; font-weight: bold;">new</span> DefaultUdpTransportMapping<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>UdpAddress<span style="color: #66cc66;">&#41;</span>listenAddress<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
      transport = <span style="color: #000000; font-weight: bold;">new</span> DefaultTcpTransportMapping<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>TcpAddress<span style="color: #66cc66;">&#41;</span>listenAddress<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    snmp = <span style="color: #000000; font-weight: bold;">new</span> Snmp<span style="color: #66cc66;">&#40;</span>dispatcher, transport<span style="color: #66cc66;">&#41;</span>;
    snmp.<span style="color: #006600;">getMessageDispatcher</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addMessageProcessingModel</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MPv1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    snmp.<span style="color: #006600;">getMessageDispatcher</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addMessageProcessingModel</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MPv2c<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    snmp.<span style="color: #006600;">getMessageDispatcher</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addMessageProcessingModel</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MPv3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    USM usm = <span style="color: #000000; font-weight: bold;">new</span> USM<span style="color: #66cc66;">&#40;</span>SecurityProtocols.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,
                      <span style="color: #000000; font-weight: bold;">new</span> OctetString<span style="color: #66cc66;">&#40;</span>MPv3.<span style="color: #006600;">createLocalEngineID</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
    SecurityModels.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addSecurityModel</span><span style="color: #66cc66;">&#40;</span>usm<span style="color: #66cc66;">&#41;</span>;
    snmp.<span style="color: #006600;">listen</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span></pre>
<p>Here we see that we have created Transport mapping to Listener on port 162. Next we add all three Message processing models, so that we can process Traps received from all three versions of SNMP (V1, V2C and V3). For SNMPv3, we add a Security model.</p>
<p>That’s it done. We listen and process the traps.</p>
<p>The default implementation of processPdu() in this class, just dumps some statistics on the output.</p>
<p><strong>Customization’s</strong></p>
<p>Let’s see what all can we do with this default example</p>
<ul>
<li>Set only the SNMP version you want to support</li>
<li>Customize the Security Model for SNMP V3</li>
<li>The whole logic lies on processPDU(). You can take complete control of how to process the message and subsequent processing of the message like dumping it to database etc.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/12/multi-threaded-trap-receiver-using-snmp4j/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
