www.pudn.com > sxg.rar > SnmpHandler.java
/*
* Copyright (c) 2003 Jens Mueller
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
An instance of this class provides SNMP-Communication using the jmgmt-API.
Has to be used in the order "instantiate" -> "connect" -> "make request"
@author Jens Mueller
*/
import jmgmt.snmp.io.SnmpConnection;
import jmgmt.snmp.io.SnmpPeer;
import jmgmt.snmp.io.Varbind;
import jmgmt.snmp.io.SnmpDecodeException;
import jmgmt.snmp.io.SnmpResponseException;
import jmgmt.asn1.OID;
import jmgmt.asn1.Asn1Value;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Vector;
import java.io.IOException;
public class SnmpHandler
{
private SnmpConnection snmpConnection;
private SnmpPeer snmpPeer;
/**
constructs a new SnmpHandler by instantiating jmgmt.snmp.io.SnmpPeer with
the submitted parameters.
@param name @param inetAddress @param port @param timeout @param commString */
public SnmpHandler( String name,
InetAddress inetAddress,
int port,
int[] timeout,
String commString )
{
snmpPeer = new SnmpPeer(name, inetAddress, port, timeout, commString);
}
/**
instantiates jmgmt.snmp.io.SnmpConnection on the represented
SnmpPeer.
@throws SocketException */
public void connect() throws SocketException
{
snmpConnection = new SnmpConnection( snmpPeer );
}
/**
makes a standard single set-request
@param oidstr the OBJECT IDENTIFIER of the request @param data the data that should be submitted @param castType one of "string" or "int". the type to which the data
should be casted. @throws SnmpAccessException */
public void setRequest( String oidstr, String data, String castType )
throws SnmpAccessException
{
OID oid = new OID( oidstr );
Vector varbinds = new Vector();
Asn1Value asn1Val = null;
try
{
if( castType.equals( "string" ) )
//asn1Val = Asn1Value.decode( data.getBytes() );
asn1Val = new Asn1Value( data );
if( castType.equals( "int" ) )
asn1Val = new Asn1Value( Integer.parseInt( data ) );
Varbind reqBind = new Varbind( oid, asn1Val );
varbinds.add( reqBind );
Varbind[] res = snmpConnection.setRequest( varbinds );
//check if data has been set
String retval = "";
Asn1Value[] asn1res = res[ 0 ].encode().getArray();
int i = 1;
while (i < asn1res.length)
{
if (asn1res[i].content instanceof String)
retval += asn1res[i].getString();
if (asn1res[i].content instanceof Long)
retval += asn1res[i].getLong();
if (asn1res[i].content instanceof Integer)
retval += asn1res[i].getInt();
if (asn1res[i].content instanceof OID)
retval += (( OID )asn1res[i].content).toString();
i++;
}
if( !retval.equals( data ) )
throw new SnmpResponseException();
}
catch( SnmpDecodeException e )
{
throw new SnmpAccessException( "Exception during set: " + e.toString() );
}
catch( SnmpResponseException e )
{
throw new SnmpAccessException( "Exception during set: " + e.toString() );
}
catch( IOException e )
{
throw new SnmpAccessException( "Exception during set: " + e.toString() );
}
}
/**
makes a standard single get-request
@param oidstr the OBJECT IDENTIFIER of the request
@return SnmpGetRequestResult @throws SnmpAccessException */
public SnmpGetRequestResult getRequest( String oidstr ) throws SnmpAccessException
{
String retval = "";
OID oid = new OID( oidstr );
Varbind reqBind = new Varbind( oid );
Vector varbinds = new Vector();
varbinds.add( reqBind );
try
{
Varbind[] res = snmpConnection.getRequest( varbinds );
Asn1Value[] asn1res = res[ 0 ].encode().getArray();
int i = 1;
while (i < asn1res.length)
{
if (asn1res[i].content instanceof String)
retval += asn1res[i].getString();
if (asn1res[i].content instanceof Long)
retval += asn1res[i].getLong();
if (asn1res[i].content instanceof Integer)
retval += asn1res[i].getInt();
if (asn1res[i].content instanceof OID)
retval += (( OID )asn1res[i].content).toString();
i++;
}
return new SnmpGetRequestResult( new GwOID( oid.toString() ), retval );
}
catch ( Exception e )
{
throw new SnmpAccessException( "Agent not accessible " + e.toString() );
}
}
/**
makes a standard single getnext-request
@param oidstr the OBJECT IDENTIFIER of the request
@return SnmpGetRequestResult @throws SnmpAccessException */
public SnmpGetRequestResult getNextRequest( String oidstr ) throws SnmpAccessException
{
String retval = "";
OID oid = new OID( oidstr );
Varbind reqBind = new Varbind( oid );
Vector varbinds = new Vector();
varbinds.add( reqBind );
try
{
Varbind[] res = snmpConnection.getNextRequest( varbinds );
Asn1Value[] asn1res = res[ 0 ].encode().getArray();
int i = 1;
while (i < asn1res.length)
{
if (asn1res[i].content instanceof String)
retval += asn1res[i].getString();
if (asn1res[i].content instanceof Long)
retval += asn1res[i].getLong();
if (asn1res[i].content instanceof Integer)
retval += asn1res[i].getInt();
if (asn1res[i].content instanceof OID)
retval += (( OID )asn1res[i].content).toString();
i++;
}
return new SnmpGetRequestResult(
new GwOID( (( OID ) asn1res[ 0 ].content ).toString() ), retval );
}
catch ( Exception e )
{
throw new SnmpAccessException( "Agent not accessible " + e.toString() );
}
}
}