下面列出了怎么用com.sun.jmx.snmp.SnmpDefinitions的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Check the type of the pdu: only the get/set/bulk request
* are accepted.
*/
private boolean checkPduType(SnmpPduPacket pdu) {
boolean result;
switch(pdu.type) {
case SnmpDefinitions.pduGetRequestPdu:
case SnmpDefinitions.pduGetNextRequestPdu:
case SnmpDefinitions.pduSetRequestPdu:
case SnmpDefinitions.pduGetBulkRequestPdu:
result = true ;
break;
default:
if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
"checkPduType", "cannot respond to this kind of PDU");
}
result = false ;
break;
}
return result ;
}
public boolean skipVariable(long var, Object data, int pduVersion) {
switch((int)var) {
case 23:
case 22:
case 21:
case 20:
case 13:
case 12:
case 11:
case 10:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
default:
break;
}
return super.skipVariable(var,data,pduVersion);
}
/**
* This is a factory method for creating new SnmpMibRequest objects.
* @param reqPdu The received PDU.
* @param vblist The vector of SnmpVarBind objects in which the
* MIB concerned by this request is involved.
* @param version The protocol version of the SNMP request.
* @param userData User allocated contextual data.
*
* @return A new SnmpMibRequest object.
*
* @since 1.5
**/
public static SnmpMibRequest newMibRequest(SnmpPdu reqPdu,
Vector<SnmpVarBind> vblist,
int version,
Object userData)
{
return new SnmpMibRequestImpl(null,
reqPdu,
vblist,
version,
userData,
null,
SnmpDefinitions.noAuthNoPriv,
getSecurityModel(version),
null,null);
}
/**
* Make a response pdu with the specified error status and index.
* NOTE: the response pdu share its varBindList with the request pdu.
*/
private SnmpPduRequest newValidResponsePdu(SnmpPduPacket reqPdu,
SnmpVarBind[] varBindList) {
SnmpPduRequest result = new SnmpPduRequest() ;
result.address = reqPdu.address ;
result.port = reqPdu.port ;
result.version = reqPdu.version ;
result.community = reqPdu.community ;
result.type = SnmpPduRequest.pduGetResponsePdu ;
result.requestId = reqPdu.requestId ;
result.errorStatus = SnmpDefinitions.snmpRspNoError ;
result.errorIndex = 0 ;
result.varBindList = varBindList ;
((SnmpAdaptorServer)adaptorServer).
updateErrorCounters(result.errorStatus) ;
return result ;
}
@Override
public void registerSetException(SnmpVarBind var,
SnmpStatusException exception)
throws SnmpStatusException {
// The index in the exception must correspond to
// the SNMP index ...
//
if (version == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(exception, getVarIndex(var)+1);
// Although the first pass of check() did not fail,
// the set() phase could not be carried out correctly.
// Since we don't know how to make an "undo", and some
// assignation may already have been performed, we're going
// to throw an snmpRspUndoFailed.
//
throw new SnmpStatusException(SnmpDefinitions.snmpRspUndoFailed,
getVarIndex(var)+1);
}
public static int mapSetException(int errorStatus, int version)
throws SnmpStatusException {
final int errorCode = errorStatus;
if (version == SnmpDefinitions.snmpVersionOne)
return errorCode;
int mappedErrorCode = errorCode;
// Now take care of V2 errorCodes that can be stored
// in the varbind itself:
if (errorCode == SnmpStatusException.noSuchObject)
// noSuchObject => notWritable
mappedErrorCode = SnmpStatusException.snmpRspNotWritable;
else if (errorCode == SnmpStatusException.noSuchInstance)
// noSuchInstance => notWritable
mappedErrorCode = SnmpStatusException.snmpRspNotWritable;
return mappedErrorCode;
}
/**
* Make a response pdu with the specified error status and index.
* NOTE: the response pdu share its varBindList with the request pdu.
*/
private SnmpPduRequest newValidResponsePdu(SnmpPduPacket reqPdu,
SnmpVarBind[] varBindList) {
SnmpPduRequest result = new SnmpPduRequest() ;
result.address = reqPdu.address ;
result.port = reqPdu.port ;
result.version = reqPdu.version ;
result.community = reqPdu.community ;
result.type = SnmpPduRequest.pduGetResponsePdu ;
result.requestId = reqPdu.requestId ;
result.errorStatus = SnmpDefinitions.snmpRspNoError ;
result.errorIndex = 0 ;
result.varBindList = varBindList ;
((SnmpAdaptorServer)adaptorServer).
updateErrorCounters(result.errorStatus) ;
return result ;
}
static final int mapErrorStatus(int errorStatus,
int protocolVersion,
int reqPduType) {
if (errorStatus == SnmpDefinitions.snmpRspNoError)
return SnmpDefinitions.snmpRspNoError;
// Too bad, an error occurs ... we need to translate it ...
//
if (protocolVersion == SnmpDefinitions.snmpVersionOne)
return mapErrorStatusToV1(errorStatus,reqPduType);
if (protocolVersion == SnmpDefinitions.snmpVersionTwo ||
protocolVersion == SnmpDefinitions.snmpVersionThree)
return mapErrorStatusToV2(errorStatus,reqPduType);
return SnmpDefinitions.snmpRspGenErr;
}
/**
* Make a response pdu with the specified error status and index.
* NOTE: the response pdu share its varBindList with the request pdu.
*/
private SnmpPduRequest newValidResponsePdu(SnmpPduPacket reqPdu,
SnmpVarBind[] varBindList) {
SnmpPduRequest result = new SnmpPduRequest() ;
result.address = reqPdu.address ;
result.port = reqPdu.port ;
result.version = reqPdu.version ;
result.community = reqPdu.community ;
result.type = SnmpPduRequest.pduGetResponsePdu ;
result.requestId = reqPdu.requestId ;
result.errorStatus = SnmpDefinitions.snmpRspNoError ;
result.errorIndex = 0 ;
result.varBindList = varBindList ;
((SnmpAdaptorServer)adaptorServer).
updateErrorCounters(result.errorStatus) ;
return result ;
}
/**
* Setter for the "JvmMemoryGCCall" variable.
*/
public void setJvmMemoryGCCall(EnumJvmMemoryGCCall x)
throws SnmpStatusException {
if (x.intValue() == JvmMemoryGCCallStart.intValue()) {
final Map<Object, Object> m = JvmContextFactory.getUserData();
try {
ManagementFactory.getMemoryMXBean().gc();
if (m != null) m.put("jvmMemory.getJvmMemoryGCCall",
JvmMemoryGCCallStarted);
} catch (Exception ex) {
if (m != null) m.put("jvmMemory.getJvmMemoryGCCall",
JvmMemoryGCCallFailed);
}
return;
}
throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
}
/**
* Make a response pdu with the specified error status and index.
* NOTE: the response pdu share its varBindList with the request pdu.
*/
private SnmpPduRequest newValidResponsePdu(SnmpPduPacket reqPdu,
SnmpVarBind[] varBindList) {
SnmpPduRequest result = new SnmpPduRequest() ;
result.address = reqPdu.address ;
result.port = reqPdu.port ;
result.version = reqPdu.version ;
result.community = reqPdu.community ;
result.type = SnmpPduRequest.pduGetResponsePdu ;
result.requestId = reqPdu.requestId ;
result.errorStatus = SnmpDefinitions.snmpRspNoError ;
result.errorIndex = 0 ;
result.varBindList = varBindList ;
((SnmpAdaptorServer)adaptorServer).
updateErrorCounters(result.errorStatus) ;
return result ;
}
/**
* This is a factory method for creating new SnmpMibRequest objects.
* @param reqPdu The received PDU.
* @param vblist The vector of SnmpVarBind objects in which the
* MIB concerned by this request is involved.
* @param version The protocol version of the SNMP request.
* @param userData User allocated contextual data.
*
* @return A new SnmpMibRequest object.
*
* @since 1.5
**/
public static SnmpMibRequest newMibRequest(SnmpPdu reqPdu,
Vector<SnmpVarBind> vblist,
int version,
Object userData)
{
return new SnmpMibRequestImpl(null,
reqPdu,
vblist,
version,
userData,
null,
SnmpDefinitions.noAuthNoPriv,
getSecurityModel(version),
null,null);
}
public boolean skipVariable(long var, Object data, int pduVersion) {
switch((int)var) {
case 23:
case 22:
case 21:
case 20:
case 13:
case 12:
case 11:
case 10:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
default:
break;
}
return super.skipVariable(var,data,pduVersion);
}
/**
* Processes a <CODE>get</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests.
*
* @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public void get(SnmpMibRequest inRequest) throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"get", "Get in Exception");
if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(SnmpStatusException.noSuchName);
Enumeration<SnmpVarBind> l = inRequest.getElements();
while(l.hasMoreElements()) {
SnmpVarBind varbind = l.nextElement();
varbind.setNoSuchObject();
}
}
public static int mapSetException(int errorStatus, int version)
throws SnmpStatusException {
final int errorCode = errorStatus;
if (version == SnmpDefinitions.snmpVersionOne)
return errorCode;
int mappedErrorCode = errorCode;
// Now take care of V2 errorCodes that can be stored
// in the varbind itself:
if (errorCode == SnmpStatusException.noSuchObject)
// noSuchObject => notWritable
mappedErrorCode = SnmpStatusException.snmpRspNotWritable;
else if (errorCode == SnmpStatusException.noSuchInstance)
// noSuchInstance => notWritable
mappedErrorCode = SnmpStatusException.snmpRspNotWritable;
return mappedErrorCode;
}
static final int mapErrorStatus(int errorStatus,
int protocolVersion,
int reqPduType) {
if (errorStatus == SnmpDefinitions.snmpRspNoError)
return SnmpDefinitions.snmpRspNoError;
// Too bad, an error occurs ... we need to translate it ...
//
if (protocolVersion == SnmpDefinitions.snmpVersionOne)
return mapErrorStatusToV1(errorStatus,reqPduType);
if (protocolVersion == SnmpDefinitions.snmpVersionTwo ||
protocolVersion == SnmpDefinitions.snmpVersionThree)
return mapErrorStatusToV2(errorStatus,reqPduType);
return SnmpDefinitions.snmpRspGenErr;
}
/**
* Processes a <CODE>getNext</CODE> operation. It will throw an exception for V1 requests or it will set exceptions within the list for V2 requests..
*
* @param inRequest The SnmpMibRequest object holding the list of variables to be retrieved.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public void getNext(SnmpMibRequest inRequest) throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"getNext", "GetNext in Exception");
if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(SnmpStatusException.noSuchName);
Enumeration<SnmpVarBind> l = inRequest.getElements();
while(l.hasMoreElements()) {
SnmpVarBind varbind = l.nextElement();
varbind.setEndOfMibView();
}
}
/**
* Processes a <CODE>getBulk</CODE> operation. It will throw an exception if the request is a V1 one or it will set exceptions within the list for V2 ones.
*
* @param inRequest The SnmpMibRequest object holding the list of variable to be retrieved.
*
* @exception SnmpStatusException An error occurred during the operation.
*/
@Override
public void getBulk(SnmpMibRequest inRequest, int nonRepeat, int maxRepeat)
throws SnmpStatusException {
SNMP_ADAPTOR_LOGGER.logp(Level.FINEST,
SnmpErrorHandlerAgent.class.getName(),
"getBulk", "GetBulk in Exception");
if(inRequest.getVersion() == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(SnmpDefinitions.snmpRspGenErr, 0);
Enumeration<SnmpVarBind> l = inRequest.getElements();
while(l.hasMoreElements()) {
SnmpVarBind varbind = l.nextElement();
varbind.setEndOfMibView();
}
}
public boolean skipVariable(long var, Object data, int pduVersion) {
switch((int)var) {
case 2:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
default:
break;
}
return super.skipVariable(var,data,pduVersion);
}
@Override
public void registerGetException(SnmpVarBind var,
SnmpStatusException exception)
throws SnmpStatusException {
// The index in the exception must correspond to
// the SNMP index ...
//
if (version == SnmpDefinitions.snmpVersionOne)
throw new SnmpStatusException(exception, getVarIndex(var)+1);
if (var == null)
throw exception;
// If we're doing a getnext ==> endOfMibView
if (getnextflag) {
var.value = SnmpVarBind.endOfMibView;
return;
}
final int errorCode = mapGetException(exception.getStatus(),
version);
// Now take care of V2 errorCodes that can be stored
// in the varbind itself:
if (errorCode ==
SnmpStatusException.noSuchObject)
// noSuchObject => noSuchObject
var.value= SnmpVarBind.noSuchObject;
else if (errorCode ==
SnmpStatusException.noSuchInstance)
// noSuchInstance => noSuchInstance
var.value= SnmpVarBind.noSuchInstance;
else
throw new SnmpStatusException(errorCode, getVarIndex(var)+1);
}
public boolean skipVariable(long var, Object data, int pduVersion) {
switch((int)var) {
case 3:
case 2:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
default:
break;
}
return super.skipVariable(var,data,pduVersion);
}
public boolean skipVariable(long var, Object data, int pduVersion) {
switch((int)var) {
case 33:
case 32:
case 31:
case 132:
case 131:
case 13:
case 12:
case 11:
case 10:
case 111:
case 110:
case 5:
case 23:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
case 1:
return true;
case 22:
case 21:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
default:
break;
}
return super.skipVariable(var,data,pduVersion);
}
SnmpPduPacket makeNoMibErrorPdu(SnmpPduRequest req, Object userData) {
// There is no agent registered
//
if (req.version == SnmpDefinitions.snmpVersionOne) {
// Version 1: => NoSuchName
return
newErrorResponsePdu(req,snmpRspNoSuchName,1);
} else if (req.version == SnmpDefinitions.snmpVersionTwo) {
// Version 2: => depends on PDU type
switch (req.type) {
case pduSetRequestPdu :
case pduWalkRequest :
// SET request => NoAccess
return
newErrorResponsePdu(req,snmpRspNoAccess,1);
case pduGetRequestPdu :
// GET request => NoSuchObject
return
makeErrorVarbindPdu(req,SnmpDataTypeEnums.
errNoSuchObjectTag);
case pduGetNextRequestPdu :
case pduGetBulkRequestPdu :
// GET-NEXT or GET-BULK => EndOfMibView
return
makeErrorVarbindPdu(req,SnmpDataTypeEnums.
errEndOfMibViewTag);
default:
}
}
// Something wrong here: => snmpRspGenErr
return newErrorResponsePdu(req,snmpRspGenErr,1);
}
/**
* Checker for the "JvmMemPoolThreshold" variable.
*/
public void checkJvmMemPoolThreshold(Long x) throws SnmpStatusException {
// if threshold is -1, it means that low memory detection is not
// supported.
if (!pool.isUsageThresholdSupported())
throw new
SnmpStatusException(SnmpDefinitions.snmpRspInconsistentValue);
final long val = x.longValue();
if (val < 0 )
throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
}
/**
* Checks the passed msg flags according to the rules specified in RFC 2572.
* @param msgFlags The msg flags.
*/
public static void checkSecurityLevel(byte msgFlags)
throws SnmpBadSecurityLevelException {
int secLevel = msgFlags & SnmpDefinitions.authPriv;
if((secLevel & SnmpDefinitions.privMask) != 0)
if((secLevel & SnmpDefinitions.authMask) == 0) {
throw new SnmpBadSecurityLevelException("Security level:"+
" noAuthPriv!!!");
}
}
private SnmpPduPacket mergeNextResponses(SnmpPduRequest req) {
int max= req.varBindList.length;
SnmpVarBind[] result= new SnmpVarBind[max];
// Go through the list of subrequests and concatenate.
// Hopefully, by now all the sub-requests should be finished
//
for(Enumeration<SnmpSubRequestHandler> e= subs.elements(); e.hasMoreElements();) {
SnmpSubRequestHandler sub= e.nextElement();
sub.updateResult(result);
}
if (req.version == snmpVersionTwo) {
return newValidResponsePdu(req,result);
}
// In v1 make sure there is no endOfMibView ...
//
for(int i=0; i < max; i++) {
SnmpValue val= result[i].value;
if (val == SnmpVarBind.endOfMibView)
return newErrorResponsePdu(req,
SnmpDefinitions.snmpRspNoSuchName, i+1);
}
// So far so good ...
//
return newValidResponsePdu(req,result);
}
SnmpPduPacket makeNoMibErrorPdu(SnmpPduRequest req, Object userData) {
// There is no agent registered
//
if (req.version == SnmpDefinitions.snmpVersionOne) {
// Version 1: => NoSuchName
return
newErrorResponsePdu(req,snmpRspNoSuchName,1);
} else if (req.version == SnmpDefinitions.snmpVersionTwo) {
// Version 2: => depends on PDU type
switch (req.type) {
case pduSetRequestPdu :
case pduWalkRequest :
// SET request => NoAccess
return
newErrorResponsePdu(req,snmpRspNoAccess,1);
case pduGetRequestPdu :
// GET request => NoSuchObject
return
makeErrorVarbindPdu(req,SnmpDataTypeEnums.
errNoSuchObjectTag);
case pduGetNextRequestPdu :
case pduGetBulkRequestPdu :
// GET-NEXT or GET-BULK => EndOfMibView
return
makeErrorVarbindPdu(req,SnmpDataTypeEnums.
errEndOfMibViewTag);
default:
}
}
// Something wrong here: => snmpRspGenErr
return newErrorResponsePdu(req,snmpRspGenErr,1);
}
private static int getSecurityModel(int version) {
switch(version) {
case SnmpDefinitions.snmpVersionOne:
return SnmpDefinitions.snmpV1SecurityModel;
default:
return SnmpDefinitions.snmpV2SecurityModel;
}
}
SnmpPduPacket makeNoMibErrorPdu(SnmpPduRequest req, Object userData) {
// There is no agent registered
//
if (req.version == SnmpDefinitions.snmpVersionOne) {
// Version 1: => NoSuchName
return
newErrorResponsePdu(req,snmpRspNoSuchName,1);
} else if (req.version == SnmpDefinitions.snmpVersionTwo) {
// Version 2: => depends on PDU type
switch (req.type) {
case pduSetRequestPdu :
case pduWalkRequest :
// SET request => NoAccess
return
newErrorResponsePdu(req,snmpRspNoAccess,1);
case pduGetRequestPdu :
// GET request => NoSuchObject
return
makeErrorVarbindPdu(req,SnmpDataTypeEnums.
errNoSuchObjectTag);
case pduGetNextRequestPdu :
case pduGetBulkRequestPdu :
// GET-NEXT or GET-BULK => EndOfMibView
return
makeErrorVarbindPdu(req,SnmpDataTypeEnums.
errEndOfMibViewTag);
default:
}
}
// Something wrong here: => snmpRspGenErr
return newErrorResponsePdu(req,snmpRspGenErr,1);
}
public boolean skipVariable(long var, Object data, int pduVersion) {
switch((int)var) {
case 33:
case 32:
case 31:
case 132:
case 131:
case 13:
case 12:
case 11:
case 10:
case 111:
case 110:
case 5:
case 23:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
case 1:
return true;
case 22:
case 21:
if (pduVersion==SnmpDefinitions.snmpVersionOne) return true;
break;
default:
break;
}
return super.skipVariable(var,data,pduVersion);
}