3600N/A/*
3600N/A * CDDL HEADER START
3600N/A *
3600N/A * The contents of this file are subject to the terms of the
3600N/A * Common Development and Distribution License, Version 1.0 only
3600N/A * (the "License"). You may not use this file except in compliance
3600N/A * with the License.
3600N/A *
3600N/A * You can obtain a copy of the license at
3600N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
3600N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
3600N/A * See the License for the specific language governing permissions
3600N/A * and limitations under the License.
3600N/A *
3600N/A * When distributing Covered Code, include this CDDL HEADER in each
3600N/A * file and include the License file at
3600N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
3600N/A * add the following below this CDDL HEADER, with the fields enclosed
3600N/A * by brackets "[]" replaced with your own identifying information:
3600N/A * Portions Copyright [yyyy] [name of copyright owner]
3600N/A *
3600N/A * CDDL HEADER END
3600N/A *
3600N/A *
3600N/A * Copyright 2008 Sun Microsystems, Inc.
3600N/A */
3600N/A
3600N/A// OpenDS imports
3600N/Aimport org.opends.server.snmp.DIRECTORY_SERVER_MIBOidTable;
3600N/A
3600N/A// OpenDMK imports
3600N/A//
3600N/Aimport com.sun.management.snmp.SnmpDefinitions;
3600N/Aimport com.sun.management.snmp.SnmpVarBindList;
3600N/Aimport com.sun.management.snmp.SnmpEngine;
3600N/Aimport com.sun.management.snmp.SnmpEngineParameters;
3600N/Aimport com.sun.management.snmp.SnmpOid;
3600N/Aimport com.sun.management.snmp.SnmpOidTableSupport;
3600N/Aimport com.sun.management.snmp.SnmpStatusException;
3600N/Aimport com.sun.management.snmp.manager.SnmpRequest;
3600N/Aimport com.sun.management.snmp.manager.SnmpSession;
3600N/Aimport com.sun.management.snmp.manager.SnmpPeer;
3600N/Aimport com.sun.management.snmp.manager.SnmpParameters;
3600N/Aimport com.sun.management.snmp.manager.usm.SnmpUsmPeer;
3600N/Aimport com.sun.management.snmp.manager.usm.SnmpUsmParameters;
3600N/A
3600N/A/**
3600N/A * This class perform a SNMP get operation.
3600N/A */
3600N/Apublic class SNMPGet {
3600N/A
3600N/A /**
3600N/A * Gets the calling arguments.
3600N/A *
3600N/A * @param args SNMP version + SNMP agent host + SNMP agent port + community
3600N/A * @return 0 if the init succeeded or 1 if the init failed
3600N/A */
3600N/A public int init(String[] args) {
3600N/A int rc = 0;
3600N/A
3600N/A System.out.println("\n");
3600N/A
3600N/A if (args.length < 5) {
3600N/A // Missing arguments
3600N/A System.out.println(
3600N/A "usage: " +
3600N/A " -v <SNMP version>" +
3600N/A " -h <remoteHost>" +
3600N/A " -p <port>" +
3600N/A " -o <oids>" +
3600N/A " -c <community/context>" +
3600N/A " -u <user>" +
3600N/A " -l <securityLevel>" +
3600N/A " -f <securityFile>" +
3600N/A " -s <connectionStatus>" +
3667N/A " -n <checkOIDs>" +
3667N/A " -w <snmpwalk>");
3600N/A rc = 1;
3600N/A } else {
3600N/A for (int i = 0; i < args.length; i++) {
3600N/A String opt = args[i];
3600N/A String val = args[i + 1];
3600N/A
3600N/A switch (opt.charAt(1)) {
3600N/A case 'v':
3600N/A version = new Integer(val).intValue();
3600N/A break;
3600N/A case 'h':
3600N/A remoteHost = val;
3600N/A break;
3600N/A case 'p':
3600N/A port = new Integer(val).intValue();
3600N/A break;
3600N/A case 'o':
3600N/A oids = val;
3600N/A break;
3600N/A case 'c':
3600N/A community = val;
3600N/A break;
3600N/A case 'u':
3600N/A user = val;
3600N/A break;
3600N/A case 'l':
3657N/A if (val.compareTo("noauthnopriv") == 0) {
3600N/A securityLevel = SnmpDefinitions.noAuthNoPriv;
3657N/A } else if (val.compareTo("authnopriv") == 0) {
3600N/A securityLevel = SnmpDefinitions.authNoPriv;
3657N/A } else if (val.compareTo("authpriv") == 0) {
3600N/A securityLevel = SnmpDefinitions.authPriv;
3600N/A } else {
3600N/A System.out.println(
3600N/A "Unknown security level " + opt.charAt(1) + ".");
3600N/A rc = 1;
3600N/A }
3600N/A break;
3600N/A case 'f':
3600N/A securityFile = val;
3600N/A break;
3600N/A case 's':
3600N/A connectStatus = val;
3600N/A break;
3600N/A case 'n':
3600N/A validOIDs = new Boolean(val).booleanValue();
3600N/A break;
3667N/A case 'w':
3667N/A walk = new Boolean(val).booleanValue();
3667N/A break;
3600N/A default:
3600N/A System.out.println("Unknown option -" + opt.charAt(1) + ".");
3600N/A rc = 1;
3600N/A }
3600N/A
3600N/A if (rc == 1) {
3600N/A break;
3600N/A }
3600N/A
3600N/A i = i + 1;
3600N/A }
3600N/A }
3600N/A
3600N/A if (rc == 0) {
3600N/A System.out.println("init() of SNMPGet succeeded");
3600N/A } else {
3600N/A System.out.println("init() of SNMPGet failed");
3600N/A }
3600N/A
3600N/A return rc;
3600N/A }
3600N/A
3600N/A /**
3600N/A * Open SNMP connection with SNMP agent.
3600N/A *
3600N/A * @return 0 if the connect succeeded or 1 if the connect failed
3600N/A */
3600N/A public int connect() {
3600N/A int rc = 0;
3600N/A
3600N/A try {
3600N/A // The OidTable generated by mibgen when compiling DIRECTORY_SERVER_MIB
3600N/A //
3600N/A final SnmpOidTableSupport oidTable = new DIRECTORY_SERVER_MIBOidTable();
3600N/A
3600N/A // Specify the OidTable containing all the DIRECTORY_SERVER_MIB knowledge
3600N/A //
3600N/A SnmpOid.setSnmpOidTable(oidTable);
3600N/A
3600N/A switch (version) {
3600N/A case 1:
3600N/A case 2:
3600N/A // Create the session
3600N/A //
3600N/A session = new SnmpSession("Get V" + version + " session");
3600N/A
3600N/A // Disable PduFixedOnError option
3600N/A //
3600N/A session.snmpOptions.setPduFixedOnError(false);
3600N/A
3600N/A // Create an SnmpPeer object for representing the entity
3600N/A // to communicate with.
3600N/A //
3600N/A final SnmpPeer agent = new SnmpPeer(remoteHost, port);
3600N/A
3600N/A // Specify the read and write community to be used
3600N/A //
3600N/A final SnmpParameters params = new SnmpParameters(
3600N/A community,
3600N/A community);
3600N/A
3600N/A // Specify the protocol version
3600N/A //
3600N/A switch (version) {
3600N/A case 1:
3600N/A params.setProtocolVersion(SnmpDefinitions.snmpVersionOne);
3600N/A break;
3600N/A case 2:
3600N/A params.setProtocolVersion(SnmpDefinitions.snmpVersionTwo);
3600N/A break;
3600N/A }
3600N/A
3600N/A // Associate the parameters with the agent
3600N/A //
3600N/A agent.setTimeout(timeOut);
3600N/A agent.setMaxTries(maxRetries);
3600N/A agent.setParams(params);
3600N/A
3600N/A // Set the default peer (agent) to a SnmpSession
3600N/A //
3600N/A session.setDefaultPeer(agent);
3600N/A break;
3600N/A case 3:
3600N/A // Custom engine parameters
3600N/A final SnmpEngineParameters engineParameters =
3600N/A new SnmpEngineParameters();
3667N/A
3667N/A // Activate encryption
3667N/A engineParameters.activateEncryption();
3667N/A
3667N/A // Set the security file
3600N/A engineParameters.setSecurityFile(securityFile);
3600N/A
3600N/A // Create the session
3600N/A //
3600N/A session = new SnmpSession(
3600N/A engineParameters,
3600N/A null,
3600N/A "Get V3 session",
3600N/A null);
3600N/A
3600N/A // SNMP V3 introduces the notion of SnmpEngine. An engine is
3600N/A // associated to the session. Other objects might need
3600N/A // the engine. You can access it using getEngine method.
3600N/A //
3600N/A final SnmpEngine engine = session.getEngine();
3600N/A
3600N/A // Create an SnmpUsmPeer object for representing the entity
3600N/A // to communicate with
3600N/A //
3600N/A final SnmpUsmPeer agentV3 = new SnmpUsmPeer(engine, remoteHost, port);
3600N/A
3600N/A // Create USM parameters for the principal defaultuser (user used when
3600N/A // requests are sent: the defaultUser is a template and for this
3600N/A // reason we cannot find it under the user mib (not created as a user)
3600N/A //
3600N/A final SnmpUsmParameters paramsV3 = new SnmpUsmParameters(
3600N/A engine,
3600N/A user);
3600N/A
3600N/A // Set the security level authentication but without privacy
3600N/A //
3600N/A paramsV3.setSecurityLevel(securityLevel);
3600N/A
3600N/A // Set the context name
3600N/A //
3657N/A if (community.compareTo("null") != 0) {
3600N/A paramsV3.setContextName(community.getBytes());
3600N/A }
3600N/A
3600N/A // Set the contextEngineId discovered by the peer upon
3600N/A // its creation
3600N/A //
3600N/A paramsV3.setContextEngineId(agentV3.getEngineId().getBytes());
3600N/A
3600N/A // Associate the parameters with the agent
3600N/A //
3600N/A agentV3.setTimeout(timeOut);
3600N/A agentV3.setMaxTries(maxRetries);
3600N/A agentV3.setParams(paramsV3);
3600N/A
3667N/A if (securityLevel != SnmpDefinitions.noAuthNoPriv) {
3667N/A // Discover timeliness of creation and boot
3667N/A //
3667N/A try {
3667N/A agentV3.processUsmTimelinessDiscovery();
3667N/A } catch (SnmpStatusException e) {
3667N/A if (connectStatus.compareTo("SnmpStatusException") == 0) {
3667N/A System.out.println(
3667N/A "connect() of SNMPGet catched as expected a " +
3667N/A "SNMP status exception: " + e.getMessage() + "\"");
3667N/A } else {
3667N/A System.out.println(
3667N/A "connect() of SNMPGet should not catch a " +
3667N/A "SNMP status exception: " + e.getMessage() + "\"");
3667N/A
3667N/A rc = 1;
3667N/A }
3667N/A } catch (Exception e) {
3667N/A System.out.println(
3667N/A "connect() of SNMPGet catched an unexpected exception: " +
3667N/A e.getMessage() + "\"");
3600N/A
3667N/A rc = 1;
3667N/A }
3600N/A }
3667N/A
3600N/A if (rc == 0) {
3600N/A // Set the default peer (agent) to a SnmpSession
3600N/A //
3600N/A session.setDefaultPeer(agentV3);
3600N/A }
3600N/A break;
3600N/A default:
3600N/A System.out.println(
3600N/A "connect() of SNMPGet: Unknown SNMP version: " +
3600N/A version + " .");
3600N/A
3600N/A rc = 1;
3600N/A }
3600N/A } catch (Exception e) {
3600N/A System.out.println(
3600N/A "connect() of SNMPGet catched an unexpected exception: " +
3600N/A e.getMessage() + "\"");
3600N/A
3600N/A rc = 1;
3600N/A }
3600N/A
3600N/A if (rc == 0) {
3600N/A System.out.println("connect() of SNMPGet succeeded");
3600N/A } else {
3600N/A System.out.println("connect() of SNMPGet failed");
3600N/A }
3600N/A
3600N/A return rc;
3600N/A }
3600N/A
3600N/A /**
3600N/A * Perform an SNMP get request on SNMP agent.
3600N/A *
3600N/A * @return 0 if the getRequest succeeded or 1 if the getRequest failed
3600N/A */
3600N/A public int getRequest() {
3600N/A int rc = 0;
3667N/A String previousOID = "";
3600N/A
3600N/A try {
3600N/A // Build the list of variables you want to query
3600N/A //
3600N/A final SnmpVarBindList list = new SnmpVarBindList("Get varbind list");
3600N/A
3667N/A // Read specific OIDs
3600N/A //
3667N/A if (walk) {
3667N/A // Walk request
3667N/A //
3667N/A list.addVarBind("0.0");
3667N/A previousOID = "0.0";
3667N/A } else {
3667N/A // Get request
3667N/A //
3667N/A list.addVarBind(oids);
3667N/A }
3600N/A
3600N/A // Make the SNMP get request
3600N/A //
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Start SNMP V" + version +
3600N/A " GET request for SNMP agent on \"" + remoteHost +
3600N/A "\" at port \"" + port + "\".");
3600N/A
3667N/A while (previousOID.compareTo("end") != 0) {
3667N/A SnmpRequest request = null;
3667N/A if (walk) {
3667N/A // Walk request
3667N/A //
3667N/A request = session.snmpGetNextRequest(null, list);
3667N/A } else {
3667N/A // Get request
3667N/A //
3667N/A request = session.snmpGetRequest(null, list);
3667N/A }
3600N/A
3667N/A // Check for a timeout of the request
3600N/A //
3667N/A boolean completed =
3667N/A request.waitForCompletion((maxRetries + 1) * timeOut);
3667N/A if (completed == false) {
3667N/A if (connectStatus.compareTo("reqTimeout") != 0) {
3600N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Request timed out, " +
3667N/A "check reachability of agent.");
3600N/A
3667N/A // Print request
3600N/A //
3600N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Request= " +
3667N/A request.toString() + ".");
3600N/A
3600N/A rc = 1;
3600N/A } else {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Request timed out as expected.");
3667N/A }
3667N/A }
3667N/A
3667N/A if (rc == 0 && completed) {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Finish SNMP V" +
3667N/A version + " GET request.");
3667N/A
3667N/A // Now we have a response. Check if the response contains an error
3667N/A //
3667N/A String errorStatus = SnmpRequest.snmpErrorToString(
3667N/A request.getErrorStatus());
3667N/A if (errorStatus.compareTo("noError") != 0) {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Error status= " +
3667N/A errorStatus + ".");
3667N/A
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Error index= " +
3667N/A request.getErrorIndex() + ".");
3667N/A
3667N/A if (errorStatus.compareTo(connectStatus) == 0) {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Get request failed as " +
3667N/A "expected with " + connectStatus + " status.");
3667N/A } else {
3667N/A if (walk && errorStatus.compareTo("noSuchName") == 0) {
3600N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Get request failed as " +
3667N/A "expected with " + connectStatus + " status.");
3600N/A } else {
3600N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Get request should " +
3667N/A "fail with " + connectStatus + " status.");
3600N/A
3600N/A rc = 1;
3600N/A }
3667N/A }
3667N/A
3667N/A previousOID = "end";
3667N/A } else {
3667N/A // Now we shall display the content of the result
3667N/A //
3667N/A SnmpVarBindList resp = request.getResponseVarBindList();
3667N/A
3667N/A System.out.println("getRequest() of SNMPGet: Result=");
3667N/A
3667N/A String tmpOID = "";
3667N/A String realOID = "";
3667N/A for (int i = 0; i < resp.getVarBindCount(); i++) {
3667N/A tmpOID = resp.getVarBindAt(i).getOid().toString();
3667N/A int endIndex = tmpOID.lastIndexOf(".");
3667N/A String indexOID = tmpOID.substring(endIndex, tmpOID.length());
3667N/A
3667N/A realOID = tmpOID.substring(0, endIndex);
3667N/A if (realOID.startsWith("1.3.6.1.2.1.66.2")) {
3667N/A endIndex = realOID.lastIndexOf(".");
3667N/A realOID = realOID.substring(0, endIndex);
3667N/A }
3667N/A
3667N/A String name = resp.getVarBindAt(i).resolveVarName(realOID).getName();
3667N/A String value = resp.getVarBindAt(i).getStringValue();
3667N/A System.out.println(name + indexOID + "=" + value);
3667N/A
3667N/A if (walk) {
3667N/A list.removeVarBind(previousOID);
3667N/A list.addVarBind(tmpOID);
3667N/A previousOID = tmpOID;
3667N/A } else {
3667N/A previousOID = "end";
3667N/A }
3667N/A }
3667N/A
3667N/A if (connectStatus.compareTo("noError") != 0) {
3667N/A // Request should failed
3600N/A //
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Get request should " +
3667N/A "fail with " + connectStatus + " status.");
3600N/A
3667N/A rc = 1;
3667N/A } else {
3667N/A if (validOIDs) {
3667N/A // Check that we obtain correct values for the OIDs
3667N/A //
3667N/A if (resp.checkForValidValues()) {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Returned values for" +
3667N/A " OIDs are correct.");
3667N/A } else {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Returned values for" +
3667N/A " OIDs are not correct.");
3667N/A
3667N/A rc = 1;
3667N/A }
3600N/A } else {
3667N/A // Check that we obtain incorrect values for the OIDs
3667N/A //
3667N/A if (resp.checkForValidValues()) {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Returned values for" +
3667N/A " OIDs should not be correct.");
3667N/A
3667N/A rc = 1;
3667N/A } else {
3667N/A System.out.println(
3667N/A "getRequest() of SNMPGet: Returned values for" +
3667N/A " OIDs are not correct as expected.");
3667N/A }
3600N/A }
3600N/A }
3600N/A }
3600N/A }
3600N/A }
3600N/A } catch (Exception e) {
3600N/A System.out.println(
3600N/A "connect() of SNMPGet catched an unexpected exception: " +
3600N/A e.getMessage() + "\"");
3600N/A
3600N/A rc = 1;
3600N/A }
3600N/A
3600N/A if (rc == 0) {
3600N/A System.out.println("getRequest() of SNMPGet succeeded");
3600N/A } else {
3600N/A System.out.println("getRequest() of SNMPGet failed");
3600N/A }
3600N/A
3600N/A return rc;
3600N/A }
3600N/A
3600N/A /**
3600N/A * Close SNMP connection with SNMP agent.
3600N/A */
3600N/A public void disconnect() {
3600N/A
3600N/A // Stop and destroy the SnmpSession if still there
3600N/A try {
3600N/A session.destroySession();
3600N/A session = null;
3600N/A } catch (Exception e) {
3600N/A // possible session already ended
3600N/A }
3600N/A
3600N/A System.out.println("disconnect() of SNMPGet succeeded");
3600N/A }
3600N/A
3600N/A /**
3600N/A * Main.
3600N/A *
3600N/A * @param args arguments
3600N/A */
3600N/A public static void main(String[] args) {
3600N/A
3600N/A SNMPGet client = new SNMPGet();
3600N/A
3600N/A int rc = 0;
3600N/A
3600N/A // Retrieve parameters
3600N/A rc = client.init(args);
3600N/A
3600N/A // If init() succeeded then open connection
3600N/A if (rc == 0) {
3600N/A rc = client.connect();
3600N/A }
3600N/A
3600N/A // If connect() succeeded then perform get request
3600N/A if (rc == 0 && connectStatus.compareTo("SnmpStatusException") != 0) {
3600N/A rc = client.getRequest();
3600N/A }
3600N/A
3600N/A // Close connection
3600N/A client.disconnect();
3600N/A
3600N/A System.exit(rc);
3600N/A }
3600N/A
3600N/A // Arguments
3600N/A int version = 0;
3600N/A String remoteHost = null;
3600N/A int port = 0;
3600N/A String oids = null;
3600N/A String community = null;
3600N/A String user = null;
3600N/A int securityLevel = SnmpDefinitions.authNoPriv;
3600N/A String securityFile = null;
3600N/A static String connectStatus = null;
3600N/A boolean validOIDs = true;
3667N/A boolean walk = false;
3600N/A
3600N/A // SnmpSession
3600N/A SnmpSession session = null;
3600N/A int timeOut = 30000; // default value
3600N/A int maxRetries = 1;
3600N/A}