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 *
6982N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6982N/A * or http://forgerock.org/license/CDDLv1.0.html.
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
6982N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6982N/A * If applicable, add the following below this CDDL HEADER, with the
6982N/A * fields enclosed by brackets "[]" replaced with your own identifying
6982N/A * 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.SnmpString;
3600N/Aimport com.sun.management.snmp.SnmpVarBind;
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 set operation.
3600N/A */
3600N/Apublic class SNMPSet {
3600N/A
3600N/A /**
3600N/A * Gets the calling arguments.
3600N/A *
3600N/A * @param args SNMP agent version + SNMP agent host + SNMP agent port
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>" +
3600N/A " -u <user>" +
3600N/A " -l <securityLevel>" +
3600N/A " -f <securityFile>" +
3600N/A " -s <connectionStatus>" +
3600N/A " -n <checkOIDs>");
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;
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 SNMPSet succeeded");
3600N/A } else {
3600N/A System.out.println("init() of SNMPSet 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("Set V" + version + " session");
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 default:
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 "Set 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
3600N/A // Discover timeliness of creation and boot
3600N/A //
3600N/A try {
3600N/A agentV3.processUsmTimelinessDiscovery();
3600N/A } catch (SnmpStatusException e) {
3600N/A if (connectStatus.compareTo("SnmpStatusException") == 0) {
3600N/A System.out.println(
3600N/A "connect() of SNMPSet catched as expected a " +
3600N/A "SNMP status exception: " + e.getMessage() + "\"");
3600N/A } else {
3600N/A System.out.println(
3600N/A "connect() of SNMPSet should not catch a " +
3600N/A "SNMP status exception: " + e.getMessage() + "\"");
3600N/A
3600N/A rc = 1;
3600N/A }
3600N/A } catch (Exception e) {
3600N/A System.out.println(
3600N/A "connect() of SNMPSet 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 // 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 SNMPSet: 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 SNMPSet 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 SNMPSet succeeded");
3600N/A } else {
3600N/A System.out.println("connect() of SNMPSet succeeded");
3600N/A }
3600N/A
3600N/A return rc;
3600N/A }
3600N/A
3600N/A /**
3600N/A * Perform an SNMP set request on SNMP agent.
3600N/A *
3600N/A * @return 0 if the setRequest succeeded or 1 if the setRequest failed
3600N/A */
3600N/A public int setRequest() {
3600N/A int rc = 0;
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
3600N/A // Write one specific OID
3600N/A //
3600N/A SnmpVarBind oid = new SnmpVarBind(oids);
3600N/A oid.setSnmpValue(new SnmpString("myValue"));
3600N/A list.addVarBind(oid);
3600N/A
3600N/A // Make the SNMP set request
3600N/A //
3600N/A System.out.println(
3600N/A "setRequest() of SNMPSet: Start SNMP V" + version +
3600N/A " GET request for SNMP agent on \"" + remoteHost +
3600N/A "\" at port \"" + port + "\".");
3600N/A
3600N/A // Set request
3600N/A //
3600N/A SnmpRequest request = session.snmpSetRequest(null, list);
3600N/A
3600N/A // Check for a timeout of the request
3600N/A //
3600N/A boolean completed = request.waitForCompletion((maxRetries + 1) * timeOut);
3600N/A if (completed == false) {
3600N/A System.out.println(
3600N/A "setRequest() of SNMPSet: Request timed out, " +
3600N/A "check reachability of agent.");
3600N/A
3600N/A // Print request
3600N/A //
3600N/A System.out.println(
3600N/A "setRequest() of SNMPSet: Request= " +
3600N/A request.toString() + ".");
3600N/A
3600N/A rc = 1;
3600N/A }
3600N/A
3600N/A if (rc == 0) {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Finish SNMP V" +
3600N/A version + " GET request.");
3600N/A
3600N/A // Now we have a response. Check if the response contains an error
3600N/A //
3600N/A String errorStatus = SnmpRequest.snmpErrorToString(
3600N/A request.getErrorStatus());
3600N/A if (errorStatus.compareTo("noError") != 0) {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Error status= " +
3600N/A errorStatus + ".");
3600N/A
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Error index= " +
3600N/A request.getErrorIndex() + ".");
3600N/A
3600N/A if (errorStatus.compareTo(connectStatus) == 0) {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Get request failed as " +
3600N/A "expected with " + connectStatus + " status.");
3600N/A } else {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Get request should " +
3600N/A "fail with " + connectStatus + " status.");
3600N/A
3600N/A rc = 1;
3600N/A }
3600N/A } else {
3600N/A // Now we shall display the content of the result
3600N/A //
3600N/A SnmpVarBindList resp = request.getResponseVarBindList();
3600N/A
3600N/A System.out.println("getRequest() of SNMPGet: Result=");
3600N/A
3600N/A for (int i = 0; i < resp.getVarBindCount(); i++) {
3600N/A System.out.println(resp.getVarBindAt(i));
3600N/A }
3600N/A
3600N/A if (connectStatus.compareTo("noError") != 0) {
3600N/A // Request should failed
3600N/A //
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Get request should " +
3600N/A "fail with " + connectStatus + " status.");
3600N/A
3600N/A rc = 1;
3600N/A } else {
3600N/A if (validOIDs) {
3600N/A // Check that we obtain correct values for the OIDs
3600N/A //
3600N/A if (resp.checkForValidValues()) {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Returned values for" +
3600N/A " OIDs are correct.");
3600N/A } else {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Returned values for" +
3600N/A " OIDs are not correct.");
3600N/A
3600N/A rc = 1;
3600N/A }
3600N/A } else {
3600N/A // Check that we obtain incorrect values for the OIDs
3600N/A //
3600N/A if (resp.checkForValidValues()) {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Returned values for" +
3600N/A " OIDs should not be correct.");
3600N/A
3600N/A rc = 1;
3600N/A } else {
3600N/A System.out.println(
3600N/A "getRequest() of SNMPGet: Returned values for" +
3600N/A " OIDs are not correct as expected.");
3600N/A }
3600N/A }
3600N/A }
3600N/A }
3600N/A }
3600N/A } catch (Exception e) {
3600N/A System.out.println(
3600N/A "setRequest() of SNMPSet 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("setRequest() of SNMPSet succeeded");
3600N/A } else {
3600N/A System.out.println("setRequest() of SNMPSet 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 SNMPSet 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 SNMPSet client = new SNMPSet();
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 set request
3600N/A if (rc == 0 && connectStatus.compareTo("SnmpStatusException") != 0) {
3600N/A rc = client.setRequest();
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;
3600N/A
3600N/A // SnmpSession
3600N/A SnmpSession session = null;
3600N/A
3600N/A int timeOut = 30000; // default value
3600N/A int maxRetries = 1;
3600N/A}