private void notifyResponse(ResponseEvent response, String targetOid) {
   if (response == null) {
     for (ResponseHandler responseHandler : responseHandlers) {
       responseHandler.onTimeout(targetOid);
     }
   } else {
     PDU responsePDU = response.getResponse();
     int errorStatus = responsePDU.getErrorStatus();
     int errorIndex = responsePDU.getErrorIndex();
     String errorStatusText = responsePDU.getErrorStatusText();
     switch (errorStatus) {
       case SnmpConstants.SNMP_ERROR_SUCCESS:
         for (ResponseHandler responseHandler : responseHandlers) {
           responseHandler.onSuccess(targetOid);
         }
         break;
       default:
         if (log.isDebugEnabled()) {
           log.debug("Error: Request Failed");
           log.debug("Error Status = " + errorStatus);
           log.debug("Error Index = " + errorIndex);
           log.debug("Error Status Text = " + errorStatusText);
         }
         for (ResponseHandler responseHandler : responseHandlers) {
           responseHandler.onError(targetOid, errorStatus, errorStatusText);
         }
         break;
     }
   }
 }
Exemple #2
0
  public void SentGetSnmp() throws IOException {

    TransportMapping transport = new DefaultUdpTransportMapping();
    transport.listen();

    // Target
    CommunityTarget target = CreateAndSetTarget();

    // PDU
    PDU pdu = CreateAndSetPdu();

    // SNMP
    Snmp snmp = new Snmp(transport);

    // System.out.println("Sending request to " + ipFromInput + " ...");
    ResponseEvent response = snmp.get(pdu, target);

    if (response != null) {
      // System.out.println("Got response from " + ipFromInput);
      PDU responsePdu = response.getResponse();

      if (responsePdu != null) {
        int errorStatus = responsePdu.getErrorStatus();
        int errorIndex = responsePdu.getErrorIndex();
        String errorStatusText = responsePdu.getErrorStatusText();

        if (errorStatus == PDU.noError) {
          readResponse(responsePdu.getVariableBindings());
          // System.out.println(responsePdu.getVariableBindings().getClass());
          // System.out.println("SNMP get response: " + responsePdu.getVariableBindings());
        } else {
          System.out.println("Error: Request Failed");
          System.out.println("Error Status: " + errorStatus);
          System.out.println("Error Index: " + errorIndex);
          System.out.println("Error Status Text: " + errorStatusText);
        }
      } else {
        System.out.println("Error: PDU response is null");
      }
    } else {
      System.out.println("Error: Agent Timeout");
    }
    snmp.close();
  }