E.1 synchronousManager.Java


The following is a listing of the Java program:

[View full width]
 
[View full width]
import javax.management.snmp.SnmpDefinitions; import javax.management.snmp.SnmpOid; import javax.management.snmp.SnmpVarBindList; import javax.management.snmp.manager.SnmpPeer; import javax.management.snmp.manager.SnmpParameters; import javax.management.snmp.manager.SnmpRequest; import javax.management.snmp.manager.SnmpSession; //import javax.management.snmp.manager.SnmpEventReportDispatcher; import com.sun.jdmk.Trace; import com.sun.jdmk.snmp.SnmpOidTableSupport; /** * To use the SNMP agent provided as part of JDMK, use port 8085. */ public class SynchronousManager { /** * The main entry point. When calling the program, the command * line must contain: * 1) operation: GET, GETNEXT. * 2) target: IP address or DNS name of the remote agent * 3) community: string for the operation * 4) OID: the OID of interest * 5) value: Object instance value (for set operations) * 6) port: port number to use */ public static void main(String argv[]) { final int numParameters = 6; if (argv.length != numParameters) { usage(); java.lang.System.exit(1); } String snmpOp = argv[0]; String host = argv[1]; String community = argv[2]; String requiredOid = argv[3]; String oidValue = argv[4]; String port = argv[5]; try { /* Initialize the JDMK SNMP Manager API. Specify the OidTable containing the SNMP MIB II data. Use the OidTable generated by mibgen after compiling MIB II. */ SnmpOidTableSupport oidTable = new RFC1213_MIBOidTable(); SnmpOid.setSnmpOidTable(oidTable); // Create a SnmpPeer object for representing the remote entity. SnmpPeer agent = new SnmpPeer(host, Integer.parseInt(port)); // Create required parameters for the remote entity. // We expect the remote agent to support a public and private community SnmpParameters params = new SnmpParameters(community, "private"); // Associate the parameters with the remote agent. agent.setSnmpParam(params); // Create the session for controlling the request. SnmpSession session= new SnmpSession("SyncManager session"); // A default peer (agent) is now associated with an SnmpSession. session.setDefaultPeer(agent); // Build the varbind list. SnmpVarBindList list = new SnmpVarBindList("SyncManager varbind list"); list.addVarBind(requiredOid); // Issue the required SNMP request and wait for the result. issueRequest(agent, snmpOp, list, session, host, port); // Destroy the session session.destroySession(); java.lang.System.exit(0); } catch(Exception e) { java.lang.System.err.println("Exception occurred:" + e ); e.printStackTrace(); } } /** * Return command line usage of the program. */ public static void usage() { java.lang.System.out.println("java SynchronousManager <op> <host> <community> graphics/ccc.gif <OID> <value> <port>"); java.lang.System.out.println("where"); java.lang.System.out.println("\t-op: required SNMP operation."); java.lang.System.out.println("\t-host: hostname or IP address of the SNMP agent. graphics/ccc.gif "); java.lang.System.out.println("\t-community: string for the operation."); java.lang.System.out.println("\t-OID: required object instance identifier."); java.lang.System.out.println("\t-value: of the object instance (for set only)."); java.lang.System.out.println("\t-port: port number for the remote agent."); } /** * Issue the required SNMP message. */ public static void issueRequest(SnmpPeer agent, String operation, SnmpVarBindList list, SnmpSession session, String host, String port) { final String GEToperation = "GET"; final String GETNEXToperation = "GETNEXT"; final String SEToperation = "SET"; SnmpRequest request = null; try { // Make the SNMP request and wait for the result. if (operation.compareToIgnoreCase(GEToperation) == 0) { request = session.snmpGetRequest(null, list); } else if (operation.compareToIgnoreCase(GETNEXToperation) == 0) { request = session.snmpGetNextRequest(null, list); } else { java.lang.System.out.println("Unknown operation. Exiting..."); java.lang.System.exit(0); } java.lang.System.out.println("Sent " + operation + " request to agent on " + graphics/ccc.gif host + " port " + port); boolean completed = request.waitForCompletion(10000); // Check for a timeout of the request. if (completed == false) { java.lang.System.out.println("Request timed out. Check reachability of graphics/ccc.gif agent"); // Print request. java.lang.System.out.println("Request: " + request.toString()); java.lang.System.exit(0); } // Check if the response contains an error. int errorStatus = request.getErrorStatus(); if (errorStatus != SnmpDefinitions.snmpRspNoError) { java.lang.System.out.println("Error status = " + SnmpRequest. graphics/ccc.gif snmpErrorToString(errorStatus)); java.lang.System.out.println("Error index = " + request.getErrorIndex()); java.lang.System.exit(0); } // Display the result. SnmpVarBindList result = request.getResponseVarBindList(); java.lang.System.out.println("Result: \n" + result); } catch(Exception e) { java.lang.System.err.println("Exception occurred:" + e ); e.printStackTrace(); } } }


Network Management, MIBs and MPLS
Network Management, MIBs and MPLS: Principles, Design and Implementation
ISBN: 0131011138
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net