⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpv1communicationinterface.java

📁 snmp inquistor Pour la communication avec les agents SNMP on utilise un package Java open source
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                        throw new SNMPSetException("OID " + itemID[i] + " expected at index " + i + ", OID " + newObjectIdentifier + " received", i+1, SNMPRequestException.FAILED);
                    }
                }
                
                break;
            
            }
            
        }
        
        
        requestID++;
    
        
        return retrievedVars;
        
    }
    
    
    
    
    /**
    *    Retrieve all MIB variable values whose OIDs start with the supplied baseID. Since the entries of
    *   an SNMP table have the form  <baseID>.<tableEntry>.<index>, this will retrieve all of the table 
    *   data as an SNMPVarBindList object consisting of sequence of SNMPVariablePairs.
    *    Uses SNMPGetNextRequests to retrieve variable values in sequence.
    *    @throws IOException Thrown when timeout experienced while waiting for response to request.
    *    @throws SNMPBadValueException 
    */
    
    public SNMPVarBindList retrieveMIBTable(String baseID)
        throws IOException, SNMPBadValueException, SNMPGetException
    {
        // send GetNextRequests until receive
        // an error message or a repeat of the object identifier we sent out
        SNMPVarBindList retrievedVars = new SNMPVarBindList();
        
        
        int errorStatus = 0;
        int errorIndex = 0;
        
        String currentID = baseID;
        SNMPObjectIdentifier requestedObjectIdentifier = new SNMPObjectIdentifier(currentID);
        
        
        while (errorStatus == 0)
        {
            
            SNMPVariablePair nextPair = new SNMPVariablePair(requestedObjectIdentifier, new SNMPNull());
            SNMPSequence varList = new SNMPSequence();
            varList.addSNMPObject(nextPair);
            SNMPPDU pdu = new SNMPPDU(SNMPBERCodec.SNMPGETNEXTREQUEST, requestID, errorStatus, errorIndex, varList);
            SNMPMessage message = new SNMPMessage(version, community, pdu);
            byte[] messageEncoding = message.getBEREncoding();
            DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, remotePort);
            
            /*
            System.out.println("Request bytes:");
            
            for (int i = 0; i < messageEncoding.length; ++i)
            {
                System.out.print(getHex(messageEncoding[i]) + " ");
            }
            */
            
            dSocket.send(outPacket);
            
            
            DatagramPacket inPacket = new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize);
        
            dSocket.receive(inPacket);
            
            byte[] encodedMessage = inPacket.getData();
            
            
            SNMPMessage receivedMessage = new SNMPMessage(SNMPBERCodec.extractNextTLV(encodedMessage,0).value);
            SNMPPDU receivedPDU = receivedMessage.getPDU();
            
            // check request identifier; if incorrect, just ignore packet and continue waiting
            if (receivedPDU.getRequestID() == requestID)
            {
                
                // check error status; if retrieval problem, just break - could be there are no additional OIDs
                if (receivedPDU.getErrorStatus() != 0)
                {
                    break;
                    //throw new SNMPGetException("OID following " + requestedObjectIdentifier + " not available for retrieval");        
                }
                
                varList = receivedPDU.getVarBindList();
                SNMPSequence newPair = (SNMPSequence)(varList.getSNMPObjectAt(0));
                
                SNMPObjectIdentifier newObjectIdentifier = (SNMPObjectIdentifier)(newPair.getSNMPObjectAt(0));
                SNMPObject newValue = newPair.getSNMPObjectAt(1);
                
                // now see if retrieved ID starts with table base; if not, done with table - break
                String newOIDString = (String)newObjectIdentifier.toString();
                if (!newOIDString.startsWith(baseID))
                    break;
                
                retrievedVars.addSNMPObject(newPair);
                    
                requestedObjectIdentifier = newObjectIdentifier;
            
                requestID++;
            
            }
            
            
        }
            
        
        return retrievedVars;
        
    }
    
    
    
    
    /**
    *    Retrieve all MIB variable values whose OIDs start with the supplied baseIDs. The normal way for
    *   this to be used is for the base OID array to consist of the base OIDs of the columns of a table.
    *   This method will then retrieve all of the entries of the table corresponding to these columns, one 
    *   row at a time (i.e., the entries for each row will be retrieved in a single SNMP request). This 
    *   will retrieve the table data as an SNMPVarBindList object consisting of sequence of SNMPVariablePairs,
    *   with the entries for each row grouped together. This may provide a more convenient arrangement of
    *   the table data than the simpler retrieveMIBTable method taking a single OID as argument; in addition,
    *   it's more efficient, requiring one SNMP request per row rather than one request per entry.
    *    Uses SNMPGetNextRequests to retrieve variable values for each row in sequence.
    *    @throws IOException Thrown when timeout experienced while waiting for response to request.
    *    @throws SNMPBadValueException 
    *   @throws SNMPGetException Thrown if incomplete row retrieved
    */
    
    public SNMPVarBindList retrieveMIBTable(String[] baseID)
        throws IOException, SNMPBadValueException, SNMPGetException
    {
        // send GetNextRequests until receive
        // an error message or a repeat of the object identifier we sent out
        SNMPVarBindList retrievedVars = new SNMPVarBindList();
        
        int errorStatus = 0;
        int errorIndex = 0;
        
        SNMPObjectIdentifier[] requestedObjectIdentifier = new SNMPObjectIdentifier[baseID.length];
        for (int i = 0; i < baseID.length; i++)
        {
               requestedObjectIdentifier[i] = new SNMPObjectIdentifier(baseID[i]);
        }
        

retrievalLoop:
        
        while (errorStatus == 0)
        {
            
            SNMPSequence varList = new SNMPSequence();
            
            for (int i = 0; i < requestedObjectIdentifier.length; i++)
            {
                SNMPVariablePair nextPair = new SNMPVariablePair(requestedObjectIdentifier[i], new SNMPNull());
                varList.addSNMPObject(nextPair);
            }
            
            SNMPPDU pdu = new SNMPPDU(SNMPBERCodec.SNMPGETNEXTREQUEST, requestID, errorStatus, errorIndex, varList);
            SNMPMessage message = new SNMPMessage(version, community, pdu);
            
            byte[] messageEncoding = message.getBEREncoding();
            
            DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, remotePort);
            
            /*
            System.out.println("Request bytes:");
            
            for (int i = 0; i < messageEncoding.length; ++i)
            {
                System.out.print(getHex(messageEncoding[i]) + " ");
            }
            */
            
            dSocket.send(outPacket);
            
            
            DatagramPacket inPacket = new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize);
        
            dSocket.receive(inPacket);
            
            byte[] encodedMessage = inPacket.getData();
            
            
            SNMPMessage receivedMessage = new SNMPMessage(SNMPBERCodec.extractNextTLV(encodedMessage,0).value);
            SNMPPDU receivedPDU = receivedMessage.getPDU();
            
            // check request identifier; if incorrect, just ignore packet and continue waiting
            if (receivedPDU.getRequestID() == requestID)
            {
                
                // check error status; if retrieval problem for error index 1, just break - assume there are no additional OIDs
                // to retrieve. If index is other than 1, throw exception
                if (receivedPDU.getErrorStatus() != 0)
                {
                    int retrievedErrorIndex = receivedPDU.getErrorIndex();
                    
                    if (retrievedErrorIndex == 1)
                    {
                        break retrievalLoop;
                    }
                    else
                    {
                        throw new SNMPGetException("OID following " + requestedObjectIdentifier[retrievedErrorIndex - 1] + " not available for retrieval", retrievedErrorIndex, receivedPDU.getErrorStatus());
                    }    
                }
                
                // copy info from retrieved sequence to var bind list
                varList = receivedPDU.getVarBindList();
                
                // make sure got the right number of vars in reply; if not, throw GetException
                if(varList.size() != requestedObjectIdentifier.length)
                {
                    throw new SNMPGetException("Incomplete row of table received", 0, SNMPRequestException.FAILED);
                }
                
                // copy the retrieved variable pairs into retrievedVars
                for (int i = 0; i < varList.size(); i++)
                {
                    SNMPSequence newPair = (SNMPSequence)(varList.getSNMPObjectAt(i));
                    
                    SNMPObjectIdentifier newObjectIdentifier = (SNMPObjectIdentifier)(newPair.getSNMPObjectAt(0));
                    SNMPObject newValue = newPair.getSNMPObjectAt(1);
                    
                    // now see if retrieved ID starts with table base; if not, done with table - break
                    String newOIDString = (String)newObjectIdentifier.toString();
                    if (!newOIDString.startsWith(baseID[i]))
                    {
                        if (i == 0)
                        {
                            // it's the first element of the row; just break
                            break retrievalLoop;
                        }
                        else
                        {
                            // it's a subsequent row element; throw exception
                            throw new SNMPGetException("Incomplete row of table received", i+1, SNMPRequestException.FAILED);
                        }
                    }
                        
                    retrievedVars.addSNMPObject(newPair);
                    
                    // set requested identifiers array to current identifiers to do get-next for next row
                    requestedObjectIdentifier[i] = newObjectIdentifier;
                }
                
                
                requestID++;
            
            }
            
            
        }
            
        
        return retrievedVars;
        
    }
    
    
    
    
    
    
    /**
    *   Set the size of the buffer used to receive response packets. RFC 1157 stipulates that an SNMP
    *   implementation must be able to receive packets of at least 484 bytes, so if you try to set the
    *   size to a value less than this, the receive buffer size will be set to 484 bytes. In addition,
    *   the maximum size of a UDP packet payload is 65535 bytes, so setting the buffer to a larger size
    *   will just waste memory. The default value is 512 bytes. The value may need to be increased if
    *   get-requests are issued for multiple OIDs.
    */
    
    public void setReceiveBufferSize(int receiveBufferSize)
    {
        if (receiveBufferSize >= 484)
        {
            this.receiveBufferSize = receiveBufferSize;
        }
        else
        {
            this.receiveBufferSize = 484;
        }
    }
    
    
    
    /**
    *   Returns the current size of the buffer used to receive response packets. 
    */
    
    public int getReceiveBufferSize()
    {
        return this.receiveBufferSize;
    }
    
    
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -