/*
* 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 get operation.
*/
public class SNMPGet {
/**
* Gets the calling arguments.
*
* @param args SNMP version + SNMP agent host + SNMP agent port + community
* @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>" +
" -u <user>" +
" -l <securityLevel>" +
" -f <securityFile>" +
" -s <connectionStatus>" +
" -n <checkOIDs>" +
" -w <snmpwalk>");
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;
case 'w':
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
//
// Disable PduFixedOnError option
//
// 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;
}
// 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,
"Get 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 SNMPGet catched as expected a " +
} else {
"connect() of SNMPGet should not catch a " +
rc = 1;
}
} catch (Exception e) {
"connect() of SNMPGet catched an unexpected exception: " +
e.getMessage() + "\"");
rc = 1;
}
}
if (rc == 0) {
// Set the default peer (agent) to a SnmpSession
//
}
break;
default:
"connect() of SNMPGet: Unknown SNMP version: " +
version + " .");
rc = 1;
}
} catch (Exception e) {
"connect() of SNMPGet catched an unexpected exception: " +
e.getMessage() + "\"");
rc = 1;
}
if (rc == 0) {
} else {
}
return rc;
}
/**
* Perform an SNMP get request on SNMP agent.
*
* @return 0 if the getRequest succeeded or 1 if the getRequest failed
*/
public int getRequest() {
int rc = 0;
try {
// Build the list of variables you want to query
//
// Read specific OIDs
//
if (walk) {
// Walk request
//
previousOID = "0.0";
} else {
// Get request
//
}
// Make the SNMP get request
//
"getRequest() of SNMPGet: Start SNMP V" + version +
" GET request for SNMP agent on \"" + remoteHost +
if (walk) {
// Walk request
//
} else {
// Get request
//
}
// Check for a timeout of the request
//
boolean completed =
if (completed == false) {
"getRequest() of SNMPGet: Request timed out, " +
"check reachability of agent.");
// Print request
//
"getRequest() of SNMPGet: Request= " +
rc = 1;
} else {
"getRequest() of SNMPGet: Request timed out as expected.");
}
}
"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 failed as " +
} else {
"getRequest() of SNMPGet: Get request should " +
rc = 1;
}
}
previousOID = "end";
} else {
// Now we shall display the content of the result
//
}
if (walk) {
} else {
previousOID = "end";
}
}
// 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) {
"connect() of SNMPGet 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 get request
}
// Close connection
client.disconnect();
}
// Arguments
boolean validOIDs = true;
boolean walk = false;
// SnmpSession
}