本文整理汇总了Java中org.snmp4j.event.ResponseEvent类的典型用法代码示例。如果您正苦于以下问题:Java ResponseEvent类的具体用法?Java ResponseEvent怎么用?Java ResponseEvent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ResponseEvent类属于org.snmp4j.event包,在下文中一共展示了ResponseEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress("23.23.52.11/161"));
target.setTimeout(3000); //3s
target.setRetries(1);
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
VariableBinding[] array = {new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.7")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.10")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.19"))};
pdu.addAll(array);
//pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response == null) {
System.out.println("TimeOut...");
} else {
if (response.getErrorStatus() == PDU.noError) {
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
for (VariableBinding vb : vbs) {
System.out.println(vb.getVariable().toString());
}
} else {
System.out.println("Error:" + response.getErrorStatusText());
}
}
}
示例2: processResponse
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* TODO: Merge this logic with {@link Snmp4JWalker.Snmp4JResponseListener#processResponse(PDU response)}
*/
private static SnmpValue[] processResponse(Snmp4JAgentConfig agentConfig, ResponseEvent responseEvent) throws IOException {
SnmpValue[] retvalues = { null };
if (responseEvent.getResponse() == null) {
log().warn("processResponse: Timeout. Agent: "+agentConfig);
} else if (responseEvent.getError() != null) {
log().warn("processResponse: Error during get operation. Error: "+responseEvent.getError().getLocalizedMessage(), responseEvent.getError());
} else if (responseEvent.getResponse().getType() == PDU.REPORT) {
log().warn("processResponse: Error during get operation. Report returned with varbinds: "+responseEvent.getResponse().getVariableBindings());
} else if (responseEvent.getResponse().getVariableBindings().size() < 1) {
log().warn("processResponse: Received PDU with 0 varbinds.");
} else if (responseEvent.getResponse().get(0).getSyntax() == SMIConstants.SYNTAX_NULL) {
log().info("processResponse: Null value returned in varbind: " + responseEvent.getResponse().get(0));
} else {
retvalues = convertResponseToValues(responseEvent);
if (log().isDebugEnabled()) {
log().debug("processResponse: SNMP operation successful, value: "+Arrays.toString(retvalues));
}
}
return retvalues;
}
示例3: snmpGet
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* 获取指定OID的 get
*
* @param snmp
* @param target
* @param oid
* @return
* @throws IOException
*/
public static PDU snmpGet(Snmp snmp, Target target, String oid) throws IOException {
ScopedPDU pdu = new ScopedPDU();
pdu.setType(PDU.GET);
pdu.add(new VariableBinding(new OID(oid)));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if(response == null){
log.warn("response null - error:{} peerAddress:{} source:{} request:{}",
responseEvent.getError(),
responseEvent.getPeerAddress(),
responseEvent.getSource(),
responseEvent.getRequest());
}
return response;
}
示例4: snmpWalk
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* walk方式获取指定的oid value
*
* @param snmp
* @param target
* @param oid
* @return
* @throws IOException
*/
public static List<PDU> snmpWalk(Snmp snmp, Target target, String oid) throws IOException {
List<PDU> pduList = new ArrayList<>();
ScopedPDU pdu = new ScopedPDU();
OID targetOID = new OID(oid);
pdu.add(new VariableBinding(targetOID));
boolean finished = false;
while (!finished) {
VariableBinding vb = null;
ResponseEvent respEvent = snmp.getNext(pdu, target);
PDU response = respEvent.getResponse();
if (null == response) {
break;
} else {
vb = response.get(0);
}
// check finish
finished = checkWalkFinished(targetOID, pdu, vb);
if (!finished) {
pduList.add(response);
// Set up the variable binding for the next entry.
pdu.setRequestID(new Integer32(0));
pdu.set(0, vb);
}
}
return pduList;
}
示例5: checkResponse
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
private void checkResponse(ResponseEvent re, Response<?> sresp)
{
if (null == re || null == re.getResponse())
{
sresp.setCode(Response.TIMEOUT);
sresp.setPhrase("No Response.");
return ;
}
PDU pdu = re.getResponse();
List<VariableBinding> errs = pdu.getBindingList(USM_ERROR_PREFIX);
if (errs != null && !errs.isEmpty())
{
sresp.setCode(Response.AUTH_FAIL);
sresp.setPhrase("AuthFail: " + errs.toString());
return ;
}
sresp.setCode(Response.OK);
sresp.setPhrase("success");
}
示例6: onResponse
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
public void onResponse(ResponseEvent event) {
// Always cancel async request when response has been received
// otherwise a memory leak is created! Not canceling a request
// immediately can be useful when sending a request to a broadcast address.
((Snmp)event.getSource()).cancel(event.getRequest(), this);
// check for valid response
if (event.getRequest() == null || event.getResponse() == null) {
// ignore null requests/responses
LOG.debug("Received invalid SNMP event. Request: " + event.getRequest() + " / Response: " + event.getResponse());
return;
}
PDU pdu = event.getResponse();
processPDU(pdu);
}
示例7: validateResponse
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected void validateResponse(ResponseEvent event) {
super.validateResponse(event);
PDU response = event.getResponse();
final int responseSize = response.size();
if (responseSize <= nonRepeaters) {
throw new TruncatedResponseException(
"response contains no repeaters; too many non-repeaters?");
}
if (nonRepeaters + repeaters >= responseSize) {
if (context.getConfig().isWalkAllowsTruncatedRepetition()) {
this.repeaters = responseSize - nonRepeaters;
}
else {
throw new TruncatedResponseException(
"response contains partial first repetition; "
+ "set walkAllowsTruncatedRepetition if you wish to allow it");
}
}
}
示例8: get
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
public ResponseEvent get() throws IOException {
send();
lock.lock();
try {
while (event == null) {
readyCondition.await();
}
if (event.getError() instanceof IOException) {
throw (IOException) event.getError();
}
return event;
}
catch (InterruptedException ex) {
// treat this as though a timeout occurred
cancel(request, this);
return new ResponseEvent(delegate, target.getAddress(),
request, null, userHandle);
}
finally {
lock.unlock();
}
}
示例9: get
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* This method is capable of handling multiple OIDs
* @param oids
* @return
* @throws IOException
*/
public Map<OID, String> get(OID oids[]) throws IOException
{
PDU pdu = createPDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, getTarget(), null);
if(event != null) {
PDU pdu2 = event.getResponse();
VariableBinding[] binds = pdu2!=null?event.getResponse().toArray():null;
if(binds!=null)
{
Map<OID, String> res = new LinkedHashMap<OID, String>(binds.length);
for(VariableBinding b: binds)
res.put(b.getOid(), b.getVariable().toString());
return res;
}else return null;
}
throw new RuntimeException("GET timed out");
}
示例10: walk
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
private static PDU walk(Snmp snmp, PDU request, Target target)
throws IOException {
request.setNonRepeaters(0);
OID rootOID = request.get(0).getOid();
PDU response = null;
int objects = 0;
int requests = 0;
long startTime = System.currentTimeMillis();
do {
requests++;
ResponseEvent responseEvent = SnmpUtil.snmp.send(request, target);
response = responseEvent.getResponse();
if (response != null) {
objects += response.size();
}
}
while (!processWalk(response, request, rootOID));
System.out.println();
System.out.println("Total requests sent: " + requests);
System.out.println("Total objects received: " + objects);
System.out.println("Total walk time: "
+ (System.currentTimeMillis() - startTime) + " milliseconds");
return response;
}
示例11: go
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
@Override
public DataResponse go() {
DataResponse response = new DataResponse(sensorid, SNMPSensorDefinition.KIND);
if(!isInitialized()){
init();
}
if(isInitialized()){
PDU pdu = getPDU();
ResponseEvent resp = SNMPUtil.getNoThrow(getSnmpTarget(), pdu);
if(resp != null){
PDU resppdu = resp.getResponse();
if(resppdu != null){
fillChannels(response, SNMPUtil.convertToMap(resppdu));
}
} else {
response = getErrorResponse("Data Error", -1, "SNMP Sensor returned no data");
}
} else {
response = getErrorResponse("Init Error", -1, "SNMP Sensor not initialized");
}
// TODO Auto-generated method stub
return response;
}
示例12: go
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
@Override
public DataResponse go() {
DataResponse response = new DataResponse(sensorid, SNMPSensorSubInterfaceDefinition.KIND);
if(!isInitialized()){
init();
}
if(isInitialized()){
PDU pdu = getPDU();
ResponseEvent resp = SNMPUtil.getNoThrow(getSnmpTarget(), pdu);
if(resp != null){
PDU resppdu = resp.getResponse();
if(resppdu != null){
fillChannels(response, SNMPUtil.convertToMap(resppdu));
}
} else {
response = getErrorResponse("Data Error", -1, "SNMP Sensor returned no data");
}
} else {
response = getErrorResponse("Init Error", -1, "SNMP Sensor not initialized");
}
// TODO Auto-generated method stub
return response;
}
示例13: get
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* Request multiple OIDs
*
* @param oids OIDS
* @return ResponseEvent
*/
public ResponseEvent get(OID oids[]) {
try {
PDU pdu = new PDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}
pdu.setType(PDU.GET);
ResponseEvent event;
event = snmp.send(pdu, getTarget(), null);
if (event != null) {
return event;
}
throw new RuntimeException("Timeout");
} catch (IOException e) {
throw new RuntimeException("Problem by response the OID");
}
}
示例14: queryFreeRAMValue
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* Calculate the RAM load
*
* @param respNow Response
* @return double Value
*/
private double queryFreeRAMValue(ResponseEvent respNow) {
try {
if (respNow != null && respNow.getResponse() != null) {
double ramFree = Double.parseDouble(respNow.getResponse().get(this.ramFreePos).toValueString());
if (ramFree != 0) {
ramFree = ramFree / 1024;
}
return ramFree;
} else {
logger.error("SNMP Response was NULL - Assuming SNMPD is offline. Exiting now!");
this.snmpError = true;
stop();
System.exit(-1);
}
return 0;
} catch (NullPointerException e) {
logger.error("SNMP RAM Values threw Nullpointer - Assuming SNMPD is offline. Exiting now!");
this.snmpError = true;
stop();
System.exit(-1);
return 0;
}
}
示例15: calculateUsedRAMValue
import org.snmp4j.event.ResponseEvent; //导入依赖的package包/类
/**
* Calculate the RAM load
*
* @param respNow Response
* @return double Value
*/
private double calculateUsedRAMValue(ResponseEvent respNow) {
try {
if (respNow != null && respNow.getResponse() != null) {
double ramFree = Double.valueOf(respNow.getResponse().get(this.ramFreePos).toValueString());
double ramTotal = Double.valueOf(respNow.getResponse().get(this.ramTotalPos).toValueString());
double ramUsed = ramTotal - ramFree;
ramUsed = ramUsed / 1024;
return ramUsed;
}
return 0;
} catch (NullPointerException e) {
return 0;
}
}