/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2008 Sun Microsystems, Inc.
*/
// OpenDS imports
// OpenDMK imports
//
/**
* This class perform a SNMP set operation.
*/
public class SNMPSet {
/**
* Gets the calling arguments.
*
* @param args SNMP agent version + SNMP agent host + SNMP agent port
* @return 0 if the init succeeded or 1 if the init failed
*/
int rc = 0;
// Missing arguments
"usage: " +
" -v <SNMP version>" +
" -h <remoteHost>" +
" -p <port>" +
" -o <oids>" +
" -c <community>" +
" -u <user>" +
" -l <securityLevel>" +
" -f <securityFile>" +
" -s <connectionStatus>" +
" -n <checkOIDs>");
rc = 1;
} else {
case 'v':
break;
case 'h':
remoteHost = val;
break;
case 'p':
break;
case 'o':
break;
case 'c':
break;
case 'u':
break;
case 'l':
} else {
rc = 1;
}
break;
case 'f':
securityFile = val;
break;
case 's':
connectStatus = val;
break;
case 'n':
break;
default:
rc = 1;
}
if (rc == 1) {
break;
}
i = i + 1;
}
}
if (rc == 0) {
} else {
}
return rc;
}
/**
* Open SNMP connection with SNMP agent.
*
* @return 0 if the connect succeeded or 1 if the connect failed
*/
public int connect() {
int rc = 0;
try {
// The OidTable generated by mibgen when compiling DIRECTORY_SERVER_MIB
//
// Specify the OidTable containing all the DIRECTORY_SERVER_MIB knowledge
//
switch (version) {
case 1:
case 2:
// Create the session
//
// Create an SnmpPeer object for representing the entity
// to communicate with.
//
// Specify the read and write community to be used
//
// Specify the protocol version
//
switch (version) {
case 1:
break;
case 2:
break;
default:
break;
}
// Associate the parameters with the agent
//
// Set the default peer (agent) to a SnmpSession
//
break;
case 3:
// Custom engine parameters
final SnmpEngineParameters engineParameters =
new SnmpEngineParameters();
// Activate encryption
// Set the security file
// Create the session
//
session = new SnmpSession(
null,
"Set V3 session",
null);
// SNMP V3 introduces the notion of SnmpEngine. An engine is
// associated to the session. Other objects might need
// the engine. You can access it using getEngine method.
//
// Create an SnmpUsmPeer object for representing the entity
// to communicate with
//
// Create USM parameters for the principal defaultuser (user used when
// requests are sent: the defaultuser is a template and for this
// reason we cannot find it under the user mib (not created as a user)
//
user);
// Set the security level authentication but without privacy
//
// Set the context name
//
}
// Set the contextEngineId discovered by the peer upon
// its creation
//
// Associate the parameters with the agent
//
// Discover timeliness of creation and boot
//
try {
} catch (SnmpStatusException e) {
"connect() of SNMPSet catched as expected a " +
} else {
"connect() of SNMPSet should not catch a " +
rc = 1;
}
} catch (Exception e) {
"connect() of SNMPSet catched an unexpected exception: " +
e.getMessage() + "\"");
rc = 1;
}
if (rc == 0) {
// Set the default peer (agent) to a SnmpSession
//
}
break;
default:
"connect() of SNMPSet: Unknown SNMP version: "
+ version + " .");
rc = 1;
}
} catch (Exception e) {
"connect() of SNMPSet catched an unexpected exception: " +
e.getMessage() + "\"");
rc = 1;
}
if (rc == 0) {
} else {
}
return rc;
}
/**
* Perform an SNMP set request on SNMP agent.
*
* @return 0 if the setRequest succeeded or 1 if the setRequest failed
*/
public int setRequest() {
int rc = 0;
try {
// Build the list of variables you want to query
//
// Write one specific OID
//
// Make the SNMP set request
//
"setRequest() of SNMPSet: Start SNMP V" + version +
" GET request for SNMP agent on \"" + remoteHost +
// Set request
//
// Check for a timeout of the request
//
if (completed == false) {
"setRequest() of SNMPSet: Request timed out, " +
"check reachability of agent.");
// Print request
//
"setRequest() of SNMPSet: Request= " +
rc = 1;
}
if (rc == 0) {
"getRequest() of SNMPGet: Finish SNMP V" +
version + " GET request.");
// Now we have a response. Check if the response contains an error
//
"getRequest() of SNMPGet: Error status= " +
errorStatus + ".");
"getRequest() of SNMPGet: Error index= " +
"getRequest() of SNMPGet: Get request failed as " +
} else {
"getRequest() of SNMPGet: Get request should " +
rc = 1;
}
} else {
// Now we shall display the content of the result
//
}
// Request should failed
//
"getRequest() of SNMPGet: Get request should " +
rc = 1;
} else {
if (validOIDs) {
// Check that we obtain correct values for the OIDs
//
if (resp.checkForValidValues()) {
"getRequest() of SNMPGet: Returned values for" +
" OIDs are correct.");
} else {
"getRequest() of SNMPGet: Returned values for" +
" OIDs are not correct.");
rc = 1;
}
} else {
// Check that we obtain incorrect values for the OIDs
//
if (resp.checkForValidValues()) {
"getRequest() of SNMPGet: Returned values for" +
" OIDs should not be correct.");
rc = 1;
} else {
"getRequest() of SNMPGet: Returned values for" +
" OIDs are not correct as expected.");
}
}
}
}
}
} catch (Exception e) {
"setRequest() of SNMPSet catched an unexpected exception: " +
e.getMessage() + "\"");
rc = 1;
}
if (rc == 0) {
} else {
}
return rc;
}
/**
* Close SNMP connection with SNMP agent.
*/
public void disconnect() {
// Stop and destroy the SnmpSession if still there
try {
} catch (Exception e) {
// possible session already ended
}
}
/**
* Main.
*
* @param args arguments
*/
int rc = 0;
// Retrieve parameters
// If init() succeeded then open connection
if (rc == 0) {
}
// If connect() succeeded then perform set request
}
// Close connection
client.disconnect();
}
// Arguments
boolean validOIDs = true;
// SnmpSession
}