[SNMP4J] My Hello World app !!!
Muralidharan Narayanan
murali at TriveniDigital.com
Tue Jun 14 19:22:37 CEST 2005
Hello Jaimes:
I used your HelloWorldSNMP.java application on my windows XP machine but I
am getting the following errors. Do you or anybody else who have tried this
know why it is happening.
ONe thing I can see is that the response PDU is null and that is why I am
exceptions are thrown as shown below. Does that mean there is no agent
daemon running on my PC?
Murali
******************** The program *******************
/*
* HelloWorldSNMP.java
*
* @author rodolfojcj
* @version 1.0
* This is my first app using SNMP4J package.
* My goal with it is to send a message with a GET PDU
* to my computer, which is running a snmp daemon.
* In this example, only the sysName (1.3.6.2.1.1.5.0) object is requested
*
* I've just tested it in this scenario:
* - O.S. Linux Fedora Core 1
* - snmpd service or daemon is running
* - "public" is the community
* - Only tested for SnmpV1
* - IDE NetBeans 4.0
* - JDK 1.5.0
*
* I've just followed the SnmpRequest.java program (which is part of
* the snmp4j package, corresponding to the tools) to do this example
*/
package org.snmp4j.myExamples;
import org.snmp4j.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
//import org.snmp4j.mp.*;
import java.io.IOException;
import java.util.Vector;
public class HelloWorldSNMP {
// class members
OctetString community = new OctetString( "public");
Address address = new UdpAddress( "localhost/161");
TransportMapping transport;
Snmp snmp;
Target target;
Vector vbs = new Vector();
/** Creates a new instance of HacerGetSNMPV1 */
public HelloWorldSNMP() {
// 1. create SNMP session
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp( transport );
}
catch ( IOException e )
{
}
// 2. set the target which will be requested
CommunityTarget targetX = new CommunityTarget();
targetX.setCommunity( community );
target = ( Target ) targetX;
target.setVersion( 0 );
target.setAddress( address );
target.setRetries( 1 );
target.setTimeout( 1000 );
try {
snmp.listen();
}
catch ( IOException e )
{
}
// 3. create the PDU of the message. In this case a GET
PDU request = new PDU();
request.setType( -96 ); // honestly, I don't know yet why -96 ???
// Answer: -96 == 0xA0 == PDU.GET
//
vbs.add( new VariableBinding( new OID("1.3.6.1.2.1.1.5.0" ) ) );
//for ( int i = 0; i < vbs.size(); i++ )
request.add( ( VariableBinding ) vbs.get(0 ) );
// prepare to get the response of the agent
PDU response = null;
try {
response = snmp.sendPDU( request, target);
snmp.close();
}
catch ( IOException e )
{
}
// 4. print output
System.out.println( "Response received with requestID = " +
response.getRequestID() + " , errorIndex = " +
response.getErrorIndex() + " , errorStatus = " +
response.getErrorStatus() );
for ( int i = 0; i < response.size(); i++ )
{
VariableBinding vb = response.get( i );
System.out.println( vb.toString() );
}
}
public static void main( String args[] ) {
System.out.println( "Hello World SNMP4J !!!");
System.out.println( " The beginning !!! " );
HelloWorldSNMP app = new HelloWorldSNMP();
System.out.println( " Just finished !!!" );
}
}
****************************** the output
Hello World SNMP4J !!!
The beginning !!!
Exception in thread "main" java.lang.NullPointerException
at org.snmp4j.myExamples.HelloWorldSNMP.<init>(HelloWorldSNMP.java:94)
at org.snmp4j.myExamples.HelloWorldSNMP.main(HelloWorldSNMP.java:107)
More information about the SNMP4J
mailing list