/* ---------------------------------------------------------------------------- Copyright (c) 2003 jSNMP Enterprises, All Rights Reserved Worldwide ------------------------------------------------------------------------------ Disclaimer: This software is provided AS IS, and without any warranty other than those which may be provided in a separate writing specifically referencing the software contained herein. All other warranties, except those which may be provided separately in writing, are expressly disclaimed. WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THERE IS NO WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE which is given with respect to this software or the operability thereof. ------------------------------------------------------------------------ */ // ------------------------------ Package ------------------------------------- // ------------------------------ Imports ------------------------------------- import java.net.*; import java.rmi.RemoteException; import java.rmi.NotBoundException; import java.io.UnsupportedEncodingException; import com.outbackinc.services.protocol.snmp.*; import com.outbackinc.services.protocol.snmp.CSM.*; import com.outbackinc.services.protocol.snmp.rmi.*; /** * Sample which illustrates how to use the SnmpService under RMI to place * get-next orders. In this case, the walker walks the remote * agent's MIB until the end is reached. */ public class RmiV1WalkerTest implements SnmpCustomer { // ------------------------------ Constants --------------------------------- // ------------------------------ Class variables --------------------------- // ------------------------------ Member (instance) variables --------------- private SnmpService m_cSnmpService; private SnmpAuthoritativeSession m_cSnmpRemoteAuthoritativeSession; private SnmpOrderInfo m_cSnmpOrderInfo; private String[] m_szLastOID; private int m_iOrderNumStart; // ------------------------------ Methods ----------------------------------- /** * Constructor */ public RmiV1WalkerTest(String szRmiHost, String szHost, String szReadCommunity) { //Sets the initial OID to something which should be before the table m_szLastOID = new String[1]; m_szLastOID[0] = "1.3.6"; //Initializes the Snmp Service m_cSnmpOrderInfo = new SnmpOrderInfo(2, 3, 0); CSMSecurityInfo cCSMSecurityInfo; try { cCSMSecurityInfo = new CSMSecurityInfo(szReadCommunity.getBytes("ASCII"), szReadCommunity.getBytes("ASCII")); } catch(UnsupportedEncodingException uee) { cCSMSecurityInfo = new CSMSecurityInfo(szReadCommunity.getBytes(), szReadCommunity.getBytes()); } try { RMISnmpClient m_cRMISnmpClient = new RMISnmpClient(szRmiHost); SnmpAuthoritativeSessionFactory cSnmpAuthoritativeSessionFactory = m_cRMISnmpClient.getAuthoritativeSessionFactory(); m_cSnmpRemoteAuthoritativeSession = cSnmpAuthoritativeSessionFactory.createRemoteAuthoritativeSession(szHost, 161, SnmpConstants.SNMP_VERSION_1, cCSMSecurityInfo); m_cSnmpService = m_cRMISnmpClient.getService(); } catch (RemoteException re) { System.out.println("RMI communication failure with " + szRmiHost + " : " + re.getMessage()); return; } catch (NotBoundException nbe) { System.out.println("RMI communication failure with " + szRmiHost + " : " + nbe.getMessage()); return; } catch (MalformedURLException mue) { System.out.println("RMI communication failure with " + szRmiHost + " : " + mue.getMessage()); return; } catch (UnknownHostException uhe) { System.out.println("Unable to create session with remote agent as host (" + szHost + ") is unknown."); m_cSnmpService.stop(); return; } catch (SnmpSecurityException sse) { System.out.println("Unable to create secure session with remote agent " + szHost + " (" + sse.getMessage() + ")."); m_cSnmpService.stop(); return; } m_iOrderNumStart = 1; //Starts the walker walking System.out.println("Walking MIB Tree of " + szHost + " using SNMPv1"); getNext(); } /** * Main method to start execution * * @param szArgs contains cmdline args, first of which must be the szHost, second must be read community * */ public static void main(String[] szArgs) { if (szArgs.length < 3) { usage(); return; } String szRmiHost = szArgs[0]; String szHost = szArgs[1]; try { String szReadCommunity = szArgs[2]; //Creates new walker RmiV1WalkerTest cRmiV1WalkerTest = new RmiV1WalkerTest(szRmiHost, szHost, szReadCommunity); } catch (Exception e) { e.printStackTrace(); } } /** * * Displays correct usage */ public static void usage() { System.out.println("Usage : java RmiV1WalkerTest "); System.out.println("\trmihostname : host on which jSNMP RMIServer resides"); System.out.println("\thostname : agent on which agent resides to walk"); System.out.println("\treadcommunity : read community of agent to walk"); } //SnmpCustomer Implementation /** * callback for delivering successes. In this case the implementation * is to print out the Object ID received and then use it for the basis * for the get-next request */ public void deliverSuccessfulOrder(int iOrderNum, SnmpVarBind cSnmpVarBind) { if (m_szLastOID[0].compareTo(cSnmpVarBind.getName()) != 0) { System.out.println(iOrderNum + ": " + cSnmpVarBind.getName() + " (" + cSnmpVarBind.getStringValue() + ")"); m_szLastOID[0] = cSnmpVarBind.getName(); getNext(); } else { System.out.println("Finished"); m_cSnmpService.stop(); m_cSnmpService = null; m_cSnmpRemoteAuthoritativeSession = null; m_cSnmpOrderInfo = null; System.exit(0); } } /** * callback for failures. In this case if a failure is received, * stop walking the MIB */ public void deliverFailedOrder(int iOrderNum, int iErrorStatus) { System.out.println(iOrderNum + ": order failed (" + SnmpConstants.errorIDToString(iErrorStatus) + ")"); System.out.println("Ending MIB Walk"); m_cSnmpService.stop(); m_cSnmpService = null; m_cSnmpRemoteAuthoritativeSession = null; m_cSnmpOrderInfo = null; System.exit(0); } /** * gets the next object from the remote agent. Demonstrates * placing orders with the snmp service */ public void getNext() { m_iOrderNumStart = m_cSnmpService.placeGetNextOrder(m_cSnmpRemoteAuthoritativeSession, m_cSnmpOrderInfo, false, m_szLastOID, this, m_iOrderNumStart); } }